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

refactor: add option to configure error propagation #67

Merged

Conversation

neodmy
Copy link
Contributor

@neodmy neodmy commented Jun 2, 2023

Description

Provide an additional option to control whether the library should send the response with the validation error or let it get passed to the next error handler.

Related Issue

No issue related so far. I will add the link when created.

Motivation and Context

I'm working on a project in which it is necessary to register errors to a vendor analytic server. One of the approaches using the SDK of that tool is by setting an Express error handler and explicitly executing one of the functions available on their API. In our project, we are using swagger-endpoint-validation. The problem is that in the current implementation, validation errors are always captured, and therefore they never reach our error handler. We've tried initializing swagger-endpoint-validation after our own error handlers have been registered in the Express stack, but then the payload and params validation is not performed, so faulty data reaches our business logic making it crash.

After checking with the team, we agreed that adding an additional option that controls whether the error handle that this library uses propagates the error, or sends the response with the validation error.

To ensure backward compatibility, the error propagation is off by default. So current users of this library are not forced to set their own error middleware necessarily.

How Has This Been Tested?

I've used Node 16, as specified in the .nvmrc. Tests consist of 3 test cases:

  • If propagateError is true, the validation error should reach the next error handler
  • If propagateError is false or not defined, the validation error should not reach the next error handler

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

debug(JSON.stringify(error));
res.status(error.status || 500).send(error);
if (options.propagateError) {
Copy link

@Betisman Betisman Jun 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we always have a non null options object? Otherwise, this line could throw an error instead of jumping to the else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm passing in the normalizedOptions, which always returns an object according to lib/normalizeOptions.js

* @see https://github.com/cdimascio/express-openapi-validator#asyncawait
*/
const add = app => {
const add = (app, options) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const add = (app, options) => {
const add = (app, { propagatedError }) => {

I'm not sure if this can make the code more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. However, if we keep adding more options, it could be a bit cumbersome

@@ -3,14 +3,19 @@ const debug = require('debug')('swagger-endpoint-validator:custom-error-handler'
/**
* Adds a custom error handler for the errors thrown by the validator.
* @param {Object} app - Express application object.
* @param {Object} options - Configuration options.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I'm not sure if adding here the only field expected in this object is better for readibility or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm following the same convention as in lib/normalizeOptions.js. It's true that there more options are used. However, if we plan to keep adding new options, it could become a bit difficult to maintain. Anyway, all the options are documented in index.js

Copy link

@Betisman Betisman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just added a couple of questions, but for me the changes are good to go.

@kevinccbsg
Copy link
Contributor

@neodmy LGTM

@@ -0,0 +1,37 @@
const request = require('supertest');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Member

@inigomarquinez inigomarquinez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Thanks for your contribution, @neodmy !

@inigomarquinez
Copy link
Member

@all-contributors please add @neodmy for code, test

@allcontributors
Copy link
Contributor

@inigomarquinez

I've put up a pull request to add @neodmy! 🎉

@inigomarquinez inigomarquinez merged commit 4d9ad76 into onebeyond:master Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants