From 0b7ab075b7475d572a2bb40e53610c0ab2d776c6 Mon Sep 17 00:00:00 2001 From: dario-piotrowicz Date: Fri, 3 Jun 2022 11:18:04 +0100 Subject: [PATCH] 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 --- packages/core/src/errors.ts | 7 +++++-- packages/core/test/runtime_error_spec.ts | 26 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/core/src/errors.ts b/packages/core/src/errors.ts index c2b45cb1532..0eb1f3737c5 100644 --- a/packages/core/src/errors.ts +++ b/packages/core/src/errors.ts @@ -103,10 +103,13 @@ export function formatRuntimeError( // 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; } diff --git a/packages/core/test/runtime_error_spec.ts b/packages/core/test/runtime_error_spec.ts index 75eda22aa52..c7360b7260a 100644 --- a/packages/core/test/runtime_error_spec.ts +++ b/packages/core/test/runtime_error_spec.ts @@ -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.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.PIPE_NOT_FOUND, errorMessage); + expect(error.toString()) + .toBe(`Error: NG0302: Pipe not found? Find more at https://angular.io/errors/NG0302`); + }); });