@mersocarlin/api-error
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

@mersocarlin/api-error

Named API errors with original status code and custom message.

Install

yarn add @mersocarlin/api-error
npm i @mersocarlin/api-error

Errors included

  • BadGateway
  • BadRequest
  • Conflict
  • Forbidden
  • GatewayTimeout
  • InternalServerError
  • MethodNotAllowed
  • NotFound
  • PaymentRequired
  • RequestTimeout
  • TooManyRequests
  • Unauthorized

Usage

import { ApiError, UnauthorizedError } from '@mersocarlin/api-error'


function login(credentials) {
  try {
    await doLogin(credentials)
  } catch (error) {
    throw new UnauthorizedError('Invalid credentials', error)
  }
}

function otherOperation() {
  try {
    ...
  } catch (error) {
    throw new ApiError('Something went wrong', 500, error)
  }
}

API

ApiError

Prop Type Default Description
message String ApiError Message that describes the type of error
statusCode Number 500 Status code
originalError Object Original error thrown (from try/catch, promise reject...)

BadGatewayError

Prop Type Default Description
message String Bad Gateway Message that describes the type of error
error Object Original error thrown (from try/catch, promise reject...)

Note: Same applies for all other error types.

Readme

Keywords

Package Sidebar

Install

npm i @mersocarlin/api-error

Weekly Downloads

8

Version

0.0.2

License

MIT

Unpacked Size

8.68 kB

Total Files

5

Last publish

Collaborators

  • mersocarlin