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

Handle undefined path in getErrorsFromParent #667

Merged
merged 4 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/stitching/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ if (
ERROR_SYMBOL = '@@__subSchemaErrors';
}

export const ErrorSymbol = ERROR_SYMBOL;

export function annotateWithChildrenErrors(
object: any,
childrenErrors: Array<{ path?: Array<string | number> }>,
Expand Down Expand Up @@ -62,7 +64,7 @@ export function getErrorsFromParent(
const errors = (object && object[ERROR_SYMBOL]) || [];
const childrenErrors: Array<{ path?: Array<string | number> }> = [];
for (const error of errors) {
if (error.path.length === 1 && error.path[0] === fieldName) {
if ((!error.path) || (error.path.length === 1 && error.path[0] === fieldName)) {
return {
kind: 'OWN',
error,
Expand Down
22 changes: 21 additions & 1 deletion src/test/testErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
GraphQLResolveInfo
} from 'graphql';
import {
checkResultAndHandleErrors
checkResultAndHandleErrors,
getErrorsFromParent,
ErrorSymbol,
} from '../stitching/errors';

import 'mocha';
Expand All @@ -17,7 +19,25 @@ class ErrorWithResult extends Error {
}
}

const mockErrors = {
responseKey: '',
[ErrorSymbol]: [
{
message: 'Test error without path',
},
],
};

describe('Errors', () => {
describe('getErrorsFromParent', () => {
it('should return OWN error kind if path is not defined', () => {
assert.deepEqual(
getErrorsFromParent(mockErrors, 'responseKey'),
{ kind: 'OWN', error: mockErrors[ErrorSymbol][0] },
);
});
});

describe('checkResultAndHandleErrors', () => {
it('persists single error with a result', done => {
const result = {
Expand Down