j2s/lib/src/stdlib/values/errors.ts

40 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-01-06 11:20:51 +00:00
import { func, object } from "../primordials.ts";
2024-12-11 09:53:02 +00:00
import { String } from "./string.ts";
export class Error {
public declare name: string;
public declare message: string;
public toString() {
let res = this.name || "Error";
const msg = this.message;
if (msg) res += ": " + msg;
return res;
}
public constructor (msg = "") {
if (func.invokeType(arguments, this) === "call") return new Error(msg);
this.message = String(msg);
}
}
2024-12-13 00:29:41 +00:00
object.defineField(Error.prototype, "name", { c: true, e: false, w: true, v: "Error" });
object.defineField(Error.prototype, "message", { c: true, e: false, w: true, v: "" });
2024-12-11 09:53:02 +00:00
func.setCallable(Error, true);
func.setConstructable(Error, true);
export class SyntaxError extends Error { }
2024-12-13 00:29:41 +00:00
object.defineField(SyntaxError.prototype, "name", { c: true, e: false, w: true, v: "SyntaxError" });
2024-12-11 09:53:02 +00:00
func.setCallable(SyntaxError, true);
func.setConstructable(SyntaxError, true);
export class TypeError extends Error { }
2024-12-13 00:29:41 +00:00
object.defineField(TypeError.prototype, "name", { c: true, e: false, w: true, v: "TypeError" });
2024-12-11 09:53:02 +00:00
func.setCallable(TypeError, true);
func.setConstructable(TypeError, true);
export class RangeError extends Error { }
2024-12-13 00:29:41 +00:00
object.defineField(RangeError.prototype, "name", { c: true, e: false, w: true, v: "RangeError" });
2024-12-11 09:53:02 +00:00
func.setCallable(RangeError, true);
func.setConstructable(RangeError, true);