This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(errors): add error middleware that handles erorrs thrown, based o…
…n their types and returns proper status codes This type of middleware is particularly useful for this type of problem. We have various instances in which warp throws errors when failing to evaluate a contract state, and we want to parse those properly and return the correct status code when they occur. Right now we over thrown 503 errors, which suggest to clients that the app failed to proc ess, when it's more likely they are requesting contracts that have not been mined yet or do not adhere to specs define d by warp.
- Loading branch information
dtfiedler
committed
Sep 20, 2023
1 parent
e77d1f9
commit c05dbb9
Showing
6 changed files
with
256 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { KoaContext } from '../types'; | ||
import { Next } from 'koa'; | ||
import { BadRequestError, EvaluationError, NotFoundError } from '../types'; | ||
|
||
// globally handle errors and return proper status based on their type | ||
export async function errorMiddleware(ctx: KoaContext, next: Next) { | ||
const { logger } = ctx.state; | ||
try { | ||
await next(); | ||
} catch (error) { | ||
logger.error('Error processing request.', { | ||
error: error instanceof Error ? error.message : error, | ||
}); | ||
if (error instanceof EvaluationError) { | ||
ctx.status = 400; | ||
ctx.body = error.message; | ||
} else if (error instanceof NotFoundError) { | ||
ctx.status = 404; | ||
ctx.body = error.message; | ||
} else if (error instanceof BadRequestError) { | ||
ctx.status = 400; | ||
ctx.body = error.message; | ||
} else { | ||
ctx.status = 503; | ||
ctx.body = 'Internal server error.'; | ||
} | ||
} | ||
} |
Oops, something went wrong.