Hoppa till innehåll
This package has changed name, was previously @soleil-api/http-error.
More information

@soleil-se/http-error

Extension of Error to be able to set HTTP status and send error response.

Terminal window
npm i @soleil-se/http-error

Extends: Error

Creates an instance of HttpError.
Available status codes.

ParamTypeDescription
messageStringError message
statusNumberHTTP status code.
responseObjectHTTP response.

Helper function to set status and send error.
Static method of HttpError.

ParamTypeDescription
responseResponseResponse from SiteVision router.
errorErrorError to be sent.
import HttpError from '@soleil-se/http-error';
router.get('/', (req, res) => {
try {
if (notFound) {
throw new HttpError('Something was not found', 404);
}
} catch (e) {
HttpError.sendError(res, e);
}
});

Extends: HttpError

Creates an instance of BadRequestError.
Has HTTP status code 400.

ParamType
messageString
import { BadRequestError } from '@soleil-se/http-error';
throw new BadRequestError('400 - Bad request!');

Extends: HttpError

Creates an instance of UnauthorizedError.
Has HTTP status code 401.

ParamType
messageString
import { UnauthorizedError } from '@soleil-se/http-error';
throw new UnauthorizedError('401 - Unauthorized!');

Extends: HttpError

Creates an instance of ForbiddenError.
Has HTTP status code 403.

ParamType
messageString
import { ForbiddenError } from '@soleil-se/http-error';
throw new ForbiddenError('403 - Forbidden!');

Extends: HttpError

Creates an instance of NotFoundError.
Has HTTP status code 404.

ParamType
messageString
import { NotFoundError } from '@soleil-se/http-error';
throw new NotFoundError('404 - Not found!');

Extends: HttpError

Creates an instance of InternalServerError.
Has HTTP status code 500.

ParamType
messageString
import { InternalServerError } from '@soleil-se/http-error';
throw new InternalServerError('500 - Internal server error!');

Extends: HttpError

Creates an instance of InternalServerError.
Has HTTP status code 503.

ParamType
messageString
import { ServiceUnavailableError } from '@soleil-se/http-error';
throw new ServiceUnavailableError('503 - Service unavailable error!');

Helper function to set status and send error.

ParamTypeDescription
resResponseResponse from SiteVision router.
eErrorError to be sent.
import { sendError } from '@soleil-se/http-error';
router.get('', (req, res) => {
try {
// Do stuff
} catch (e) {
sendError(res, e);
}
});