Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to catch server error? #162

Open
ujlm opened this issue Oct 4, 2023 · 0 comments
Open

How to catch server error? #162

ujlm opened this issue Oct 4, 2023 · 0 comments

Comments

@ujlm
Copy link

ujlm commented Oct 4, 2023

I have a custom API route which might throw an error when a user is not authenticated server-side. How to properly catch this error on the clients-side uploadToS3 function? Simply wrapping it in a try/catch function doesn't provide me with the error message sent from the server.

Client
const { url } = await uploadToS3(file, { endpoint: { request: { body, }, }, });

Server

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {

  if (userIsNotAuthenticated) {
    return res
    .status(401)
    .json({ message: "User not authenticated" });
  }

  try {
    await uploadHandler(newReq as NextApiRequest, res);
  } catch (error) {
    return res
    .status(500)
    .json({ message: "S3 error" });
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant