diff --git a/packages/core/src/error_handler.ts b/packages/core/src/error_handler.ts index 6564e6782b..794d199b1d 100644 --- a/packages/core/src/error_handler.ts +++ b/packages/core/src/error_handler.ts @@ -7,6 +7,7 @@ */ import {getDebugContext, getErrorLogger, getOriginalError} from './errors'; +import {DebugContext} from './view/types'; @@ -58,17 +59,17 @@ export class ErrorHandler { } /** @internal */ - _findContext(error: any): any { + _findContext(error: any): DebugContext|null { return error ? (getDebugContext(error) || this._findContext(getOriginalError(error))) : null; } /** @internal */ - _findOriginalError(error: Error): any { - let e = getOriginalError(error); + _findOriginalError(error: any): Error|null { + let e = error && getOriginalError(error); while (e && getOriginalError(e)) { e = getOriginalError(e); } - return e; + return e || null; } } diff --git a/packages/core/src/errors.ts b/packages/core/src/errors.ts index 502a7f8cee..5d51a977a1 100644 --- a/packages/core/src/errors.ts +++ b/packages/core/src/errors.ts @@ -21,8 +21,8 @@ export function getOriginalError(error: Error): Error { return (error as any)[ERROR_ORIGINAL_ERROR]; } -export function getErrorLogger(error: Error): (console: Console, ...values: any[]) => void { - return (error as any)[ERROR_LOGGER] || defaultErrorLogger; +export function getErrorLogger(error: unknown): (console: Console, ...values: any[]) => void { + return error && (error as any)[ERROR_LOGGER] || defaultErrorLogger; } diff --git a/packages/core/test/error_handler_spec.ts b/packages/core/test/error_handler_spec.ts index b02e68f405..3b75a9ce65 100644 --- a/packages/core/test/error_handler_spec.ts +++ b/packages/core/test/error_handler_spec.ts @@ -23,7 +23,7 @@ function errorToString(error: any) { const errorHandler = new ErrorHandler(); (errorHandler as any)._console = logger as any; errorHandler.handleError(error); - return logger.res.map(line => line.join('#')).join('\n'); + return logger.res.map(line => line.map(x => `${x}`).join('#')).join('\n'); } describe('ErrorHandler', () => { @@ -32,6 +32,16 @@ describe('ErrorHandler', () => { expect(e).toContain('message!'); }); + it('should correctly handle primitive values', () => { + expect(errorToString('message')).toBe('ERROR#message'); + expect(errorToString(404)).toBe('ERROR#404'); + expect(errorToString(0)).toBe('ERROR#0'); + expect(errorToString(true)).toBe('ERROR#true'); + expect(errorToString(false)).toBe('ERROR#false'); + expect(errorToString(null)).toBe('ERROR#null'); + expect(errorToString(undefined)).toBe('ERROR#undefined'); + }); + describe('context', () => { it('should print nested context', () => { const cause = new Error('message!');