oRPC No Throw Literal

Always throw Error instances instead of literal values.

SKILL.md

GitHub

No Throw Literal

In JavaScript, you can throw any value, but it's best to throw only Error instances.

// eslint-disable-next-line no-throw-literal
throw 'error' // ✗ avoid
throw new Error('error') // ✓ recommended

oRPC treats thrown Error instances as best practice, as recommended by JavaScript Standard Style.

Configuration

Customize behavior by setting throwableError in the Registry:

declare module '@orpc/server' { // or '@orpc/contract', '@orpc/client'
  interface Registry {
    throwableError: Error
  }
}

Avoid any/unknown for throwableError because it prevents the client from inferring type-safe errors. Use null | undefined | {} (equivalent to unknown) for stricter inference.

If you configure throwableError as null | undefined | {}, check isSuccess instead of error:

const { error, data, isSuccess } = await safe(client('input'))

if (!isSuccess) {
  if (isDefinedError(error)) {
    // handle type-safe error
  }
} else {
  // handle success
}

Bonus

Enable the ESLint no-throw-literal rule.