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

Deprecate 'printError' function #3252

Merged
merged 1 commit into from Aug 30, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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