ShellError represents an error that occurred while executing a shell command with the Bun Shell.
class
$.ShellError
class ShellError
try {
const result = await $`exit 1`;
} catch (error) {
if (error instanceof ShellError) {
console.log(error.exitCode); // 1
}
}
Read from stdout as an ArrayBuffer
@returnsStdout as an ArrayBuffer
const output = await $`echo hello`; console.log(output.arrayBuffer()); // ArrayBuffer { byteLength: 6 }
Read from stdout as an Uint8Array
@returnsStdout as an Uint8Array
const output = await $`echo hello`; console.log(output.bytes()); // Uint8Array { byteLength: 6 }
Read from stdout as a JSON object
@returnsStdout as a JSON object
const output = await $`echo '{"hello": 123}'`; console.log(output.json()); // { hello: 123 }
- @param encoding
The encoding to use when decoding the output
@returnsStdout 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 ontargetObject
, which when accessed returns a string representing the location in the code at whichError.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 aboveconstructorOpt
, includingconstructorOpt
, 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();