From 3eaa06bd05afc9fd342e6bc6d12c969297831430 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 30 Nov 2026 20:48:37 +0000 Subject: [PATCH 1/2] Error has JavaScript's name and toString() - `name` ("Error", and each subclass its own: RangeError, TypeError, SyntaxError, ReferenceError, SuppressedError) is a field a subclass may set (`this.name = "ValidationError"`) - `toString()` is "Name: message", or the name alone without a message - so an Error prints, and is made a string, as in JavaScript (`${e}`, `"" + e`, print(e), console.log(e)); it was refused (no toString), and a caught one held in `any` printed "[object Object]" - `message` stays the first field after the vtable, where the report of an uncaught exception reads it; `name` is declared after the constructor's fields Co-Authored-By: Claude Opus 5.5 --- src/core/core.d.ts | 2 ++ src/core/core.ts | 20 +++++++++++++++ tests/error_name_tostring.ts | 50 ++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 tests/error_name_tostring.ts diff --git a/src/core/core.d.ts b/src/core/core.d.ts index 257b6f6..8c2fa7d 100644 --- a/src/core/core.d.ts +++ b/src/core/core.d.ts @@ -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 diff --git a/src/core/core.ts b/src/core/core.ts index 5206484..965c8ca 100644 --- a/src/core/core.ts +++ b/src/core/core.ts @@ -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"; } } @@ -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"; } } diff --git a/tests/error_name_tostring.ts b/tests/error_name_tostring.ts new file mode 100644 index 0000000..2629320 --- /dev/null +++ b/tests/error_name_tostring.ts @@ -0,0 +1,50 @@ +// 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 = caught; + check(asError.toString() == "RangeError: thrown", "a caught error's text"); + } + + print(e, v); + console.log(e, new TypeError("printed")); + console.log("ALL DONE"); +} From a158b511b65459ebd9bc6af9bd7a37c35c017fd8 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 30 Nov 2026 21:17:39 +0000 Subject: [PATCH 2/2] error_name_tostring: prints nothing with "Error" in it, which tests.sh takes as a failure Co-Authored-By: Claude Opus 5.5 --- tests/error_name_tostring.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/error_name_tostring.ts b/tests/error_name_tostring.ts index 2629320..81374bb 100644 --- a/tests/error_name_tostring.ts +++ b/tests/error_name_tostring.ts @@ -44,7 +44,8 @@ function main() { check(asError.toString() == "RangeError: thrown", "a caught error's text"); } - print(e, v); - console.log(e, new TypeError("printed")); + // (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"); }