Exception hierarchy
All WorkOS exceptions implement theRequestException interface and extend the native JavaScript Error class:
Exception types
ApiKeyRequiredException
Thrown when an API key is required but not provided. Status:403
When it occurs: When attempting to call a server-side method without initializing the client with an API key.
From api-key-required.exception.ts:1:
UnauthorizedException
Thrown when the API key is invalid or the request cannot be authorized. Status:401
When it occurs: Invalid or expired API key, missing authentication credentials.
From unauthorized.exception.ts:3:
BadRequestException
Thrown when the request is malformed or contains invalid parameters. Status:400
When it occurs: Invalid request parameters, malformed data, validation failures.
From bad-request.exception.ts:3:
NotFoundException
Thrown when the requested resource cannot be found. Status:404
When it occurs: Accessing a resource that doesn’t exist or has been deleted.
From not-found.exception.ts:3:
ConflictException
Thrown when the request conflicts with the current state of the server. Status:409
When it occurs: Attempting to create a resource that already exists, or modifying a resource that has been changed by another request.
From conflict.exception.ts:3:
UnprocessableEntityException
Thrown when the request is well-formed but cannot be processed due to semantic errors. Status:422
When it occurs: Business logic validation failures, unmet requirements.
From unprocessable-entity.exception.ts:4:
RateLimitExceededException
Thrown when the rate limit for API requests has been exceeded. Status:429
When it occurs: Making too many requests in a short period.
From rate-limit-exceeded.exception.ts:7:
OauthException
Thrown when OAuth authentication fails. Status: Variable (depends on OAuth error) When it occurs: OAuth flow errors, invalid grants, expired tokens. Fromoauth.exception.ts:3:
GenericServerException
Thrown for general server errors that don’t fit other categories. Status: Variable (5xx server errors) When it occurs: Internal server errors, service unavailable, unexpected errors. Fromgeneric-server.exception.ts:3:
ParseError
Thrown when the response body cannot be parsed as JSON. Status:500
When it occurs: Malformed JSON in API response, network corruption.
From parse-error.ts:3:
Error handling implementation
The SDK handles errors in thehandleHttpError method:
From workos.ts:430:
Best practices
Always catch specific exceptions
Use request IDs for debugging
All exceptions include arequestID property that can be used to debug issues with WorkOS support:
Implement retry logic for rate limits
Type-safe error handling
Summary
Structured exceptions
All exceptions implement a consistent interface with status codes and request IDs.
Type safety
Use TypeScript’s instanceof to handle specific exception types.
Request IDs
Every exception includes a request ID for debugging with WorkOS support.
Retry logic
Implement retry logic for rate limits using the retryAfter property.