From 5db5dfd8674f3168e405c8a3c6962847060f51c2 Mon Sep 17 00:00:00 2001 From: Panayot Cankov Date: Thu, 1 Dec 2016 09:03:40 +0200 Subject: [PATCH] Properly extend Error instead of implementing it, update the Error message and stack to appear in the console and error activity --- .../xml-declaration/xml-declaration-tests.ts | 4 +- tns-core-modules/lib.core.d.ts | 1 + tns-core-modules/utils/debug-common.ts | 47 ++++++++++ tns-core-modules/utils/debug.android.ts | 22 +++++ tns-core-modules/utils/debug.d.ts | 31 +------ tns-core-modules/utils/debug.ios.ts | 22 +++++ tns-core-modules/utils/debug.ts | 90 ------------------- 7 files changed, 97 insertions(+), 120 deletions(-) create mode 100644 tns-core-modules/utils/debug-common.ts create mode 100644 tns-core-modules/utils/debug.android.ts create mode 100644 tns-core-modules/utils/debug.ios.ts delete mode 100644 tns-core-modules/utils/debug.ts diff --git a/tests/app/xml-declaration/xml-declaration-tests.ts b/tests/app/xml-declaration/xml-declaration-tests.ts index 674a91961..fa8cc2b19 100644 --- a/tests/app/xml-declaration/xml-declaration-tests.ts +++ b/tests/app/xml-declaration/xml-declaration-tests.ts @@ -782,7 +782,7 @@ export function test_NonExistingElementError() { var basePath = "xml-declaration/"; var expectedErrorStart = "Building UI from XML. @file:///app/" + basePath + "errors/non-existing-element.xml:11:5\n" + - " ↳Module 'ui/unicorn' not found for element 'Unicorn'."; + " > Module 'ui/unicorn' not found for element 'Unicorn'."; var message; try { builder.load(__dirname + "/errors/non-existing-element.xml"); @@ -796,7 +796,7 @@ export function test_NonExistingElementInTemplateError() { var basePath = "xml-declaration/"; var expectedErrorStart = "Building UI from XML. @file:///app/" + basePath + "errors/non-existing-element-in-template.xml:14:17\n" + - " ↳Module 'ui/unicorn' not found for element 'Unicorn'."; + " > Module 'ui/unicorn' not found for element 'Unicorn'."; var message; var page = builder.load(__dirname + "/errors/non-existing-element-in-template.xml"); TKUnit.assert(view, "Expected the xml to generate a page"); diff --git a/tns-core-modules/lib.core.d.ts b/tns-core-modules/lib.core.d.ts index 5168a5584..43918b42f 100644 --- a/tns-core-modules/lib.core.d.ts +++ b/tns-core-modules/lib.core.d.ts @@ -878,6 +878,7 @@ declare var RegExp: RegExpConstructor; interface Error { name: string; + stack: string; message: string; } diff --git a/tns-core-modules/utils/debug-common.ts b/tns-core-modules/utils/debug-common.ts new file mode 100644 index 000000000..7dc575ef2 --- /dev/null +++ b/tns-core-modules/utils/debug-common.ts @@ -0,0 +1,47 @@ +import { knownFolders } from "file-system" + +export var debug = true; + +var applicationRootPath: string; +function ensureAppRootPath() { + if (!applicationRootPath) { + applicationRootPath = knownFolders.currentApp().path; + applicationRootPath = applicationRootPath.substr(0, applicationRootPath.length - "app/".length); + } +} + +export class Source { + private _uri: string; + private _line: number; + private _column: number; + + private static _source: symbol = Symbol("source"); + + constructor(uri: string, line: number, column: number) { + ensureAppRootPath(); + + if (uri.length > applicationRootPath.length && uri.substr(0, applicationRootPath.length) === applicationRootPath) { + this._uri = "file://" + uri.substr(applicationRootPath.length); + } else { + this._uri = uri; + } + this._line = line; + this._column = column; + } + + get uri(): string { return this._uri; } + get line(): number { return this._line; } + get column(): number { return this._column; } + + public toString() { + return this._uri + ":" + this._line + ":" + this._column; + } + + public static get(object: any): Source { + return object[Source._source]; + } + + public static set(object: any, src: Source) { + object[Source._source] = src; + } +} diff --git a/tns-core-modules/utils/debug.android.ts b/tns-core-modules/utils/debug.android.ts new file mode 100644 index 000000000..ab0a391a9 --- /dev/null +++ b/tns-core-modules/utils/debug.android.ts @@ -0,0 +1,22 @@ +import { Source } from "./debug-common"; +export * from "./debug-common"; + +export class ScopeError extends Error { + constructor(inner: Error, message?: string) { + let formattedMessage; + if (message && inner.message) { + formattedMessage = message + "\n > " + inner.message.replace("\n", "\n "); + } else { + formattedMessage = message || inner.message || undefined; + } + super(formattedMessage); + this.stack = "Error: " + this.message + "\n" + inner.stack.substr(inner.stack.indexOf("\n") + 1); + this.message = formattedMessage; + } +} + +export class SourceError extends ScopeError { + constructor(child: Error, source: Source, message?: string) { + super(child, message ? message + " @" + source + "" : source + ""); + } +} diff --git a/tns-core-modules/utils/debug.d.ts b/tns-core-modules/utils/debug.d.ts index 12f711a0c..643393982 100644 --- a/tns-core-modules/utils/debug.d.ts +++ b/tns-core-modules/utils/debug.d.ts @@ -43,35 +43,15 @@ declare module "utils/debug" { /** * An Error class that provides additional context to an error. */ - export class ScopeError implements Error { + export class ScopeError extends Error { /** * Creates a new ScopeError providing addtional context to the child error. * @param child The child error to extend. * @param message Additional message to prepend to the child error. */ constructor(child: Error, message?: string); - - /** - * Gets the child error. - */ - child: Error; - - /** - * Gets the error message. - */ - message: string; - - /** - * Gets the stack trace. - */ - stack: string; - - /** - * Gets the error name. - */ - name: string; } - + /** * Represents a scope error providing addiot */ @@ -83,10 +63,5 @@ declare module "utils/debug" { * @param message Additonal message to prepend along the source location and the child error's message. */ constructor(child: Error, source: Source, message?: string); - - /** - * Gets the error source. - */ - source: Source; } -} \ No newline at end of file +} diff --git a/tns-core-modules/utils/debug.ios.ts b/tns-core-modules/utils/debug.ios.ts new file mode 100644 index 000000000..ee09f31fd --- /dev/null +++ b/tns-core-modules/utils/debug.ios.ts @@ -0,0 +1,22 @@ +import { Source } from "./debug-common"; +export * from "./debug-common"; + +export class ScopeError extends Error { + constructor(inner: Error, message?: string) { + let formattedMessage; + if (message && inner.message) { + formattedMessage = message + "\n > " + inner.message.replace("\n", "\n "); + } else { + formattedMessage = message || inner.message || undefined; + } + super(formattedMessage); + this.stack = inner.stack; + this.message = formattedMessage; + } +} + +export class SourceError extends ScopeError { + constructor(child: Error, source: Source, message?: string) { + super(child, message ? message + " @" + source + "" : source + ""); + } +} diff --git a/tns-core-modules/utils/debug.ts b/tns-core-modules/utils/debug.ts deleted file mode 100644 index badf09bc2..000000000 --- a/tns-core-modules/utils/debug.ts +++ /dev/null @@ -1,90 +0,0 @@ -import {knownFolders} from "file-system" - -export var debug = true; - -// TODO: Get this from the runtimes... -var applicationRootPath: string; -function ensureAppRootPath() { - if (!applicationRootPath) { - applicationRootPath = knownFolders.currentApp().path; - applicationRootPath = applicationRootPath.substr(0, applicationRootPath.length - "app/".length); - } -} - -export class Source { - private _uri: string; - private _line: number; - private _column: number; - - private static _source: symbol = Symbol("source"); - - constructor(uri: string, line: number, column: number) { - ensureAppRootPath(); - - if (uri.length > applicationRootPath.length && uri.substr(0, applicationRootPath.length) === applicationRootPath) { - this._uri = "file://" + uri.substr(applicationRootPath.length); - } else { - this._uri = uri; - } - this._line = line; - this._column = column; - } - - get uri(): string { return this._uri; } - get line(): number { return this._line; } - get column(): number { return this._column; } - - public toString() { - return this._uri + ":" + this._line + ":" + this._column; - } - - public static get(object: any): Source { - return object[Source._source]; - } - - public static set(object: any, src: Source) { - object[Source._source] = src; - } -} - -export class ScopeError extends Error { - private _child: Error; - private _message: string; - - constructor(child: Error, message?: string) { - super(message); - if (!child) { - throw new Error("Required child error!"); - } - this._child = child; - this._message = message; - } - - get child() { return this._child; } - get message() { - if (this._message && this._childMessage) { - // It is a ↳ but the ios fails to show this symbol at the moment. - return this._message + "\n \u21B3" + this._childMessage.replace("\n", "\n "); - } - return this._message || this._childMessage || undefined; - } - get name() { return this.child.name; } - get stack() { return (this.child).stack; } - - private get _childMessage(): string { - return this.child.message; - } - - public toString() { return "Error: " + this.message; } -} - -export class SourceError extends ScopeError { - private _source: Source; - - constructor(child: Error, source: Source, message?: string) { - super(child, message ? message + " @" + source + "" : source + ""); - this._source = source; - } - - get source() { return this._source; } -}