Bun

class

$.ShellError

class ShellError

ShellError represents an error that occurred while executing a shell command with the Bun Shell.

try {
  const result = await $`exit 1`;
} catch (error) {
  if (error instanceof ShellError) {
    console.log(error.exitCode); // 1
  }
}
  • cause?: unknown

    The cause of the error.

  • readonly exitCode: number
  • message: string
  • name: string
  • stack?: string
  • readonly stderr: Buffer
  • readonly stdout: Buffer
  • static stackTraceLimit: number

    The maximum number of stack frames to capture.

  • Read from stdout as an ArrayBuffer

    @returns

    Stdout as an ArrayBuffer

    const output = await $`echo hello`;
    console.log(output.arrayBuffer()); // ArrayBuffer { byteLength: 6 }
    
  • Read from stdout as a Blob

    @returns

    Stdout as a blob

    const output = await $`echo hello`;
    console.log(output.blob()); // Blob { size: 6, type: "" }
    
  • Read from stdout as an Uint8Array

    @returns

    Stdout as an Uint8Array

    const output = await $`echo hello`;
    console.log(output.bytes()); // Uint8Array { byteLength: 6 }
    
  • json(): any;

    Read from stdout as a JSON object

    @returns

    Stdout as a JSON object

    const output = await $`echo '{"hello": 123}'`;
    console.log(output.json()); // { hello: 123 }
    
  • encoding?: BufferEncoding
    ): string;

    Read from stdout as a string

    @param encoding

    The encoding to use when decoding the output

    @returns

    Stdout as a string with the given encoding

    Read as UTF-8 string

    const output = await $`echo hello`;
    console.log(output.text()); // "hello\n"
    

    Read as base64 string

    const output = await $`echo ${atob("hello")}`;
    console.log(output.text("base64")); // "hello\n"
    
  • targetObject: object,
    constructorOpt?: Function
    ): void;

    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();
    
  • static isError(
    value: unknown
    ): value is Error;

    Check if a value is an instance of Error

    @param value

    The value to check

    @returns

    True if the value is an instance of Error, false otherwise

  • err: Error,
    stackTraces: CallSite[]
    ): any;