@jordanforeman/rest-exceptions
v1.1.0
Published
Common REST Exceptions for use in Express APIs
Downloads
5
Readme
@jordanforeman/rest-exceptions
A set of common REST exceptions to be used in conjunction with @jordanforeman/api-framework
.
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 Name | Status Code | Default Message | Documentation |
|----------------|-------------|-----------------|---------------|
| BadRequestException
| 400
| Bad Request | MDN |
| ConflictingResourceException
| 409
| Conflict | MDN |
| ForbiddenException
| 403
| Forbidden | MDN |
| GenericRestException
| 500
| Internal Server Error | MDN |
| NotFoundException
| 404
| Not Found | MDN |
| UnauthorizedException
| 401
| Unauthorized | MDN |