Skip to content

The type of the error argument of ErrorHandlingMiddleware should be Error or string #286

@adamgcoulon

Description

@adamgcoulon

While working with ErrorHandlingMiddleware I noticed that the error argument is a string when calling response.error(code, message). The message argument enters the ErrorHandlingMiddleware as the error argument, however, it is a string and not an Error object. This results in us needing to check the type of error at runtime and asserting its type as a string if so.

https://github.com/jeremydaly/lambda-api/blob/main/index.d.ts#L53

const middleware: ErrorHandlingMiddleware = (error, request, response, next) => {

  // At runtime, check if `error` is a string
  const isErrorString = typeof error === 'string';

  if (isErrorString) {
    handleErrorString(error as string, request, response, next);
  } else {
    handleErrorObject(error, request, response, next);
  }

};

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions