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

Error types are inaccurate #476

Closed
knowlesc opened this issue Oct 2, 2024 · 4 comments
Closed

Error types are inaccurate #476

knowlesc opened this issue Oct 2, 2024 · 4 comments

Comments

@knowlesc
Copy link

knowlesc commented Oct 2, 2024

the GraphQLResponse type is defined like this:
image

However, if there's an internal node error, which we occasionally see ("Premature close"), the response looks like this:

{
      data: undefined,
      errors: [
        {
          name: "FetchError",
          message: "Invalid response body while trying to fetch https://app.useanvil.com/graphql: Premature close",
          code: "ERR_STREAM_PREMATURE_CLOSE",
          stack:
            "FetchError: Invalid response body while trying to fetch https://app.useanvil.com/graphql: Premature close",
        },
      ],
      statusCode: 200
    }

It would be great if we could rely on the error types to be able to safely parse errors.

Thank you!

@newhouse
Copy link
Contributor

newhouse commented Oct 2, 2024

Hello @knowlesc and thanks for writing in!

I'm not an expert in TypeScript, but doesn't that error you're showing adhere to the type ResponseError type definition?

  • message is the only key marked as required, and it is present, and is a string as specified.
  • name is not required, but it is a string as specified.
  • no other explicitly defined keys are present, and so are also not violated.
  • there are no required keys missing.
  • code and stack would fall into the "catch-all" of [key: string]: any;, would they not?

Am I wrong about this or was there something else you were expecting? It's hard to anticipate all the myriad errors that we may encounter, so I'm not sure that there's a realistic way to code for all of them. Let me know your thoughts, please.

@knowlesc
Copy link
Author

knowlesc commented Oct 3, 2024

Hi @newhouse - thanks for the quick response.

This behaviour is technically accurate, however it doesn't make it clear which keys will be included. An any in TypeScript doesn't really provide any info. Looking at the code, I can see a function to normalize node errors here: https://github.com/anvilco/node-anvil/blob/main/src/errors.js#L36-L47

When that executes, we have a set of clearly defined error keys (name, message, code, cause, stack). The response error type would be clearer if it was something like:

 * @property {Array<ResponseError | NodeError>} [errors]

where NodeError was defined as

/** @typedef {{
  message: string,
  code: number,
  cause: string,
  stack: string,
  name: string,
}} NodeError */

At least this way, consumers can understand that errors may come back in that format, and build their error handlers to specifically handle those types of errors, without having to dig into the library code.

Hopefully my description makes sense - let me know if you have any questions.

@newhouse newhouse mentioned this issue Oct 4, 2024
10 tasks
@newhouse
Copy link
Contributor

newhouse commented Oct 4, 2024

OK, that sounds good. I added this in 3.3.1. @knowlesc can you pull that down and try it?

@newhouse
Copy link
Contributor

newhouse commented Oct 7, 2024

Going to close this for now.

@newhouse newhouse closed this as completed Oct 7, 2024
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

No branches or pull requests

2 participants