Skip to content

Commit

Permalink
Add path to formatError
Browse files Browse the repository at this point in the history
While path was added to the serializable error, it was not yet added to the default formatError function.

Adding this is technically breaking since it begins to expose new information from your API.
  • Loading branch information
leebyron committed Nov 9, 2016
1 parent 95c1bc2 commit 3e069f5
Show file tree
Hide file tree
Showing 29 changed files with 211 additions and 86 deletions.
18 changes: 17 additions & 1 deletion src/error/__tests__/GraphQLError-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { parse, Source, GraphQLError } from '../../';
import { parse, Source, GraphQLError, formatError } from '../../';


describe('GraphQLError', () => {
Expand Down Expand Up @@ -123,4 +123,20 @@ describe('GraphQLError', () => {
);
});

it('default error formatter includes path', () => {
const e = new GraphQLError(
'msg',
null,
null,
null,
[ 'path', 3, 'to', 'field' ]
);

expect(formatError(e)).to.deep.equal({
message: 'msg',
locations: undefined,
path: [ 'path', 3, 'to', 'field' ]
});
});

});
6 changes: 4 additions & 2 deletions src/error/formatError.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ export function formatError(error: GraphQLError): GraphQLFormattedError {
invariant(error, 'Received null or undefined error.');
return {
message: error.message,
locations: error.locations
locations: error.locations,
path: error.path
};
}

export type GraphQLFormattedError = {
message: string,
locations: ?Array<GraphQLErrorLocation>
locations: ?Array<GraphQLErrorLocation>,
path: ?Array<string | number>
};

export type GraphQLErrorLocation = {
Expand Down
33 changes: 22 additions & 11 deletions src/execution/__tests__/executor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,27 +403,38 @@ describe('Execute: Handles basic execution tasks', () => {

expect(result.errors && result.errors.map(formatError)).to.deep.equal([
{ message: 'Error getting syncError',
locations: [ { line: 3, column: 7 } ] },
locations: [ { line: 3, column: 7 } ],
path: [ 'syncError' ] },
{ message: 'Error getting syncRawError',
locations: [ { line: 4, column: 7 } ] },
locations: [ { line: 4, column: 7 } ],
path: [ 'syncRawError' ] },
{ message: 'Error getting syncReturnError',
locations: [ { line: 5, column: 7 } ] },
locations: [ { line: 5, column: 7 } ],
path: [ 'syncReturnError' ] },
{ message: 'Error getting syncReturnErrorList1',
locations: [ { line: 6, column: 7 } ] },
locations: [ { line: 6, column: 7 } ],
path: [ 'syncReturnErrorList', 1 ] },
{ message: 'Error getting syncReturnErrorList3',
locations: [ { line: 6, column: 7 } ] },
locations: [ { line: 6, column: 7 } ],
path: [ 'syncReturnErrorList', 3 ] },
{ message: 'Error getting asyncReject',
locations: [ { line: 8, column: 7 } ] },
locations: [ { line: 8, column: 7 } ],
path: [ 'asyncReject' ] },
{ message: 'Error getting asyncRawReject',
locations: [ { line: 9, column: 7 } ] },
locations: [ { line: 9, column: 7 } ],
path: [ 'asyncRawReject' ] },
{ message: 'An unknown error occurred.',
locations: [ { line: 10, column: 7 } ] },
locations: [ { line: 10, column: 7 } ],
path: [ 'asyncEmptyReject' ] },
{ message: 'Error getting asyncError',
locations: [ { line: 11, column: 7 } ] },
locations: [ { line: 11, column: 7 } ],
path: [ 'asyncError' ] },
{ message: 'Error getting asyncRawError',
locations: [ { line: 12, column: 7 } ] },
locations: [ { line: 12, column: 7 } ],
path: [ 'asyncRawError' ] },
{ message: 'Error getting asyncReturnError',
locations: [ { line: 13, column: 7 } ] },
locations: [ { line: 13, column: 7 } ],
path: [ 'asyncReturnError' ] },
]);
});

Expand Down
57 changes: 38 additions & 19 deletions src/execution/__tests__/lists-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ describe('Execute: Accepts any iterable as list value', () => {
{ data: { nest: { test: null } },
errors: [ {
message: 'Expected Iterable, but did not find one for field DataType.test.',
locations: [ { line: 1, column: 10 } ]
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test' ]
} ] }
));

Expand Down Expand Up @@ -157,7 +158,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: { test: null } },
errors: [
{ message: 'bad',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test' ] }
] }
));

Expand All @@ -180,7 +182,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: { test: [ 1, null, 2 ] } },
errors: [
{ message: 'bad',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test', 1 ] }
] }
));

Expand Down Expand Up @@ -208,7 +211,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: null },
errors: [
{ message: 'Cannot return null for non-nullable field DataType.test.',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test' ] }
] }
));

Expand All @@ -231,7 +235,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: null },
errors: [
{ message: 'Cannot return null for non-nullable field DataType.test.',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test' ] }
] }
));

Expand All @@ -240,7 +245,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: null },
errors: [
{ message: 'bad',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test' ] }
] }
));

Expand All @@ -263,7 +269,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: { test: [ 1, null, 2 ] } },
errors: [
{ message: 'bad',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test', 1 ] }
] }
));

Expand All @@ -286,7 +293,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: { test: null } },
errors: [
{ message: 'Cannot return null for non-nullable field DataType.test.',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test', 1 ] }
] }
));

Expand All @@ -309,7 +317,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: { test: null } },
errors: [
{ message: 'Cannot return null for non-nullable field DataType.test.',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test', 1 ] }
] }
));

Expand All @@ -323,7 +332,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: { test: null } },
errors: [
{ message: 'bad',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test' ] }
] }
));

Expand All @@ -341,7 +351,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: { test: null } },
errors: [
{ message: 'Cannot return null for non-nullable field DataType.test.',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test', 1 ] }
] }
));

Expand All @@ -350,7 +361,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: { test: null } },
errors: [
{ message: 'bad',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test', 1 ] }
] }
));

Expand All @@ -375,7 +387,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: null },
errors: [
{ message: 'Cannot return null for non-nullable field DataType.test.',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test', 1 ] }
] }
));

Expand All @@ -384,7 +397,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: null },
errors: [
{ message: 'Cannot return null for non-nullable field DataType.test.',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test' ] }
] }
));

Expand All @@ -402,7 +416,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: null },
errors: [
{ message: 'Cannot return null for non-nullable field DataType.test.',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test', 1 ] }
] }
));

Expand All @@ -411,7 +426,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: null },
errors: [
{ message: 'Cannot return null for non-nullable field DataType.test.',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test' ] }
] }
));

Expand All @@ -420,7 +436,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: null },
errors: [
{ message: 'bad',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test' ] }
] }
));

Expand All @@ -438,7 +455,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: null },
errors: [
{ message: 'Cannot return null for non-nullable field DataType.test.',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test', 1 ] }
] }
));

Expand All @@ -447,7 +465,8 @@ describe('Execute: Handles list nullability', () => {
{ data: { nest: null },
errors: [
{ message: 'bad',
locations: [ { line: 1, column: 10 } ] }
locations: [ { line: 1, column: 10 } ],
path: [ 'nest', 'test', 1 ] }
] }
));

Expand Down
1 change: 1 addition & 0 deletions src/validation/__tests__/ArgumentsOfCorrectType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function badValue(argName, typeName, value, line, column, errors) {
return {
message: badValueMessage(argName, typeName, value, realErrors),
locations: [ { line, column } ],
path: undefined,
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/validation/__tests__/DefaultValuesOfCorrectType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function defaultForNonNullArg(varName, typeName, guessTypeName, line, column) {
return {
message: defaultForNonNullArgMessage(varName, typeName, guessTypeName),
locations: [ { line, column } ],
path: undefined,
};
}

Expand All @@ -35,6 +36,7 @@ function badValue(varName, typeName, val, line, column, errors) {
return {
message: badValueForDefaultArgMessage(varName, typeName, val, realErrors),
locations: [ { line, column } ],
path: undefined,
};
}

Expand Down
1 change: 1 addition & 0 deletions src/validation/__tests__/FieldsOnCorrectType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function undefinedField(
suggestedFields
),
locations: [ { line, column } ],
path: undefined,
};
}

Expand Down
4 changes: 3 additions & 1 deletion src/validation/__tests__/FragmentsOnCompositeTypes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function error(fragName, typeName, line, column) {
return {
message: fragmentOnNonCompositeErrorMessage(fragName, typeName),
locations: [ { line, column } ],
path: undefined,
};
}

Expand Down Expand Up @@ -101,7 +102,8 @@ describe('Validate: Fragments on composite types', () => {
}
`, [
{ message: inlineFragmentOnNonCompositeErrorMessage('String'),
locations: [ { line: 3, column: 16 } ] }
locations: [ { line: 3, column: 16 } ],
path: undefined }
]);
});

Expand Down
2 changes: 2 additions & 0 deletions src/validation/__tests__/KnownArgumentNames-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function unknownArg(argName, fieldName, typeName, suggestedArgs, line, column) {
return {
message: unknownArgMessage(argName, fieldName, typeName, suggestedArgs),
locations: [ { line, column } ],
path: undefined,
};
}

Expand All @@ -33,6 +34,7 @@ function unknownDirectiveArg(
return {
message: unknownDirectiveArgMessage(argName, directiveName, suggestedArgs),
locations: [ { line, column } ],
path: undefined,
};
}

Expand Down
6 changes: 4 additions & 2 deletions src/validation/__tests__/KnownDirectives-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ import {
function unknownDirective(directiveName, line, column) {
return {
message: unknownDirectiveMessage(directiveName),
locations: [ { line, column } ]
locations: [ { line, column } ],
path: undefined,
};
}

function misplacedDirective(directiveName, placement, line, column) {
return {
message: misplacedDirectiveMessage(directiveName, placement),
locations: [ { line, column } ]
locations: [ { line, column } ],
path: undefined,
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/validation/__tests__/KnownFragmentNames-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
function undefFrag(fragName, line, column) {
return {
message: unknownFragmentMessage(fragName),
locations: [ { line, column } ]
locations: [ { line, column } ],
path: undefined,
};
}

Expand Down
1 change: 1 addition & 0 deletions src/validation/__tests__/KnownTypeNames-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function unknownType(typeName, suggestedTypes, line, column) {
return {
message: unknownTypeMessage(typeName, suggestedTypes),
locations: [ { line, column } ],
path: undefined,
};
}

Expand Down
1 change: 1 addition & 0 deletions src/validation/__tests__/LoneAnonymousOperation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function anonNotAlone(line, column) {
return {
message: anonOperationNotAloneMessage(),
locations: [ { line, column } ],
path: undefined,
};
}

Expand Down
Loading

0 comments on commit 3e069f5

Please sign in to comment.