1.1.0 • Published 4 years ago

@jordanforeman/rest-exceptions v1.1.0

Weekly downloads
4
License
MIT
Repository
-
Last release
4 years ago

@jordanforeman/rest-exceptions

A set of common REST exceptions to be used in conjunction with @jordanforeman/api-framework.

semantic-release npm.io npm.io Renovate enabled

Installation

$ npm i --save @jordanforeman/rest-exceptions

Usage

When something goes wrong in your application, choose which error to throw, and do so. Below is an example of throwing a 404 Not Found exception:

import {NotFoundException} from '@jordanforeman/rest-exceptions';

function getTheThing(id) {
    try {
        const myThing = await repository.getTheThingById(id);

        return myThing;
    } catch (e) {
        throw new NotFoundException();
    }
}

...and with a custom error message

import {NotFoundException} from '@jordanforeman/rest-exceptions';

function getTheThing(id) {
    try {
        const myThing = await repository.getTheThingById(id);

        return myThing;
    } catch (e) {
        throw new NotFoundException('Could not find the thing you were looking for!');
    }
}

Available Exceptions

Exception NameStatus CodeDefault MessageDocumentation
BadRequestException400Bad RequestMDN
ConflictingResourceException409ConflictMDN
ForbiddenException403ForbiddenMDN
GenericRestException500Internal Server ErrorMDN
NotFoundException404Not FoundMDN
UnauthorizedException401UnauthorizedMDN