oRPC No Throw Literal
Always throw Error instances instead of literal values.
SKILL.mdskills/orpc-no-throw-literal/SKILL.md
GitHubNo 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
Errorinstances 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/unknownforthrowableErrorbecause it prevents the client from inferring type-safe errors. Usenull | undefined | {}(equivalent tounknown) for stricter inference.
If you configure
throwableErrorasnull | undefined | {}, checkisSuccessinstead oferror:
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.