Merge pull request #3217 from NativeScript/fix-error

Properly extend Error so message and stack appear in error activity
This commit is contained in:
Panayot Cankov
2016-12-01 11:52:35 +02:00
committed by GitHub
7 changed files with 97 additions and 120 deletions

View File

@ -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");

View File

@ -878,6 +878,7 @@ declare var RegExp: RegExpConstructor;
interface Error {
name: string;
stack: string;
message: string;
}

View File

@ -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;
}
}

View File

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

View File

@ -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;
}
}
}

View File

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

View File

@ -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 (<any>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; }
}