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

View File

@ -1,3 +1,4 @@
import type { HttpErrorOptions } from './HttpError';
import { HttpError } 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.'. * Default message is 'The given input is not supported by the server configuration.'.
* @param message - Optional, more specific, message. * @param message - Optional, more specific, message.
* @param options - Optional error options.
*/ */
public constructor(message?: string) { public constructor(message?: string, options?: HttpErrorOptions) {
super(400, 'BadRequestHttpError', message ?? 'The given input is not supported by the server configuration.'); super(400,
'BadRequestHttpError',
message ?? 'The given input is not supported by the server configuration.',
options);
} }
public static isInstance(error: any): error is BadRequestHttpError { public static isInstance(error: any): error is BadRequestHttpError {

View File

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

View File

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

View File

@ -1,5 +1,10 @@
import { isError } from './ErrorUtil'; import { isError } from './ErrorUtil';
export interface HttpErrorOptions {
cause?: unknown;
errorCode?: string;
}
/** /**
* A class for all errors that could be thrown by Solid. * 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. * 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 { export class HttpError extends Error {
protected static readonly statusCode: number; protected static readonly statusCode: number;
public 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. * 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 statusCode - HTTP status code needed for the HTTP response.
* @param name - Error name. Useful for logging and stack tracing. * @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); super(message);
this.statusCode = statusCode; this.statusCode = statusCode;
this.name = name; this.name = name;
this.options = options;
} }
public static isInstance(error: any): error is HttpError { public static isInstance(error: any): error is HttpError {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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