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 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.

  • 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;

    Create .stack property on a target object

  • 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;