All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 50s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
39 lines
905 B
TypeScript
39 lines
905 B
TypeScript
import { Context, Next } from "@oak/oak";
|
|
|
|
import { APIErrorCode, APIFailure } from "../../shared/api.ts";
|
|
import { APIException } from "../lib/api-exception.ts";
|
|
|
|
export async function errorMiddleware(ctx: Context, next: Next) {
|
|
try {
|
|
await next();
|
|
} catch (err) {
|
|
if (err instanceof APIException) {
|
|
const responseBody: APIFailure = {
|
|
success: false,
|
|
error: {
|
|
code: err.code,
|
|
message: err.message,
|
|
},
|
|
};
|
|
|
|
ctx.response.status = err.status;
|
|
ctx.response.body = responseBody;
|
|
|
|
console.log(responseBody);
|
|
} else {
|
|
console.error(err);
|
|
|
|
const responseBody: APIFailure = {
|
|
success: false,
|
|
error: {
|
|
code: APIErrorCode.SERVER_ERROR,
|
|
message: "Unexpected server error",
|
|
},
|
|
};
|
|
|
|
ctx.response.status = 500;
|
|
ctx.response.body = responseBody;
|
|
}
|
|
}
|
|
}
|