Skip to content

Commit

Permalink
fix: fix global error handling for ServerExceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lennykean committed Feb 23, 2021
1 parent 03095d3 commit 350cd59
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
7 changes: 6 additions & 1 deletion core/mv-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ContextAccessor } from "./context-accessor.ts";

export type NextMiddlewareFunction = () => Promise<void>;

/**
Expand All @@ -7,5 +9,8 @@ export type NextMiddlewareFunction = () => Promise<void>;
* Middleware will be executed immediately before a request is processed.
*/
export interface MvMiddleware {
execute(context: unknown, next: NextMiddlewareFunction): Promise<void>;
execute(
context: ContextAccessor,
next: NextMiddlewareFunction,
): Promise<void>;
}
19 changes: 10 additions & 9 deletions core/server-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ export class ServerController {
},
parameterMetadatas?.map((parameterMetadata) => ({
index: parameterMetadata.index,
name: parameterMetadata.name,
type: parameterMetadata.type,
})),
);
Expand All @@ -255,15 +256,6 @@ export class ServerController {

private async handleError(err: unknown, contextAccessor: ContextAccessor) {
this.#logger.error(err, "An unhandled exception occurred");
for (const handler of this.getGlobalErrorHandlers()) {
try {
const result = await handler(err, contextAccessor);
if (result && result.handled) {
return;
}
// deno-lint-ignore no-empty
} catch {}
}
if (err instanceof ServerException) {
try {
let content: string;
Expand All @@ -283,6 +275,15 @@ export class ServerController {
// deno-lint-ignore no-empty
} catch {}
}
for (const handler of this.getGlobalErrorHandlers()) {
try {
const result = await handler(err, contextAccessor);
if (result && result.handled) {
return;
}
// deno-lint-ignore no-empty
} catch {}
}
contextAccessor.setBody("An unknown error occurred");
contextAccessor.setHeader("Content-Type", "text/plain");
contextAccessor.setStatus(500);
Expand Down

0 comments on commit 350cd59

Please sign in to comment.