Skip to content

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.

// Simple error with a message
throw new Error("something went wrong");
// Error with a code
throw new Error("not found", 404);
// Error with a code and additional data
throw new Error("validation failed", "VALIDATION_ERROR", { "field": "age" });
PropertyTypeDescription
messagestringThe error message (required)
codeanyOptional error code (e.g. 404, "NOT_FOUND")
dataanyOptional additional data
stackstringStack trace (reserved for future use)

Returns a string representation of the error.

const err = new Error("file not found", 404);
println(err.toString()); // "Error: file not found"
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"
}
}

Since code and data are flexible, you can build rich error objects:

// HTTP-style errors
throw new Error("Unauthorized", 401, { "required": "Bearer token" });
// Validation errors with field details
throw new Error("Invalid input", "VALIDATION", {
"field": "email",
"reason": "must be a valid email address"
});
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());
}
}