0.0.2 • Published 3 years ago

@mersocarlin/api-error v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

@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

PropTypeDefaultDescription
messageStringApiErrorMessage that describes the type of error
statusCodeNumber500Status code
originalErrorObjectOriginal error thrown (from try/catch, promise reject...)

BadGatewayError

PropTypeDefaultDescription
messageStringBad GatewayMessage that describes the type of error
errorObjectOriginal error thrown (from try/catch, promise reject...)

Note: Same applies for all other error types.

0.0.2

3 years ago

0.0.1

4 years ago