diff --git a/packages/rest/src/runtime/error.mts b/packages/rest/src/runtime/error.mts index 186ecc0..e5a3b50 100644 --- a/packages/rest/src/runtime/error.mts +++ b/packages/rest/src/runtime/error.mts @@ -1,12 +1,12 @@ import {HttpRequest, HttpResponse} from './http-event.mjs'; -import {Result} from "@nornir/core"; +import {AttachmentRegistry, Result} from "@nornir/core"; /** * Base error type for exceptions in rest handlers. * Can be directly converted into a HTTP response. */ export abstract class NornirRestError extends Error implements NodeJS.ErrnoException { - public abstract toHttpResponse(): HttpResponse; + public abstract toHttpResponse(registry: AttachmentRegistry): HttpResponse | Promise; // eslint-disable-next-line @typescript-eslint/no-explicit-any public static isNornirRestError(err: any): err is NornirRestError { @@ -25,18 +25,18 @@ export abstract class NornirRestRequestError extend super(message); } - abstract toHttpResponse(): HttpResponse; + abstract toHttpResponse(registry: AttachmentRegistry): HttpResponse | Promise; } interface ErrorMapping { errorMatch(error: unknown): boolean; - toHttpResponse(error: unknown): HttpResponse; + toHttpResponse(error: unknown, registry: AttachmentRegistry): HttpResponse | Promise; } // eslint-disable-next-line @typescript-eslint/no-explicit-any -export function mapErrorClass T>(error: TClass, toHttpResponse: (err: T) => HttpResponse): ErrorMapping; +export function mapErrorClass T>(error: TClass, toHttpResponse: (err: T, registry: AttachmentRegistry) => HttpResponse | Promise): ErrorMapping; // eslint-disable-next-line @typescript-eslint/no-explicit-any -export function mapErrorClass T>(error: TClass, toHttpResponse: (err: T) => HttpResponse): ErrorMapping { +export function mapErrorClass T>(error: TClass, toHttpResponse: (err: T, registry: AttachmentRegistry) => HttpResponse | Promise): ErrorMapping { return { errorMatch: (err: unknown): err is T => err instanceof error, toHttpResponse @@ -50,19 +50,19 @@ export function mapError(errorMatch: (err: unknown) => err is T, toHttpRespon } } -export function httpErrorHandler(errorMappings?: ErrorMapping[]): (input: Result) => HttpResponse { +export function httpErrorHandler(errorMappings?: ErrorMapping[]): (input: Result, registry: AttachmentRegistry) => Promise { const defaultedErrorMappings = errorMappings || []; - return (input: Result) => { + return async (input: Result, registry: AttachmentRegistry) => { if (input.isErr) { const error = input.error; const mapping = defaultedErrorMappings.find(mapping => mapping.errorMatch(error)); const responseConverter = mapping?.toHttpResponse if (responseConverter != undefined) { - return responseConverter(error); + return responseConverter(error, registry); } if (NornirRestError.isNornirRestError(error)) { - return error.toHttpResponse(); + return error.toHttpResponse(registry); } } return input.unwrap(); diff --git a/packages/test/src/rest.ts b/packages/test/src/rest.ts index 1f0f5b1..8a2e88e 100644 --- a/packages/test/src/rest.ts +++ b/packages/test/src/rest.ts @@ -37,7 +37,7 @@ const frameworkChain = nornir() })) .use(router()) .useResult(httpErrorHandler([ - mapErrorClass(TestError, err => ({ + mapErrorClass(TestError, (err) => ({ statusCode: HttpStatusCode.InternalServerError, headers: { "content-type": AnyMimeType,