initial commit, boilerplate stuff
This commit is contained in:
37
api/middleware/error.ts
Normal file
37
api/middleware/error.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { Context, Next } from "@oak/oak";
|
||||
|
||||
import { APIErrorCode, APIException, APIFailure } from "../model/interfaces.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user