refactor(core): improve the runtime errors formatting (#46239)

the formatted error messages always include a period separator between the
provided error message and the find-more suffix, this is not always
desirable as it may add periods when they shoud not be, so improve the
formatting by checking and applying the period only if the provided message
doesn't end with a character which already represents a separator

additionally also improve the formatting by trimming the provided error
message

note that such trimming needs to be performed before the separator check
so that for example an error message like `"some error! "` doesn't produce
`"some error! ."` but it successfully produces "some error!"

PR Close #46239
This commit is contained in:
dario-piotrowicz
2022-06-03 11:18:04 +01:00
committed by Alex Rickabaugh
parent f12cf2b89b
commit 0b7ab075b7
2 changed files with 31 additions and 2 deletions
+5 -2
View File
@@ -103,10 +103,13 @@ export function formatRuntimeError<T extends number = RuntimeErrorCode>(
// generate a link to the error details page on angular.io.
const fullCode = `NG0${Math.abs(code)}`;
let errorMessage = `${fullCode}${message ? ': ' + message : ''}`;
let errorMessage = `${fullCode}${message ? ': ' + message.trim() : ''}`;
if (ngDevMode && code < 0) {
errorMessage = `${errorMessage}. Find more at ${ERROR_DETAILS_PAGE_BASE_URL}/${fullCode}`;
const addPeriodSeparator = !errorMessage.match(/[.,;!?]$/);
const separator = addPeriodSeparator ? '.' : '';
errorMessage =
`${errorMessage}${separator} Find more at ${ERROR_DETAILS_PAGE_BASE_URL}/${fullCode}`;
}
return errorMessage;
}
+26
View File
@@ -29,4 +29,30 @@ describe('RuntimeError utils', () => {
expect(errorInstance.toString())
.toBe('Error: NG0301: Some error message. Find more at https://angular.io/errors/NG0301');
});
it('should trim the provided error message', () => {
const error = new RuntimeError(RuntimeErrorCode.EXPORT_NOT_FOUND, ' Some error message ');
expect(error.toString())
.toBe('Error: NG0301: Some error message. Find more at https://angular.io/errors/NG0301');
});
['.', ',', ';', '!', '?'].forEach(
character => it(
`should not add a period between the error message and the aio guide suffix if the message ends with '${
character}'`,
() => {
const errorMessage = `Pipe not found${character}`;
const error =
new RuntimeError<RuntimeErrorCode>(RuntimeErrorCode.PIPE_NOT_FOUND, errorMessage);
expect(error.toString())
.toBe(`Error: NG0302: Pipe not found${
character} Find more at https://angular.io/errors/NG0302`);
}));
it('should trim the error message before applying the period check', () => {
const errorMessage = `Pipe not found? `;
const error = new RuntimeError<RuntimeErrorCode>(RuntimeErrorCode.PIPE_NOT_FOUND, errorMessage);
expect(error.toString())
.toBe(`Error: NG0302: Pipe not found? Find more at https://angular.io/errors/NG0302`);
});
});