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.

Install

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

HttpError

Extends: Error

new HttpError(message, status)

Creates an instance of HttpError.
Available status codes.

ParamTypeDescription
messageStringError message
statusNumberHTTP status code.
responseObjectHTTP response.

HttpError.sendError(res, e)

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

ParamTypeDescription
responseResponseResponse from SiteVision router.
errorErrorError to be sent.

Example

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);
}
});

BadRequestError

Extends: HttpError

new BadRequestError(message)

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

ParamType
messageString

Example

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

UnauthorizedError

Extends: HttpError

new UnauthorizedError(message)

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

ParamType
messageString

Example

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

ForbiddenError

Extends: HttpError

new ForbiddenError(message)

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

ParamType
messageString

Example

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

NotFoundError

Extends: HttpError

new NotFoundError(message)

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

ParamType
messageString

Example

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

InternalServerError

Extends: HttpError

new InternalServerError()

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

ParamType
messageString

Example

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

ServiceUnavailableError

Extends: HttpError

new ServiceUnavailableError()

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

ParamType
messageString

Example

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

sendError(res, e)

Helper function to set status and send error.

ParamTypeDescription
resResponseResponse from SiteVision router.
eErrorError to be sent.

Example

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