Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose ShellError #13864

Open
ajitid opened this issue Sep 10, 2024 · 1 comment
Open

Expose ShellError #13864

ajitid opened this issue Sep 10, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request shell Something to do with Bun as a shell

Comments

@ajitid
Copy link

ajitid commented Sep 10, 2024

What is the problem this feature would solve?

Right now when I install @types/bun and write:

import { $, ShellError } from 'bun'

try {
  await $`a-command-that-will-surely-fail`
} catch (err) {
  if (err instanceof ShellError) {
    // handle error
  }
}

The code doesn't give TypeScript error but fails at runtime because ShellError is undefined.

What is the feature you are proposing to solve the problem?

Expose ShellError class, just like $ is available. This way I would be able to use instanceof on it.

What alternatives have you considered?

I tried two workarounds neither which I liked:

  1. Intentionally make Bun shell throw error once so I could grab an actual instance
import { $, type ShellError } from 'bun'

let classCtor: Function
try {
  await $`a-command-that-will-surely-fail^#^#`
} catch (err: any) {
  classCtor = err.constructor
}

export const isShellError = (err: any): err is ShellError => {
  return err instanceof classCtor
}
  1. Check if properties match, and declare the error as ShellError
import { type ShellError } from 'bun'

export const isShellError = (err: any): err is ShellError => {
  if (err === null) return false
  if (typeof err !== 'object') return false
  return (
    err.constructor?.name === 'ShellError' &&
    typeof err.json === 'function' &&
    typeof err.exitCode === 'number'
  )
}
@ajitid ajitid added the enhancement New feature or request label Sep 10, 2024
@Electroid Electroid added the shell Something to do with Bun as a shell label Sep 10, 2024
@alii alii self-assigned this Feb 17, 2025
@alii
Copy link
Collaborator

alii commented Feb 19, 2025

This is fixed in #17424. Working on fixing other type declaration issues before that merges

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request shell Something to do with Bun as a shell
Projects
None yet
Development

No branches or pull requests

3 participants