feat: Allow HttpErrors to store cause and errorCode

This commit is contained in:
Joachim Van Herwegen 2021-06-30 15:34:09 +02:00
parent dfb8ac938d
commit e44c337d0f
11 changed files with 45 additions and 22 deletions

View File

@ -1,3 +1,4 @@
import type { HttpErrorOptions } from '../../../util/errors/HttpError';
import { HttpError } from '../../../util/errors/HttpError';
/**
@ -7,8 +8,8 @@ import { HttpError } from '../../../util/errors/HttpError';
export class IdpInteractionError extends HttpError {
public readonly prefilled: Record<string, string>;
public constructor(status: number, message: string, prefilled: Record<string, string>) {
super(status, 'IdpInteractionError', message);
public constructor(status: number, message: string, prefilled: Record<string, string>, options?: HttpErrorOptions) {
super(status, 'IdpInteractionError', message, options);
this.prefilled = prefilled;
}

View File

@ -1,3 +1,4 @@
import type { HttpErrorOptions } from './HttpError';
import { HttpError } from './HttpError';
/**
@ -8,9 +9,13 @@ export class BadRequestHttpError extends HttpError {
/**
* Default message is 'The given input is not supported by the server configuration.'.
* @param message - Optional, more specific, message.
* @param options - Optional error options.
*/
public constructor(message?: string) {
super(400, 'BadRequestHttpError', message ?? 'The given input is not supported by the server configuration.');
public constructor(message?: string, options?: HttpErrorOptions) {
super(400,
'BadRequestHttpError',
message ?? 'The given input is not supported by the server configuration.',
options);
}
public static isInstance(error: any): error is BadRequestHttpError {

View File

@ -1,10 +1,11 @@
import type { HttpErrorOptions } from './HttpError';
import { HttpError } from './HttpError';
/**
* An error thrown when a request conflict with current state of the server.
*/
export class ConflictHttpError extends HttpError {
public constructor(message?: string) {
super(409, 'ConflictHttpError', message);
public constructor(message?: string, options?: HttpErrorOptions) {
super(409, 'ConflictHttpError', message, options);
}
public static isInstance(error: any): error is ConflictHttpError {

View File

@ -1,11 +1,12 @@
import type { HttpErrorOptions } from './HttpError';
import { HttpError } from './HttpError';
/**
* An error thrown when an agent is not allowed to access data.
*/
export class ForbiddenHttpError extends HttpError {
public constructor(message?: string) {
super(403, 'ForbiddenHttpError', message);
public constructor(message?: string, options?: HttpErrorOptions) {
super(403, 'ForbiddenHttpError', message, options);
}
public static isInstance(error: any): error is ForbiddenHttpError {

View File

@ -1,5 +1,10 @@
import { isError } from './ErrorUtil';
export interface HttpErrorOptions {
cause?: unknown;
errorCode?: string;
}
/**
* A class for all errors that could be thrown by Solid.
* All errors inheriting from this should fix the status code thereby hiding the HTTP internals from other components.
@ -7,17 +12,20 @@ import { isError } from './ErrorUtil';
export class HttpError extends Error {
protected static readonly statusCode: number;
public readonly statusCode: number;
public readonly options: HttpErrorOptions;
/**
* Creates a new HTTP error. Subclasses should call this with their fixed status code.
* @param statusCode - HTTP status code needed for the HTTP response.
* @param name - Error name. Useful for logging and stack tracing.
* @param message - Message to be thrown.
* @param message - Error message.
* @param options - Optional options.
*/
public constructor(statusCode: number, name: string, message?: string) {
public constructor(statusCode: number, name: string, message?: string, options: HttpErrorOptions = {}) {
super(message);
this.statusCode = statusCode;
this.name = name;
this.options = options;
}
public static isInstance(error: any): error is HttpError {

View File

@ -1,10 +1,11 @@
import type { HttpErrorOptions } from './HttpError';
import { HttpError } from './HttpError';
/**
* A generic error message, given when an unexpected condition was encountered and no more specific message is suitable.
*/
export class InternalServerError extends HttpError {
public constructor(message?: string) {
super(500, 'InternalServerError', message);
public constructor(message?: string, options?: HttpErrorOptions) {
super(500, 'InternalServerError', message, options);
}
public static isInstance(error: any): error is InternalServerError {

View File

@ -1,10 +1,11 @@
import type { HttpErrorOptions } from './HttpError';
import { HttpError } from './HttpError';
/**
* An error thrown when data was found for the requested identifier, but is not supported by the target resource.
*/
export class MethodNotAllowedHttpError extends HttpError {
public constructor(message?: string) {
super(405, 'MethodNotAllowedHttpError', message);
public constructor(message?: string, options?: HttpErrorOptions) {
super(405, 'MethodNotAllowedHttpError', message, options);
}
public static isInstance(error: any): error is MethodNotAllowedHttpError {

View File

@ -1,10 +1,11 @@
import type { HttpErrorOptions } from './HttpError';
import { HttpError } from './HttpError';
/**
* An error thrown when no data was found for the requested identifier.
*/
export class NotFoundHttpError extends HttpError {
public constructor(message?: string) {
super(404, 'NotFoundHttpError', message);
public constructor(message?: string, options?: HttpErrorOptions) {
super(404, 'NotFoundHttpError', message, options);
}
public static isInstance(error: any): error is NotFoundHttpError {

View File

@ -1,11 +1,13 @@
import type { HttpErrorOptions } from './HttpError';
import { HttpError } from './HttpError';
/**
* The server either does not recognize the request method, or it lacks the ability to fulfil the request.
* Usually this implies future availability (e.g., a new feature of a web-service API).
*/
export class NotImplementedHttpError extends HttpError {
public constructor(message?: string) {
super(501, 'NotImplementedHttpError', message);
public constructor(message?: string, options?: HttpErrorOptions) {
super(501, 'NotImplementedHttpError', message, options);
}
public static isInstance(error: any): error is NotImplementedHttpError {

View File

@ -1,11 +1,12 @@
import type { HttpErrorOptions } from './HttpError';
import { HttpError } from './HttpError';
/**
* An error thrown when an agent is not authorized.
*/
export class UnauthorizedHttpError extends HttpError {
public constructor(message?: string) {
super(401, 'UnauthorizedHttpError', message);
public constructor(message?: string, options?: HttpErrorOptions) {
super(401, 'UnauthorizedHttpError', message, options);
}
public static isInstance(error: any): error is UnauthorizedHttpError {

View File

@ -1,11 +1,12 @@
import type { HttpErrorOptions } from './HttpError';
import { HttpError } from './HttpError';
/**
* An error thrown when the media type of incoming data is not supported by a parser.
*/
export class UnsupportedMediaTypeHttpError extends HttpError {
public constructor(message?: string) {
super(415, 'UnsupportedMediaTypeHttpError', message);
public constructor(message?: string, options?: HttpErrorOptions) {
super(415, 'UnsupportedMediaTypeHttpError', message, options);
}
public static isInstance(error: any): error is UnsupportedMediaTypeHttpError {