@rtorcato/cf-common
Classes
CloudflareError
Defined in: index.ts:14
An error that carries an HTTP status, for Workers that turn thrown errors
into Responses.
expose marks whether message is safe to send to the client: it defaults
to true for 4xx (the caller's fault, tell them why) and false for 5xx
(our fault — don't leak internals).
Example
throw new CloudflareError('Session not found', { status: 404 })
Extends
Error
Constructors
Constructor
new CloudflareError(
message,options?):CloudflareError
Defined in: index.ts:21
Parameters
message
string
options?
status?
number
expose?
boolean
code?
string
cause?
unknown
Returns
Overrides
Error.constructor
Properties
name
readonlyname:"CloudflareError"='CloudflareError'
Defined in: index.ts:15
Overrides
Error.name
status
readonlystatus:number
Defined in: index.ts:16
expose
readonlyexpose:boolean
Defined in: index.ts:17
code?
readonlyoptionalcode?:string
Defined in: index.ts:19
Optional machine-readable code (e.g. KV_PARSE_FAILED) for branching on a specific failure.
Functions
isCloudflareError()
isCloudflareError(
error):error is CloudflareError
Defined in: index.ts:40
Type guard for CloudflareError. Structural, not just instanceof:
tsup bundles each subpath entry separately, so a CloudflareError thrown by
one module (e.g. kv) is a different class object from the one http's
defineFetch sees — instanceof across that boundary is false. Matching on
name + status keeps the guard (and defineFetch's status mapping) correct
regardless of which bundle created the error.
Parameters
error
unknown
Returns
error is CloudflareError
toCloudflareError()
toCloudflareError(
error,status?):CloudflareError
Defined in: index.ts:55
Coerce any thrown value into a CloudflareError, passing an existing
one through unchanged. Reuses js-common's getErrorMessage for the message
and defaults to a non-exposed 500.
Parameters
error
unknown
status?
number = 500