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`); + }); });