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

improve(validation): add location to error messages #4145

Open
wants to merge 1 commit into
base: 16.x.x
Choose a base branch
from
Open
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
26 changes: 24 additions & 2 deletions src/validation/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,28 @@
return errors;
}

/**
* Combine multiple errors into a single error, whose message
* contains the error messages and their line:column on separate lines.
*/
function combineErrorsWithLocation(errors: ReadonlyArray<GraphQLError>): Error {
const errorMessageWithLocations = errors
.map((error) => {
if (!error.locations?.length) {
return error.message;
}

Check warning on line 119 in src/validation/validate.ts

View check run for this annotation

Codecov / codecov/patch

src/validation/validate.ts#L118-L119

Added lines #L118 - L119 were not covered by tests

const locations =
error.locations
.map(({ line, column }) => `${line}:${column}`)
.join(', ') ?? '';

Check warning on line 124 in src/validation/validate.ts

View check run for this annotation

Codecov / codecov/patch

src/validation/validate.ts#L124

Added line #L124 was not covered by tests
return `${error.message} (${locations})`;
})
.join('\n\n');

return new Error(errorMessageWithLocations);
}

/**
* Utility function which asserts a SDL document is valid by throwing an error
* if it is invalid.
Expand All @@ -116,7 +138,7 @@
export function assertValidSDL(documentAST: DocumentNode): void {
const errors = validateSDL(documentAST);
if (errors.length !== 0) {
throw new Error(errors.map((error) => error.message).join('\n\n'));
throw combineErrorsWithLocation(errors);
}
}

Expand All @@ -132,6 +154,6 @@
): void {
const errors = validateSDL(documentAST, schema);
if (errors.length !== 0) {
throw new Error(errors.map((error) => error.message).join('\n\n'));
throw combineErrorsWithLocation(errors);
}
}
Loading