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

fix(core): Map NodeOperationErrors to BadRequestError in /resource-locator-results #9073

Conversation

tomi
Copy link
Contributor

@tomi tomi commented Apr 5, 2024

Summary

While investigating https://linear.app/n8n/issue/ADO-1361/dynamic-options-fail-to-load-if-the-node-contains-an-expression-in-an I noticed this:

UI calls the /dynamic-node-parameters/resource-locator-results endpoint to resolve resources to show in the NDV resource picker. For example the user can select an n8n workflow from a list in the n8n node. However, if the credentials are invalid or missing, the resolution will throw a NodeOperationError. This results in HTTP 500 response from the endpoint. Since this is an expected error due to invalid configuration, it makes more sense to respond with 400 Bad Request.

Related tickets and issues

https://linear.app/n8n/issue/ADO-1361/dynamic-options-fail-to-load-if-the-node-contains-an-expression-in-an

Review / Merge checklist

  • PR title and summary are descriptive. Remember, the title automatically goes into the changelog. Use (no-changelog) otherwise. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.

    A bug is not considered fixed, unless a test is added to prevent it from happening again.
    A feature is not complete without tests.

…cator-results

UI calls the /dynamic-node-parameters/resource-locator-results endpoint to resolve
resources to show in the NDV resource picker. For example the user can select an
n8n workflow from a list in the n8n node. However, if the credentials are invalid or
missing, the resolution will throw a NodeOperationError. This results in HTTP 500
response from the endpoint. Since this is an expected error due to invalid
configuration, it makes more sense to respond with 400 Bad Request.
@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Apr 5, 2024
Comment on lines +97 to +99
if (error instanceof NodeOperationError) {
throw new BadRequestError(error.message);
}
Copy link
Contributor

@ivov ivov Apr 5, 2024

Choose a reason for hiding this comment

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

Can this throw a NodeOperationError unrelated to invalid credentials? Is it safe to assume this mapping is always true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That seems to depend on the Node that is being used. In case of the n8n-nodes-base.n8n, it ends up in NodeExecuteFunctions.getCredentials, which does throw a NodeOperationError if the creds have not been defined. What's the convention of using errors in the nodes code? Do you think we should instead inherit a new Error class from NodeOperationError for credentials and catch only that?

Copy link
Contributor

Choose a reason for hiding this comment

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

Tbh I don't know what the conventions are either - @michael-radency what would you suggest?

Copy link
Contributor

Choose a reason for hiding this comment

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

for nodes if error comes from external api request it's in most cases would be NodeApiError, if error happened during operation, like invalid json parse, it would be NodeOperationError

so in case of invalid credentials it probably should be NodeApiError, but there are exceptions

not sure why we need to add a new error class as BadRequestError is a specialized wrapper to throw error with status code 400

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So would it make sense then to change the NodeExecuteFunctions.getCredentials to throw NodeApiError instead? Even tho the error is not coming from an external API. I wouldn't change it to BadRequestError as that wouldn't make sense conceptually in a function like getCredentials.

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe better to add errorCode optional parameter to NodeOperationErrorOptions that would set error code on NodeOperationError

@tomi
Copy link
Contributor Author

tomi commented Apr 29, 2024

I'm gonna close this since this is not a must have and the implications of this change are not clear

@tomi tomi closed this Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants