Skip to content

Commit

Permalink
Deprecate 'printError' function (#3252)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Aug 30, 2021
1 parent 16b3d04 commit da685ee
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Expand Up @@ -627,6 +627,7 @@ overrides:
rules:
internal-rules/require-to-string-tag: off
node/no-unpublished-import: [error, { allowModules: ['chai', 'mocha'] }]
import/no-deprecated: off
import/no-restricted-paths: off
import/no-extraneous-dependencies: [error, { devDependencies: true }]
- files: 'integrationTests/*'
Expand Down
3 changes: 3 additions & 0 deletions resources/gen-changelog.js
Expand Up @@ -12,6 +12,9 @@ const labelsConfig = {
'PR: breaking change 💥': {
section: 'Breaking Change 💥',
},
'PR: deprecation ⚠': {
section: 'Deprecation ⚠',
},
'PR: feature 🚀': {
section: 'New Feature 🚀',
},
Expand Down
34 changes: 18 additions & 16 deletions src/error/GraphQLError.ts
Expand Up @@ -198,7 +198,21 @@ export class GraphQLError extends Error {
}

toString(): string {
return printError(this);
let output = this.message;

if (this.nodes) {
for (const node of this.nodes) {
if (node.loc) {
output += '\n\n' + printLocation(node.loc);
}
}
} else if (this.source && this.locations) {
for (const location of this.locations) {
output += '\n\n' + printSourceLocation(this.source, location);
}
}

return output;
}

// FIXME: workaround to not break chai comparisons, should be remove in v16
Expand All @@ -210,21 +224,9 @@ export class GraphQLError extends Error {
/**
* Prints a GraphQLError to a string, representing useful location information
* about the error's position in the source.
*
* @deprecated Please use `error.toString` instead. Will be removed in v17
*/
export function printError(error: GraphQLError): string {
let output = error.message;

if (error.nodes) {
for (const node of error.nodes) {
if (node.loc) {
output += '\n\n' + printLocation(node.loc);
}
}
} else if (error.source && error.locations) {
for (const location of error.locations) {
output += '\n\n' + printSourceLocation(error.source, location);
}
}

return output;
return error.toString();
}
13 changes: 9 additions & 4 deletions src/error/__tests__/GraphQLError-test.ts
Expand Up @@ -132,18 +132,23 @@ describe('GraphQLError', () => {
});
});

describe('printError', () => {
describe('toString', () => {
it('Deprecated: prints an error using printError', () => {
const error = new GraphQLError('Error');
expect(printError(error)).to.equal('Error');
});

it('prints an error without location', () => {
const error = new GraphQLError('Error without location');
expect(printError(error)).to.equal('Error without location');
expect(error.toString()).to.equal('Error without location');
});

it('prints an error using node without location', () => {
const error = new GraphQLError(
'Error attached to node without location',
parse('{ foo }', { noLocation: true }),
);
expect(printError(error)).to.equal(
expect(error.toString()).to.equal(
'Error attached to node without location',
);
});
Expand Down Expand Up @@ -182,7 +187,7 @@ describe('printError', () => {
fieldB.type,
]);

expect(printError(error)).to.equal(dedent`
expect(error.toString()).to.equal(dedent`
Example error with two nodes
SourceA:2:10
Expand Down

0 comments on commit da685ee

Please sign in to comment.