0

Which is the proper way to return errors with your server in general (node/express) and reject a promise?


A) Like this: res.json({status: 1, message: 'Test missing phone.', error: 'phone'})

And then pull the error 'code' out of error and the message out of messsage on your front end, resulting in a fulfilled promise that has an error and message in it.


B) Like this;

res.status(500)
res.json({message: 'Test missing phone.'})

And then just pull message out and call .reject with the message. Or res.error or something.


Do either of these cause a 'rejected' promise or 'error' callback that would make frontend stuff easier?

Should I be causing promise/callback rejection when I get an error, or just send a fulfilled promise with an error in the object like A?

Which of these is the 'correct' way to return an error from your server to your front end?

Currently I use method A, but it seems pretty dumb because I have to handle the error twice, backend (to actually send the custom error) and frontend (to check if the fulfilled promise has an error object, and then display a popup). Method B would eliminate the 'checking if the fulfilled promise has an error object' part, since a rejected promise is an error by nature.

EDIT: I'm using React Native as frontend.

EDIT2: If you res.status(500).json({test: 'Test missing phone.'}) on your backend you can console.log(error.response.data) on the frontend to make "B" a viable option.

2
  • Depends on the cause of the error. I'd send 500 only for errors that should not happen (database unavailable, log file full). Commented Mar 28, 2017 at 21:56
  • restcookbook.com/HTTP%20Methods/400-vs-500 Commented Mar 28, 2017 at 22:05

2 Answers 2

3

TL;DR Use your "B" method

Depends on what you're using on the front-end. If it's Angular and you're using the $http object for example, the promise will get rejected if there's an error status (for example, a 400 status code) according to the docs.

So to allow the front end to "know" when there's an error automatically (instead of having to do some extra checking to see if the body contains an error message), you should probably go with the res.status() approach on the back-end. HTTP status codes are the model that you should be using for your front-end to know what's going on in the server, and most front-end HTTP libraries are built with this model in mind.

You can send additional info in the body of your response too if you want. You can have custom app code on the front-end that handles more granular error cases. For example, a status of 401 would tell the client that they're unauthorized to do some action on the server, and a body with a code member might further define exactly what the issue was, along with some human-readable description (which you already have as message in your example).

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer - but how can I access the 'message' if I'm using "B" - it doesn't actually seem to come through and I just get Error: Request failed with status code 500. I'm using axios and React Native if that matters. error.message is the same thing - I need my custom error message.
For example if I res.status(500) and then res.json({message: 'show-phone-missing-popup'}) I need the res.json to come through so I know which popup to show, but I only get res.status coming through. Hope that makes sense.
@ARMATAV no problem. I'm not familiar with the libs you're using with axios and React Native. It seems it is a new question maybe worth asking separately on StackOverflow to find someone who knows the answer: "How do I access both the status and body of an API response using axios?", or however you want to format your question.
Actually I got it. If you JSON.stringify(error) you get a list of stuff - error.response.data gives me the res.json({ message: 'bla' }) that I sent through.
@ARMATAV perfect :) glad you were able to find a solution for that.
0

Adding in the res.status(500) is proper in terms of best practices. If you say that there are 'errors with your server' you should definitely label the response as such - i.e. response code 500 which indicates exactly that.

While you know what kind of responses to expect, if this were a project you were developing with others or an API to be shared to the public, it would seem counterintuitive to not get a 500. Sending that 500 will make handling the error more straightforward whether using promises or vanilla ajax.

That said, no reason you can't also include the full JSON object that you mentioned from part A (i.e. {status: 1, message: 'Test missing phone.', error: 'phone'}) to provide more info to your front end.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.