Core concepts
Error handling
Handle errors consistently across runtimes.
Error handling
Base class
All normalized errors extend RuntimeError:
class RuntimeError extends Error {
readonly code: RuntimeErrorCode
readonly runtime?: Runtime
override readonly cause?: unknown
}
try {
// oruntime operation
} catch (error) {
if (error instanceof RuntimeError) {
console.error({ code: error.code, runtime: error.runtime, cause: error.cause })
} else {
throw error
}
}
Classes and codes
| Class | Code | Meaning |
|---|---|---|
UnsupportedRuntimeError | ERR_UNKNOWN_RUNTIME | Strict mode could not identify the runtime |
UnsupportedCapabilityError | ERR_UNSUPPORTED_CAPABILITY | A required API is unavailable |
PermissionDeniedError | ERR_PERMISSION_DENIED | The runtime exposes the API but denies access |
RuntimeOperationError | ERR_RUNTIME_OPERATION | A native operation failed for another reason |
TimeoutError | ERR_TIMEOUT | The deadline expired |
OperationAbortedError | ERR_OPERATION_ABORTED | An AbortSignal cancelled the operation |
HTTPResponseError | ERR_HTTP_RESPONSE | An HTTP response has a non-2xx status |
Inspect an HTTP error
try {
await fetchJSON('/api/user')
} catch (error) {
if (error instanceof HTTPResponseError) {
console.error(error.status, error.statusText, error.response)
}
}
The response remains available as error.response; its body is not read automatically.
Normalize an external error
throw normalizeRuntimeError(error, {
runtime: 'deno',
capability: 'environmentVariables',
operation: 'read environment variable'
})
The function recognizes common permission names and codes, including NotCapable, PermissionDenied, EACCES, and EPERM. Existing RuntimeError instances are returned unchanged to preserve their context.