dotansimha / graphql-yoga

🧘 Rewrite of a fully-featured GraphQL Server with focus on easy setup, performance & great developer experience. The core of Yoga implements WHATWG Fetch API and can run/deploy on any JS environment.
https://the-guild.dev/graphql/yoga-server
MIT License
8.22k stars 568 forks source link

GraphQLError does not respect originalError #3356

Open schettn opened 3 months ago

schettn commented 3 months ago

Describe the bug

When attempting to throw a GraphQLError object with an original error (such as an exception or an error object from another part of the system), the original error is not included in the resulting GraphQL response. Instead, the original error details get swallowed or ignored, resulting in a response that lacks critical debugging information.

Your Example Website or App

.

Steps to Reproduce the Bug or Issue

Use this inside a resolver:

const { GraphQLError } = require('graphql');

try {
  // Some code that throws an error
  throw new Error('Original error message');
} catch (originalError) {
  // Attempt to initialize a GraphQLError with the original error
  const graphQLError = new GraphQLError('A GraphQL error occurred', {
    originalError,
  });
}

Expected behavior

When a GraphQLError is created with an original error, the details of the original error (such as the message, stack trace, and other relevant properties) should be accessible in the GraphQL response. This inclusion helps with debugging and understanding the root cause of the error.

Screenshots or Videos

No response

Platform

Additional context

No response

ardatan commented 3 months ago

Error Masking in Yoga hides errors if they are not instance of GraphQLError. https://the-guild.dev/graphql/yoga-server/docs/features/error-masking If you want to see the original error, you should either disable this behavior or use GraphQLError as original error.

Also if you want to see original errors only for development purposes, see here; https://the-guild.dev/graphql/yoga-server/docs/features/error-masking#receive-original-error-in-development

schettn commented 3 months ago

I am aware of error masking.

The problem is that when throwing a GraphQLError with the originalError prop there is no originalError in the extensions of the response.

throw new GraphQLError(“Foo”, {
originalError: new Error(“Bar”)
})

But when I throw a regular error there is.

schettn commented 3 months ago

“If you want to see the original error, you should either disable this behavior or use GraphQLError as original error.”

This is not possible since for example libraries do not throw GraphQL errors.

i want to catch those errors and throw a GraphQLError with the added originalError.

“But when I throw a regular error there is.“. I am able to receive the originalError when I throw an Error; just not when I throw a GraphQLError /w originalError as described above.

neviaumi commented 4 hours ago

You may try use createGraphQLError export from graphql-yoga

See below

import { createGraphQLError } from 'graphql-yoga';
try {
  // Some code that throws an error
  throw new Error('Original error message');
} catch (originalError) {
  // Attempt to initialize a GraphQLError with the original error
  const graphQLError = createGraphQLError('A GraphQL error occurred', {
    originalError,
  });
}

The thing is https://github.com/dotansimha/graphql-yoga/blob/9efdc3831a517890dbb00c783d6da86c6a6d9b4e/packages/graphql-yoga/src/utils/mask-error.ts#L11 this line was never return ture if you create the GraphQLError on your code.

I think it may some bug on Yoga side, but solution above can play as workaround

schettn commented 4 hours ago

@neviaumi Thanks I will try it!