refactor: Assign properties directly to error.

This commit is contained in:
Ruben Verborgh
2021-07-12 17:24:08 +01:00
parent 8a62938c18
commit 649f7a9a78
4 changed files with 33 additions and 11 deletions

View File

@@ -14,7 +14,7 @@ import { TypedRepresentationConverter } from './TypedRepresentationConverter';
* Serializes an Error by filling in the provided template.
* Content-type is based on the constructor parameter.
*
* In case the input Error has an `options.errorCode` value,
* In case the input Error has an `errorCode` value,
* the converter will look in the `descriptions` for a file
* with the exact same name as that error code + `extension`.
* The templating engine will then be applied to that file.
@@ -56,8 +56,8 @@ export class ErrorToTemplateConverter extends TypedRepresentationConverter {
}
private async getErrorCodeMessage(error: Error): Promise<string | undefined> {
if (HttpError.isInstance(error) && error.options.errorCode) {
const filePath = joinFilePath(this.descriptions, `${error.options.errorCode}${this.extension}`);
if (HttpError.isInstance(error) && error.errorCode) {
const filePath = joinFilePath(this.descriptions, `${error.errorCode}${this.extension}`);
let template: string;
try {
template = await fsPromises.readFile(filePath, 'utf8');
@@ -66,7 +66,7 @@ export class ErrorToTemplateConverter extends TypedRepresentationConverter {
return;
}
return this.engine.apply(template, (error.options.details ?? {}) as NodeJS.Dict<string>);
return this.engine.apply(template, (error.details ?? {}) as NodeJS.Dict<string>);
}
}
}

View File

@@ -10,10 +10,12 @@ export interface HttpErrorOptions {
* 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.
*/
export class HttpError extends Error {
export class HttpError extends Error implements HttpErrorOptions {
protected static readonly statusCode: number;
public readonly statusCode: number;
public readonly options: HttpErrorOptions;
public readonly cause?: unknown;
public readonly errorCode?: string;
public readonly details?: NodeJS.Dict<unknown>;
/**
* Creates a new HTTP error. Subclasses should call this with their fixed status code.
@@ -26,7 +28,9 @@ export class HttpError extends Error {
super(message);
this.statusCode = statusCode;
this.name = name;
this.options = options;
this.cause = options.cause;
this.errorCode = options.errorCode;
this.details = options.details;
}
public static isInstance(error: any): error is HttpError {