Error()
→ objectNative JavaScript Error constructor for creating throwable error objects in try/catch error handling.
Syntax
new Error([message])
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
message |
string | No | Human-readable description of the error. See the caveat below — in the SFMC engine it is not readable back via error.message. |
Description
Error is the native JavaScript Error constructor. Use it with throw and try/catch for structured error handling in SSJS.
Engine caveat (runtime-verified): Unlike standard JavaScript, a JS-constructed
new Error("msg")in the SFMC (Jint) engine does not expose the message via.message—e.messagereads backundefined. Recover the message withString(e)or("" + e)(both return the constructor argument), ore.toString()(returns"Error: undefined"). This differs from engine-raised errors (thrown by the platform itself, e.g. a badPlatform.Functioncall), which do carry both.message(short text) and.description(fuller text).
Examples
Basic throw and catch
try {
throw new Error("Something went wrong");
} catch (e) {
// e.message is undefined for a JS-constructed Error — use String(e).
Write(String(e)); // "Something went wrong"
}
Conditional error
function getSubscriberEmail(sk) {
if (!sk) {
throw new Error("SubscriberKey is required");
}
var email = Platform.Function.Lookup("Subscribers", "Email", "SubscriberKey", sk);
if (!email) {
throw new Error("No subscriber found for key: " + sk);
}
return email;
}
try {
var email = getSubscriberEmail(subscriberKey);
Write("<p>Email: " + email + "</p>");
} catch (e) {
// String(e) recovers the thrown message; e.message would be undefined here.
Write('<p class="error">' + String(e) + "</p>");
}
HTTP error handling
try {
var req = new Script.Util.HttpRequest("https://api.example.com/data");
req.method = "GET";
req.continueOnError = true;
var resp = req.send();
if (resp.statusCode === 401) {
throw new Error("Unauthorized: check your access token");
} else if (resp.statusCode !== 200) {
throw new Error("API returned status " + resp.statusCode);
}
var data = Platform.Function.ParseJSON(String(resp.content) + "");
// process data...
} catch (e) {
// Use String(e) — e.message is undefined for JS-constructed Error objects.
Platform.Function.InsertData("ErrorLog", "Message", String(e), "Timestamp", Platform.Function.Now());
Platform.Response.Redirect("/error", false);
}
Error in serialization
Stringify(e) behaves differently depending on how the error was created:
- For a JS-constructed
new Error(...),Stringify(e)surfaces only a hidden{"jintException": ...}(the .NET stack) — not the message. UseString(e)to log the message. - For an engine-raised error,
Stringify(e)yields{"message": ..., "description": ...}.
try {
performOperation();
} catch (e) {
// For engine-raised errors this includes message + description;
// for new Error(...) prefer String(e) to capture the message.
Write("<pre>Error details: " + String(e) + " | " + Stringify(e) + "</pre>");
}
Notes
The error object in SSJS is similar to but not identical to the standard ECMAScript Error object, and its shape depends on origin:
- JS-constructed (
new Error("msg")):.messageisundefined; the message is only recoverable viaString(e)/("" + e). Carries a hidden.jintException..stackis not available. - Engine-raised (platform-thrown, e.g. a bad
Platform.Functioncall): exposes.message(short) and.description(fuller text, ending with a Jint/.NET exception detail)..numberand.jintExceptionareundefined. - Thrown primitives / plain objects (
throw "text",throw { message: ..., description: ... }) are valid; incatch (e),emay be a string, a plain object, or an engine error — probe accordingly. AvoidString(e)on a thrown plain object (it can throw a .NET null-reference); preferStringify(e)ore.toString()there. for (var k in e)is unreliable for property discovery: on engine errors, JSErrorinstances, and thrown strings it enumerates the message characters, not property names.
SFMC platform errors (not thrown by your code) are also catchable, and these DO carry .message:
try {
Platform.Function.Lookup("NonExistentDE", "Field", "Key", "value");
} catch (e) {
// Engine-raised errors expose .message and .description.
Write("Platform error: " + e.message);
}