Skip to content

Commit da685ee

Browse files
Deprecate 'printError' function (#3252)
1 parent 16b3d04 commit da685ee

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

.eslintrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ overrides:
627627
rules:
628628
internal-rules/require-to-string-tag: off
629629
node/no-unpublished-import: [error, { allowModules: ['chai', 'mocha'] }]
630+
import/no-deprecated: off
630631
import/no-restricted-paths: off
631632
import/no-extraneous-dependencies: [error, { devDependencies: true }]
632633
- files: 'integrationTests/*'

resources/gen-changelog.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const labelsConfig = {
1212
'PR: breaking change 💥': {
1313
section: 'Breaking Change 💥',
1414
},
15+
'PR: deprecation ⚠': {
16+
section: 'Deprecation ⚠',
17+
},
1518
'PR: feature 🚀': {
1619
section: 'New Feature 🚀',
1720
},

src/error/GraphQLError.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,21 @@ export class GraphQLError extends Error {
198198
}
199199

200200
toString(): string {
201-
return printError(this);
201+
let output = this.message;
202+
203+
if (this.nodes) {
204+
for (const node of this.nodes) {
205+
if (node.loc) {
206+
output += '\n\n' + printLocation(node.loc);
207+
}
208+
}
209+
} else if (this.source && this.locations) {
210+
for (const location of this.locations) {
211+
output += '\n\n' + printSourceLocation(this.source, location);
212+
}
213+
}
214+
215+
return output;
202216
}
203217

204218
// FIXME: workaround to not break chai comparisons, should be remove in v16
@@ -210,21 +224,9 @@ export class GraphQLError extends Error {
210224
/**
211225
* Prints a GraphQLError to a string, representing useful location information
212226
* about the error's position in the source.
227+
*
228+
* @deprecated Please use `error.toString` instead. Will be removed in v17
213229
*/
214230
export function printError(error: GraphQLError): string {
215-
let output = error.message;
216-
217-
if (error.nodes) {
218-
for (const node of error.nodes) {
219-
if (node.loc) {
220-
output += '\n\n' + printLocation(node.loc);
221-
}
222-
}
223-
} else if (error.source && error.locations) {
224-
for (const location of error.locations) {
225-
output += '\n\n' + printSourceLocation(error.source, location);
226-
}
227-
}
228-
229-
return output;
231+
return error.toString();
230232
}

src/error/__tests__/GraphQLError-test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,23 @@ describe('GraphQLError', () => {
132132
});
133133
});
134134

135-
describe('printError', () => {
135+
describe('toString', () => {
136+
it('Deprecated: prints an error using printError', () => {
137+
const error = new GraphQLError('Error');
138+
expect(printError(error)).to.equal('Error');
139+
});
140+
136141
it('prints an error without location', () => {
137142
const error = new GraphQLError('Error without location');
138-
expect(printError(error)).to.equal('Error without location');
143+
expect(error.toString()).to.equal('Error without location');
139144
});
140145

141146
it('prints an error using node without location', () => {
142147
const error = new GraphQLError(
143148
'Error attached to node without location',
144149
parse('{ foo }', { noLocation: true }),
145150
);
146-
expect(printError(error)).to.equal(
151+
expect(error.toString()).to.equal(
147152
'Error attached to node without location',
148153
);
149154
});
@@ -182,7 +187,7 @@ describe('printError', () => {
182187
fieldB.type,
183188
]);
184189

185-
expect(printError(error)).to.equal(dedent`
190+
expect(error.toString()).to.equal(dedent`
186191
Example error with two nodes
187192
188193
SourceA:2:10

0 commit comments

Comments
 (0)