Class HttpError

HTTP error class with status code

Example

// Throw 404 error with default message
throw new HttpError(404)
throw new HttpError(HttpStatusCodes.NOT_FOUND)

// Throw 404 error with custom message
throw new HttpError(404, 'X not found')
throw new HttpError(HttpStatusCodes.NOT_FOUND, 'X not found')

// Throw 500 error and expose custom message to user
throw new HttpError(HttpStatusCodes.INTERNAL_SERVER_ERROR, 'Missing configuration', {expose: true})

// Handle HttpError
if (err instanceof HttpError) {
console.log(err.message)
return err.publicMessage
}

Hierarchy

  • Error
    • HttpError

Constructors

  • Parameters

    • status: number
    • Optional message: string

      error message, if not provided message from [[HttpStatusReasons]] is used

    • Optional properties: {
          expose: boolean;
      }

      additional configuration

      • expose: boolean

        Should error message be exposed to the user

    Returns HttpError

    Throws

    Error if status is not supported

Properties

expose: boolean

Should error message be exposed to the user

message: string
name: string
publicMessage: string

this.message if this.status is true else message from [[HttpStatusReasons]] is used

stack?: string
status: number

HTTP status code

prepareStackTrace?: ((err, stackTraces) => any)

Optional override for formatting stack traces

Type declaration

stackTraceLimit: number

Methods

  • Create .stack property on a target object

    Parameters

    • targetObject: object
    • Optional constructorOpt: Function

    Returns void

Generated using TypeDoc