Hanlding User Input Error Responses in Express

Hanlding User Input Error Responses in Express
0

#1

I’m currently working through the Information & Security projects and on the Converter project I’ve run into something that I’ve been unsure of for a while and am hoping someone has some knowledge they can pass on.

In the Converter project you take user input in the form of a query string. There are a few ways that they can make a bad request:

  1. They don’t provide ?input=(VALUE_HERE)
  2. They provide a number that is not valid (a double fraction, a negative number).
  3. They provide and invalid unit (miles vs mi, kilometers vs km)

How would I decide how to handle these errors? What status codes? Do I throw errors? Do I return a json object?

A successful request will be returned a json object and a 200 status.


#2

Hi @hagnerd

Basically, you would retrieve the value of the input on the server like this:

router.get('/some/path', (req, res) => {
  const input = req.params.input
  // Do something with the input
})

Then you have to verify the input. As you said, the user can make a bad request. Well there’s a status code for that which is 400. You can find all the status codes and their descriptions here.

One way of handling such an error with Express would be the following:

router.get('/some/path', (req, res) => {
  const input = req.params.input

  let isValid = verify(input)


  // The input is correct
  if (isValid) {
    let convertedInput = convert(input)
    return res.json(convertedInput)
  }

  // Incorrect input
  return res.status(400).json('The input is invalid')
})

It’s up to you to decide what to send to the user. You can be more detailed and send different error messages for every possible error.

So what I wrote above can become this instead:

router.get('/some/path', (req, res) => {
  const input = req.params.input

  let unitIsInvalid = verifyUnit(input)
  let isNegativeInput = verifyNotNegativeInput(input)

  if (unitIsInvalid) {
    return res.status(400).json('Invalid unit')
  }

  if (isNegativeInput) {
    return res.status(400).json('Negative input')
  }

  // Etc.


  let convertedInput = convert(input)
  return res.json(convertedInput)
})

It’s really up to you to decide how detailed you want to handle the errors.
Hope it helps!


#3

Thanks @thomlom for the response!

Is it standard practice to respond with a status code and payload communicating the error (whether that’s a JSON, template, etc.) instead of throwing an error?


#4

In both cases, you will catch an error on the client-side. Indeed, requests that have status code like 400, 500, 404, etc. are errors. Responding with a status code and a payload is just more precise than just throwing an error. I don’t know if it’s standard practice but I personally prefer to do that :slight_smile:


#5

Awesome thanks again for the response!

I guess because I’m entirely self-taught I always seek out best practices instead of trusting my gut.

My inclination was to respond with a 4xx status code, and json payload with the error message because that is what I prefer when consuming an API.