Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/core/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ declare class Error {
public filename?: string;
public line?: int;
constructor(message?: string, filename?: string, line?: int);
public name: string;
public toString(): string;
}

@owned
Expand Down
20 changes: 20 additions & 0 deletions src/core/core.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
const LC_COLLATE = 1;

// `message` stays the first field, right after the vtable: the report of an uncaught exception reads it there
// (TypeScriptCompiler's UncaughtException.h). `name` comes after the constructor's fields, so it is a field a
// subclass may set (`this.name = "ValidationError"`), as in JavaScript.
export class Error {
public constructor(public message?: string, public filename?: string, public line?: int) {
}

public name: string = "Error";

// JavaScript's Error.prototype.toString: "Name: message", or the name alone when there is no message
public toString(): string {
const message = this.message;
if (message === undefined || message == "") {
return this.name;
}

return this.name + ": " + message;
}
}

export class RangeError extends Error {
public constructor(message?: string, filename?: string, line?: int) {
super(message, filename, line);
this.name = "RangeError";
}
}

export class TypeError extends Error {
public constructor(message?: string, filename?: string, line?: int) {
super(message, filename, line);
this.name = "TypeError";
}
}

export class SyntaxError extends Error {
public constructor(message?: string, filename?: string, line?: int) {
super(message, filename, line);
this.name = "SyntaxError";
}
}

export class ReferenceError extends Error {
public constructor(message?: string, filename?: string, line?: int) {
super(message, filename, line);
this.name = "ReferenceError";
}
}

Expand All @@ -34,6 +53,7 @@ export class ReferenceError extends Error {
export class SuppressedError extends Error {
public constructor(public error: any, public suppressed: any, message?: string) {
super(message);
this.name = "SuppressedError";
}
}

Expand Down
51 changes: 51 additions & 0 deletions tests/error_name_tostring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Error has JavaScript's `name` and `toString()`: "Error: boom", "RangeError: out", the name alone without a
// message. A subclass may set its own name. An Error printed or made a string was refused (no toString), and a
// caught one held in `any` printed "[object Object]".

class ValidationError extends Error {
constructor(message: string) {
super(message);
this.name = "ValidationError";
}
}

function check(condition: boolean, message: string) {
if (!condition) {
console.log("fail:", message);
assert(false, message);
}
}

function main() {
const e = new Error("boom");
check(e.name == "Error", "the name");
check(e.toString() == "Error: boom", "toString");
check(`${e}` == "Error: boom", "in a template");
check("" + e == "Error: boom", "added to a string");
check(e.message == "boom", "the message is kept");

check(new Error().toString() == "Error", "no message: the name alone");
check(new Error("").toString() == "Error", "an empty message: the name alone");

check(new RangeError("out").toString() == "RangeError: out", "RangeError");
check(new TypeError("bad").name == "TypeError", "TypeError's name");
check(new SyntaxError("x").toString() == "SyntaxError: x", "SyntaxError");
check(new ReferenceError("y").toString() == "ReferenceError: y", "ReferenceError");

const v = new ValidationError("too short");
check(v.toString() == "ValidationError: too short", "a subclass's own name");
check(v instanceof Error, "still an Error");

try {
throw new RangeError("thrown");
} catch (caught) {
check(caught instanceof RangeError, "caught as itself");
const asError = <Error>caught;
check(asError.toString() == "RangeError: thrown", "a caught error's text");
}

// (nothing printed: tests.sh takes a run that prints "Error" as failed)
const printed = `${e} ${v}`;
check(printed == "Error: boom ValidationError: too short", "both in one template");
console.log("ALL DONE");
}
Loading