Error
The Error class is globally available — no import is needed. It provides structured error objects with a message, optional error code, and optional data payload.
Creating Errors
Section titled “Creating Errors”// Simple error with a messagethrow new Error("something went wrong");
// Error with a codethrow new Error("not found", 404);
// Error with a code and additional datathrow new Error("validation failed", "VALIDATION_ERROR", { "field": "age" });Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
message | string | The error message (required) |
code | any | Optional error code (e.g. 404, "NOT_FOUND") |
data | any | Optional additional data |
stack | string | Stack trace (reserved for future use) |
Methods
Section titled “Methods”toString(): string
Section titled “toString(): string”Returns a string representation of the error.
const err = new Error("file not found", 404);println(err.toString()); // "Error: file not found"Error Handling with try/catch
Section titled “Error Handling with try/catch”function divide(a: int, b: int): int { if (b == 0) { throw new Error("division by zero", "MATH_ERROR"); } return a / b;}
function main() { try { const result = divide(10, 0); println(result); } catch (e) { println(e.message); // "division by zero" println(e.code); // "MATH_ERROR" }}Custom Error Patterns
Section titled “Custom Error Patterns”Since code and data are flexible, you can build rich error objects:
// HTTP-style errorsthrow new Error("Unauthorized", 401, { "required": "Bearer token" });
// Validation errors with field detailsthrow new Error("Invalid input", "VALIDATION", { "field": "email", "reason": "must be a valid email address"});Checking Error Properties
Section titled “Checking Error Properties”try { // ... something that might fail} catch (e) { if (e.code == 404) { println("Not found: " + e.message); } else if (e.code == "VALIDATION") { println("Validation error in field: " + e.data["field"]); } else { println("Unexpected error: " + e.toString()); }}