Express catch-all error handling with custom message or property

Viewed 16

I'm using Express inside Netlify Functions to create a custom API for my front-end Vue app. In that, I'm connecting to a few other third-party API endpoints.

While I've got a custom error-handling middleware working correctly, it still doesn't give me any ability to write a DRY code. Not sure if that's the correct way or if I'm missing something.

For example, I have something like (someRoute.ts):

import type {NextFunction, Response} from 'express'
import type {Request} from 'express-serve-static-core'
import axios from 'axios'
export default function (request : Request, response : Response, next : NextFunction) {
  axios({
    url: '/api1/'
  }).then(() => {
    axios({
      url: '/api2/'
    }).then(() => {
      response.status(200).json({
       message: 'sequential API call completed'
      })
    }).catch(next)
  }).catch(next)
}

Now, if my /api1/ call fails, I wish to send a different error message than when it fails at /api2/. There are 2 reasons I wish to do this:

  1. If a user reports an error - I wish to be able to locate, where exactly my code failed. I might have 5 or 6 API calls to complete in a single route, so it would be difficult to locate at what stage the API failed. A custom message per error or some kind of a custom property would be helpful.
  2. I'd also like to be able to present a meaningful error message to users, if possible.

To achieve 1, I thought of attaching a custom property to the request:

import type {NextFunction, Response} from 'express'
import type {Request} from 'express-serve-static-core'
import axios from 'axios'
export default function (request : Request, response : Response, next : NextFunction) {
  request.stage = 101
  axios({
    url: '/api1/'
  }).then(() => {
    request.stage = 102
    axios({
      url: '/api2/'
    }).then(() => {
      response.status(200).json({
       message: 'sequential API call completed'
      })
    }).catch(next)
  }).catch(next)
}

To achieve 2, the only way I know is using:

import type {Response} from 'express'
import type {Request} from 'express-serve-static-core'
import axios from 'axios'
export default function (request : Request, response : Response) {
  axios({
    url: '/api1/'
  }).then(() => {
    axios({
      url: '/api2/'
    }).then(() => {
      response.status(200).json({
       message: 'sequential API call completed'
      })
    }).catch(() => {
      throw new Error('Error from API 2')
      // I might as well call response.send(500) here
      // no point having a custom error handling middleware
      // if I have to manually send errors
    })
  }).catch(() => {
    throw new Error('Error from API 1')
  })
}

I can possibly use my approach for 1 and in my front-end present error messages based on error codes - but it might soon get messy to handle too many error codes.

If this is not the correct way and if others have a better way of handling errors, please let me know.

0 Answers
Related