nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: graphql/graphql-js
Pull request #3231 (2021-08-11 19:44:40)

execute: Forbid to return `null` from `serialize` function
Fixes #1579


Patch:
diff --git a/src/execution/execute.ts b/src/execution/execute.ts
index f092a6a7..8429cae8 100644
--- a/src/execution/execute.ts
+++ b/src/execution/execute.ts
@@ -750,10 +750,10 @@ function completeLeafValue(
   result: unknown,
 ): unknown {
   const serializedResult = returnType.serialize(result);
-  if (serializedResult === undefined) {
+  if (serializedResult == null) {
     throw new Error(
-      `Expected a value of type "${inspect(returnType)}" but ` +
-        `received: ${inspect(result)}`,
+      `Expected \`${inspect(returnType)}.serialize(${inspect(result)})\` to ` +
+        `return non-nullable value, returned: ${inspect(serializedResult)}`,
     );
   }
   return serializedResult;


Test patch:
diff --git a/src/execution/__tests__/executor-test.ts b/src/execution/__tests__/executor-test.ts
index 5283aa4d..330f719d 100644
--- a/src/execution/__tests__/executor-test.ts
+++ b/src/execution/__tests__/executor-test.ts
@@ -1123,7 +1123,7 @@ describe('Execute: Handles basic execution tasks', () => {
       errors: [
         {
           message:
-            'Expected a value of type "CustomScalar" but received: "CUSTOM_VALUE"',
+            'Expected `CustomScalar.serialize("CUSTOM_VALUE")` to return non-nullable value, returned: undefined',
           locations: [{ line: 1, column: 3 }],
           path: ['customScalar'],
         },
Repository: graphql/graphql-js
Pull request #3384 (2021-11-26 14:15:41)

Preserve non-error values thrown from resolvers
Fixes #3379

IvanGoncharov: Need to add some basic tests.

Patch:
diff --git a/src/error/locatedError.ts b/src/error/locatedError.ts
index fb5df58e..2fec3204 100644
--- a/src/error/locatedError.ts
+++ b/src/error/locatedError.ts
@@ -1,5 +1,5 @@
-import { inspect } from '../jsutils/inspect';
 import type { Maybe } from '../jsutils/Maybe';
+import { toError } from '../jsutils/toError';
 
 import type { ASTNode } from '../language/ast';
 
@@ -15,11 +15,7 @@ export function locatedError(
   nodes: ASTNode | ReadonlyArray<ASTNode> | undefined | null,
   path?: Maybe<ReadonlyArray<string | number>>,
 ): GraphQLError {
-  // Sometimes a non-error is thrown, wrap it as an Error instance to ensure a consistent Error interface.
-  const originalError: Error | GraphQLError =
-    rawOriginalError instanceof Error
-      ? rawOriginalError
-      : new Error('Unexpected error value: ' + inspect(rawOriginalError));
+  const originalError = toError(rawOriginalError);
 
   // Note: this uses a brand-check to support GraphQL errors originating from other contexts.
   if (isLocatedGraphQLError(originalError)) {
diff --git a/src/jsutils/toError.ts b/src/jsutils/toError.ts
new file mode 100644
index 00000000..8d562273
--- /dev/null
+++ b/src/jsutils/toError.ts
@@ -0,0 +1,20 @@
+import { inspect } from './inspect';
+
+/**
+ * Sometimes a non-error is thrown, wrap it as an Error instance to ensure a consistent Error interface.
+ */
+export function toError(thrownValue: unknown): Error {
+  return thrownValue instanceof Error
+    ? thrownValue
+    : new NonErrorThrown(thrownValue);
+}
+
+class NonErrorThrown extends Error {
+  thrownValue: unknown;
+
+  constructor(thrownValue: unknown) {
+    super('Unexpected error value: ' + inspect(thrownValue));
+    this.name = 'NonErrorThrown';
+    this.thrownValue = thrownValue;
+  }
+}


Test patch:
diff --git a/src/error/__tests__/locatedError-test.ts b/src/error/__tests__/locatedError-test.ts
index 21e858e9..2e35723a 100644
--- a/src/error/__tests__/locatedError-test.ts
+++ b/src/error/__tests__/locatedError-test.ts
@@ -16,6 +16,17 @@ describe('locatedError', () => {
     expect(locatedError(e, [], [])).to.deep.equal(e);
   });
 
+  it('wraps non-errors', () => {
+    const testObject = Object.freeze({});
+    const error = locatedError(testObject, [], []);
+
+    expect(error).to.be.instanceOf(GraphQLError);
+    expect(error.originalError).to.include({
+      name: 'NonErrorThrown',
+      thrownValue: testObject,
+    });
+  });
+
   it('passes GraphQLError-ish through', () => {
     const e = new Error();
     // @ts-expect-error