officescripts-logging-framework
    Preparing search index...

    Class ScriptError

    A custom error class for domain-specific or script-level exceptions. Designed to provide clarity and structure when handling expected or controlled failures in scripts (e.g., logging or validation errors). It supports error chaining through an optional cause parameter, preserving the original stack trace. Prefer using ScriptError for intentional business logic errors or internal errors to distinguish them from unexpected system-level failures.

    const original = new Error("Missing field")
    throw new ScriptError("Validation failed", original)

    Hierarchy

    • Error
      • ScriptError
    Index

    Constructors

    • Constructs a new ScriptError.

      Parameters

      • message: string

        A description of the error.

      • Optionalcause: Error

        (Optional) The original error that caused this one. If provided the exception message will have a reference to the cause.

      Returns ScriptError

    Properties

    cause?: Error

    (Optional) The original error that caused this one. If provided the exception message will have a reference to the cause.

    message: string
    name: string
    stack?: string
    stackTraceLimit: number

    The Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)).

    The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed.

    If set to a non-number value, or set to a negative number, stack traces will not capture any frames.

    Methods

    • Utility method to rethrow the deepest original cause if present, otherwise rethrows this ScriptError itself. Useful for deferring a controlled exception and then surfacing the root cause explicitly.

      Returns never

    • Override toString() method.

      Returns string

      The name and the message on the first line, then on the second line the Stack trace section name, i.e. 'Stack trace:'. Starting on the third line the stack trace information. If a cause was provided the stack trace will refer to the cause otherwise to the original exception.

    • Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

      const myObject = {};
      Error.captureStackTrace(myObject);
      myObject.stack; // Similar to `new Error().stack`

      The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

      The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

      The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

      function a() {
      b();
      }

      function b() {
      c();
      }

      function c() {
      // Create an error without stack trace to avoid calculating the stack trace twice.
      const { stackTraceLimit } = Error;
      Error.stackTraceLimit = 0;
      const error = new Error();
      Error.stackTraceLimit = stackTraceLimit;

      // Capture the stack trace above function b
      Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
      throw error;
      }

      a();

      Parameters

      • targetObject: object
      • OptionalconstructorOpt: Function

      Returns void