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

How to return array of errors in one response? #3247

Closed
kunhuangau opened this issue Sep 4, 2019 · 10 comments
Closed

How to return array of errors in one response? #3247

kunhuangau opened this issue Sep 4, 2019 · 10 comments
Labels
🍐 error-handling Pertaining to error handling (or lack thereof), not just for just general errors/bugs.

Comments

@kunhuangau
Copy link

The response payload could contains an array of errors. Just wondering how can I simulate to return more than one errors in one response?

I'm using apollo-server in lambda and tried throw errors in two different resolvers, I added some debug console.log, those two resolvers have been executed, but one get one error in the response.

Thanks!

@abernix abernix added the 🍐 error-handling Pertaining to error handling (or lack thereof), not just for just general errors/bugs. label Dec 31, 2020
@gastonmorixe
Copy link

+1
I have been unable to return an array of errors for custom validations. I tried throwing an Error with an array of errors property as this SO answer but it doesn't seem to work for me.

@gastonmorixe
Copy link

gastonmorixe commented Mar 17, 2021

Well it doesn't seem to be possible. After this commit error flattening was removed. 1947892

Today's code these lines forces to map from the original error array, therefore I can't think any work-around to flatten the final array from the formatError callback. Maybe there's a way to do this in a plugin? I haven't found a way yet.

return enrichedErrors.map(error => {
try {
return makePrintable(formatter(error));

@gastonmorixe
Copy link

I'd like to understand what's the point of returning an error array if it's always forced to return one element.

@glasser
Copy link
Member

glasser commented Mar 17, 2021

@gastonmorixe In what context do you mean "return an error array"?

@gastonmorixe
Copy link

gastonmorixe commented Mar 17, 2021

@glasser I want to return a flat array of ValidationError. Imagine a form with N validations and Y fields failing, IMO it should simply return errors: [ {err1} , {err2} ] not errors: [ [ {err1} , {err2} ] ]

isn't that nested array unnecessary?
what's the reason to completely forbid it from returning a custom "errors" payload from a custom formatter? or there is a way to do it that I missed?

Thank you

@glasser
Copy link
Member

glasser commented Mar 17, 2021

Sorry, to clarify, my question is the following. Is your concern:

  • (a) Apollo Server encourages me to throw/return an array of errors in some context (of which @glasser is unaware) but it doesn't really work well (ie, a "bug report")
  • (b) I want to return multiple errors from a single resolver and Apollo Server doesn't even try to let me do this (ie, a "feature request")

(And I will reiterate that in the apps I've worked on, when we need a lot of complexity around errors like the ability to return lots of validations failing to be rendered in the UI, I've been happier making that an actual part of the field's return value rather than part of the out-of-band error functionality.)

@glasser
Copy link
Member

glasser commented Mar 17, 2021

(I think if we want to do this, the best approach would be to offer a specific MultipleErrorsError or something that you can throw and that gets flattened, rather than doing some sort of magic auto flattening.)

@gastonmorixe
Copy link

Hey @glasser thank you for your detailed and prompt response.

I'd say option b. While we may agree or disagree on how errors should be handled, if the official GQL spec doesn't specify a 1-1 match between the index of the errors and the resolvers then I don't agree Apollo Server should force the devs to a nested array. At least there should be a way to opt out. I thought that was what formatError was for but it's even too late for that to help as explained above.

Thank you

@glasser
Copy link
Member

glasser commented Mar 19, 2021

I mean, I think the concern is that the mechanism we've used for expressing "resolving this path has an error" is "the resolver throws an error", and a resolver can't throw more than one error.

I'd be happy to review a PR that adds an explicit way of indicating "this resolver is failing with multiple errors"; I think something like throw multipleErrors(e1, e2, e3) could be a reasonable way to do that (with multipleErrors as a function, or maybe a particular error class).

@glasser
Copy link
Member

glasser commented Oct 20, 2022

This actually seems like it's more appropriate for https://github.com/graphql/graphql-js than Apollo Server. There's an open issue in that repository about this: graphql/graphql-js#205

@glasser glasser closed this as not planned Won't fix, can't repro, duplicate, stale Oct 20, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🍐 error-handling Pertaining to error handling (or lack thereof), not just for just general errors/bugs.
Projects
None yet
Development

No branches or pull requests

4 participants