Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAPI validator middleware should throw a custom error #445

Closed
1 task
mkurapov opened this issue Mar 25, 2024 · 0 comments · Fixed by #446
Closed
1 task

OpenAPI validator middleware should throw a custom error #445

mkurapov opened this issue Mar 25, 2024 · 0 comments · Fixed by #446
Assignees

Comments

@mkurapov
Copy link
Contributor

Context

While working on

in Rafiki, I discovered that the OpenAPI validator middleware uses the ctx.throw method directly when handling errors:

} catch (err) {
if (err instanceof Koa.HttpError) {
throw err
} else if (isValidationError(err)) {
ctx.throw(err.status ?? 500, err.errors[0])
} else {
console.log('err=', err)
ctx.throw(500)
}
}

If we want to use a custom error (like the planned OpenPaymentsServer error in Rafiki) this prevents us from writing a general error error handler at the beginning of the middleware chain, since this middleware will always throw ctx.throw(500) for something other than a Koa.HttpError (and have a busy console.error log as well, without control over it).

By throwing a custom error instead of a ctx.throw, we can directly determine whether the particular Koa error came from this validation middleware or not, allowing us to be more explicit in how we handle the error and how we log the contents of the error.

Todos

  • Have the OpenAPI middleware throw a custom error (e.g. OpenAPIValidatorMiddlewareError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant