UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

48 lines (47 loc) 2.1 kB
import { setBaseErrorOptions } from "./BaseError.js"; /** Throw when a request isn't well-formed or is unacceptable in some way. */ export class RequestError extends Error { /** HTTP status code for this error in the range `400-499`, defaults to `400` */ code; constructor(message, { code = 400, ...options } = {}) { super(message, options); this.code = code; setBaseErrorOptions(RequestError, this, options); } } RequestError.prototype.name = "RequestError"; /** Throw if an operation failed because the user is not logged in, or the login information is not well-formed. */ export class UnauthorizedError extends RequestError { constructor(message, options) { super(message, { caller: UnauthorizedError, code: 401, ...options }); } } UnauthorizedError.prototype.name = "UnauthorizedError"; /** Throw if the requested content is not found. */ export class NotFoundError extends RequestError { constructor(message, options) { super(message, { caller: NotFoundError, code: 404, ...options }); } } NotFoundError.prototype.name = "NotFoundError"; /** Throw when a request is valid and well-formed, but its actual data is not. */ export class UnprocessableError extends RequestError { constructor(message, options) { super(message, { caller: UnprocessableError, code: 422, ...options }); } } UnprocessableError.prototype.name = "UnprocessableError"; /** Throw if an operation failed because the user is logged in, but does not have sufficient privileges to access this content. */ export class ForbiddenError extends RequestError { constructor(message, options) { super(message, { caller: ForbiddenError, code: 403, ...options }); } } ForbiddenError.prototype.name = "ForbiddenError"; /** Throw if a request uses an HTTP method that is not supported. */ export class MethodNotAllowedError extends RequestError { constructor(message, options) { super(message, { caller: MethodNotAllowedError, code: 405, ...options }); } } MethodNotAllowedError.prototype.name = "MethodNotAllowedError";