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

ClassCodeMeaning
UnsupportedRuntimeErrorERR_UNKNOWN_RUNTIMEStrict mode could not identify the runtime
UnsupportedCapabilityErrorERR_UNSUPPORTED_CAPABILITYA required API is unavailable
PermissionDeniedErrorERR_PERMISSION_DENIEDThe runtime exposes the API but denies access
RuntimeOperationErrorERR_RUNTIME_OPERATIONA native operation failed for another reason
TimeoutErrorERR_TIMEOUTThe deadline expired
OperationAbortedErrorERR_OPERATION_ABORTEDAn AbortSignal cancelled the operation
HTTPResponseErrorERR_HTTP_RESPONSEAn 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.