From d56cf6a92d909af22eb3d6f09cee9a2581f60abb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=A1ko=20Hevery?= Date: Tue, 17 Dec 2019 11:39:55 -0800 Subject: [PATCH] refactor: improve assert error by including the failed condition (#34799) PR Close #34799 --- packages/core/src/util/assert.ts | 34 +++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/core/src/util/assert.ts b/packages/core/src/util/assert.ts index 165241ab1d..fc9ddc8412 100644 --- a/packages/core/src/util/assert.ts +++ b/packages/core/src/util/assert.ts @@ -14,68 +14,78 @@ import {stringify} from './stringify'; export function assertNumber(actual: any, msg: string) { if (typeof actual != 'number') { - throwError(msg); + throwError(msg, typeof actual, 'number', '==='); + } +} + +export function assertString(actual: any, msg: string) { + if (typeof actual != 'string') { + throwError(msg, actual === null ? 'null' : typeof actual, 'string', '==='); } } export function assertEqual(actual: T, expected: T, msg: string) { if (actual != expected) { - throwError(msg); + throwError(msg, actual, expected, '=='); } } export function assertNotEqual(actual: T, expected: T, msg: string) { if (actual == expected) { - throwError(msg); + throwError(msg, actual, expected, '!='); } } export function assertSame(actual: T, expected: T, msg: string) { if (actual !== expected) { - throwError(msg); + throwError(msg, actual, expected, '==='); } } export function assertNotSame(actual: T, expected: T, msg: string) { if (actual === expected) { - throwError(msg); + throwError(msg, actual, expected, '!=='); } } export function assertLessThan(actual: T, expected: T, msg: string) { if (actual >= expected) { - throwError(msg); + throwError(msg, actual, expected, '<'); } } export function assertLessThanOrEqual(actual: T, expected: T, msg: string) { if (actual > expected) { - throwError(msg); + throwError(msg, actual, expected, '<='); } } export function assertGreaterThan(actual: T, expected: T, msg: string) { if (actual <= expected) { - throwError(msg); + throwError(msg, actual, expected, '>'); } } export function assertNotDefined(actual: T, msg: string) { if (actual != null) { - throwError(msg); + throwError(msg, actual, null, '=='); } } export function assertDefined(actual: T, msg: string) { if (actual == null) { - throwError(msg); + throwError(msg, actual, null, '!='); } } -export function throwError(msg: string): never { +export function throwError(msg: string): never; +export function throwError(msg: string, actual: any, expected: any, comparison: string): never; +export function throwError(msg: string, actual?: any, expected?: any, comparison?: string): never { // tslint:disable-next-line debugger; // Left intentionally for better debugger experience. - throw new Error(`ASSERTION ERROR: ${msg}`); + throw new Error( + `ASSERTION ERROR: ${msg}` + + (comparison == null ? '' : ` [Expected=> ${expected} ${comparison} ${actual} <=Actual]`)); } export function assertDomNode(node: any) {