Skip to main content
Deno 2 is finally here 🎉️
Learn more

Custom Stack

Create a fancy error with a custom stack.

// Imports
import { createError } from "https://deno.land/x/cstack@0.1.0/mod.ts";

You can create an error by either passing an error object or creating your own custom stack.

throw createError(new Error("hello world!"));

or

throw createError({
  name: "Custom Error",
  message: "hello world",
  trace: [
    { at: "somewhere" },
    { at: "file://a/b/c.ts", async: true },
    { at: "file://a/b/c.ts", y: 123 },
    { at: "file://a/b/c.ts", y: 123, x: 456 },
    "    strings work too",
  ],
});

preview

Cstack produces clean error objects, yet beautiful errors when thrown.

try {
  throw error;
} catch (error) {
  console.log("Name:", error.name);
  console.log("Message:", error.message);
  console.log("Stack:", error.stack);
}

throw error;

preview

The magic behind this is a secret property in the object called __modifiedStack which contains styled version of the stack property. The magicError function do some magic things to make sure that the stack property is the __modifiedStack property only when the error is thrown, that way we can show custom messages when throwing errors and have a normal error object otherwise.

const error = new Error() as Error & { __modifiedStack: string };
error.name = "Custom Error";
error.message = "Hello";
error.stack = error.__modifiedStack = "Hello World, this is a modified stack";
error.__modifiedStack += "\n    This will only show when the error is thrown!";
error = magicError(error);

try {
  throw error;
} catch (error) {
  console.log("Name:", error.name);
  console.log("Message:", error.message);
  console.log("Stack:", error.stack);
}

throw error;

preview