Skip to content

Commit 5db5dfd

Browse files
committed
Properly extend Error instead of implementing it, update the Error message and stack to appear in the console and error activity
1 parent 9742d2f commit 5db5dfd

File tree

7 files changed

+97
-120
lines changed

7 files changed

+97
-120
lines changed

tests/app/xml-declaration/xml-declaration-tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ export function test_NonExistingElementError() {
782782
var basePath = "xml-declaration/";
783783
var expectedErrorStart =
784784
"Building UI from XML. @file:///app/" + basePath + "errors/non-existing-element.xml:11:5\n" +
785-
" Module 'ui/unicorn' not found for element 'Unicorn'.";
785+
" > Module 'ui/unicorn' not found for element 'Unicorn'.";
786786
var message;
787787
try {
788788
builder.load(__dirname + "/errors/non-existing-element.xml");
@@ -796,7 +796,7 @@ export function test_NonExistingElementInTemplateError() {
796796
var basePath = "xml-declaration/";
797797
var expectedErrorStart =
798798
"Building UI from XML. @file:///app/" + basePath + "errors/non-existing-element-in-template.xml:14:17\n" +
799-
" Module 'ui/unicorn' not found for element 'Unicorn'.";
799+
" > Module 'ui/unicorn' not found for element 'Unicorn'.";
800800
var message;
801801
var page = builder.load(__dirname + "/errors/non-existing-element-in-template.xml");
802802
TKUnit.assert(view, "Expected the xml to generate a page");

tns-core-modules/lib.core.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,7 @@ declare var RegExp: RegExpConstructor;
878878

879879
interface Error {
880880
name: string;
881+
stack: string;
881882
message: string;
882883
}
883884

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { knownFolders } from "file-system"
2+
3+
export var debug = true;
4+
5+
var applicationRootPath: string;
6+
function ensureAppRootPath() {
7+
if (!applicationRootPath) {
8+
applicationRootPath = knownFolders.currentApp().path;
9+
applicationRootPath = applicationRootPath.substr(0, applicationRootPath.length - "app/".length);
10+
}
11+
}
12+
13+
export class Source {
14+
private _uri: string;
15+
private _line: number;
16+
private _column: number;
17+
18+
private static _source: symbol = Symbol("source");
19+
20+
constructor(uri: string, line: number, column: number) {
21+
ensureAppRootPath();
22+
23+
if (uri.length > applicationRootPath.length && uri.substr(0, applicationRootPath.length) === applicationRootPath) {
24+
this._uri = "file://" + uri.substr(applicationRootPath.length);
25+
} else {
26+
this._uri = uri;
27+
}
28+
this._line = line;
29+
this._column = column;
30+
}
31+
32+
get uri(): string { return this._uri; }
33+
get line(): number { return this._line; }
34+
get column(): number { return this._column; }
35+
36+
public toString() {
37+
return this._uri + ":" + this._line + ":" + this._column;
38+
}
39+
40+
public static get(object: any): Source {
41+
return object[Source._source];
42+
}
43+
44+
public static set(object: any, src: Source) {
45+
object[Source._source] = src;
46+
}
47+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Source } from "./debug-common";
2+
export * from "./debug-common";
3+
4+
export class ScopeError extends Error {
5+
constructor(inner: Error, message?: string) {
6+
let formattedMessage;
7+
if (message && inner.message) {
8+
formattedMessage = message + "\n > " + inner.message.replace("\n", "\n ");
9+
} else {
10+
formattedMessage = message || inner.message || undefined;
11+
}
12+
super(formattedMessage);
13+
this.stack = "Error: " + this.message + "\n" + inner.stack.substr(inner.stack.indexOf("\n") + 1);
14+
this.message = formattedMessage;
15+
}
16+
}
17+
18+
export class SourceError extends ScopeError {
19+
constructor(child: Error, source: Source, message?: string) {
20+
super(child, message ? message + " @" + source + "" : source + "");
21+
}
22+
}

tns-core-modules/utils/debug.d.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,15 @@ declare module "utils/debug" {
4343
/**
4444
* An Error class that provides additional context to an error.
4545
*/
46-
export class ScopeError implements Error {
46+
export class ScopeError extends Error {
4747
/**
4848
* Creates a new ScopeError providing addtional context to the child error.
4949
* @param child The child error to extend.
5050
* @param message Additional message to prepend to the child error.
5151
*/
5252
constructor(child: Error, message?: string);
53-
54-
/**
55-
* Gets the child error.
56-
*/
57-
child: Error;
58-
59-
/**
60-
* Gets the error message.
61-
*/
62-
message: string;
63-
64-
/**
65-
* Gets the stack trace.
66-
*/
67-
stack: string;
68-
69-
/**
70-
* Gets the error name.
71-
*/
72-
name: string;
7353
}
74-
54+
7555
/**
7656
* Represents a scope error providing addiot
7757
*/
@@ -83,10 +63,5 @@ declare module "utils/debug" {
8363
* @param message Additonal message to prepend along the source location and the child error's message.
8464
*/
8565
constructor(child: Error, source: Source, message?: string);
86-
87-
/**
88-
* Gets the error source.
89-
*/
90-
source: Source;
9166
}
92-
}
67+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Source } from "./debug-common";
2+
export * from "./debug-common";
3+
4+
export class ScopeError extends Error {
5+
constructor(inner: Error, message?: string) {
6+
let formattedMessage;
7+
if (message && inner.message) {
8+
formattedMessage = message + "\n > " + inner.message.replace("\n", "\n ");
9+
} else {
10+
formattedMessage = message || inner.message || undefined;
11+
}
12+
super(formattedMessage);
13+
this.stack = inner.stack;
14+
this.message = formattedMessage;
15+
}
16+
}
17+
18+
export class SourceError extends ScopeError {
19+
constructor(child: Error, source: Source, message?: string) {
20+
super(child, message ? message + " @" + source + "" : source + "");
21+
}
22+
}

tns-core-modules/utils/debug.ts

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)