<@U04UQ68083H> In case of an API error, what’s the...
# ory-copilot
b
@chilly-king-10285 In case of an API error, what’s the expected error structure returned by the API? Can we assume it’s always and consistently the same? Which fields of the error object are required and which ones are optional?
b
A message from kapa.ai
Based on the knowledge sources, I can provide information about the error structure returned by Ory APIs. Ory uses a standard JSON API error format across its services, but there are actually two slightly different error structures depending on which API you're using: ## Kratos API Error Structure For the Ory Kratos API (identities frontend), the error structure includes:
Copy code
json {   "error": {     "code": 404,     "debug": "SQL field \"foo\" is not a bool.",     "details": {},     "id": "error_id",     "message": "The resource could not be found",     "reason": "User with ID 1234 does not exist.",     "request": "d7ef54b1-ec15-46e6-bccb-524b82c035e6",     "status": "Not Found"   } }
Required fields: -
error.message
Optional fields: -
error.code
- The status code -
error.debug
- Debug information (often not exposed to protect sensitive information) -
error.details
- Further error details -
error.id
- The error ID for identifying errors in application logic -
error.reason
- A human-readable reason for the error -
error.request
- The request ID for tracing errors across services -
error.status
- The status description [Kratos API Reference](https://www.ory.sh/docs/kratos/reference/api) ## Ory Network API Error Structure For other Ory APIs (identity, events, frontend, project, workspace), the error structure is slightly different:
Copy code
json {   "error": {     "debug": "The database adapter was unable to find the element",     "error": "The requested resource could not be found",     "error_description": "Object with ID 12345 does not exist",     "message": "Error message content",     "status_code": 404   } }
Required fields: -
error
(the object itself) Optional fields: -
error.debug
- Debug information (usually not available unless enabled) -
error.error
- The error name -
error.error_description
- Further information on the nature of the error -
error.message
- The error message -
error.status_code
- The HTTP status code [Ory API Reference](https://www.ory.sh/docs/reference/api) While the overall structure is consistent within each API family, there are these two different formats depending on which Ory API you're using. Both formats always include the top-level
error
object, but the internal structure differs slightly.
b
@chilly-king-10285 In the TS SDK there is a
GenericError
type and a
GenericErrorContent
type. What’s the difference?
b
A message from kapa.ai
Based on the knowledge sources provided, I don't have specific information about the TypeScript SDK's
GenericError
and
GenericErrorContent
types. The documents only show the API response structures but don't detail the TypeScript SDK type definitions. From the error structures shown in the API documentation, I can see there are two main error formats used across Ory APIs: 1. The standard Ory JSON API error format (used in identity/frontend APIs):
Copy code
{
  "error": {
    "debug": "...",
    "error": "...",
    "error_description": "...",
    "message": "...",
    "status_code": 404
  }
}
2. The generic error format (used in project/OAuth2 APIs):
Copy code
{
  "code": 404,
  "debug": "...",
  "details": {},
  "id": "...",
  "message": "...",
  "reason": "...",
  "request": "...",
  "status": "..."
}
It's possible that in the TypeScript SDK,
GenericError
might represent the complete error response object, while
GenericErrorContent
might represent the inner content of the error. However, without the actual SDK type definitions in the knowledge sources, I cannot definitively explain the difference between these two types. To get accurate information about these TypeScript types, I would recommend checking the Ory TypeScript SDK documentation or source code directly.
b
@chilly-king-10285 Here are the type declarations:
Copy code
/**
 * Error responses are sent when an error (e.g. unauthorized, bad request, ...) occurred.
 * @export
 * @interface GenericError
 */
export interface GenericError {
    /**
     * The status code
     * @type {number}
     * @memberof GenericError
     */
    'code'?: number;
    /**
     * Debug information  This field is often not exposed to protect against leaking sensitive information.
     * @type {string}
     * @memberof GenericError
     */
    'debug'?: string;
    /**
     * Further error details
     * @type {any}
     * @memberof GenericError
     */
    'details'?: any;
    /**
     *
     * @type {GenericErrorContent}
     * @memberof GenericError
     */
    'error'?: GenericErrorContent;
    /**
     * The error ID  Useful when trying to identify various errors in application logic.
     * @type {string}
     * @memberof GenericError
     */
    'id'?: string;
    /**
     * Error message  The error\'s message.
     * @type {string}
     * @memberof GenericError
     */
    'message': string;
    /**
     * A human-readable reason for the error
     * @type {string}
     * @memberof GenericError
     */
    'reason'?: string;
    /**
     * The request ID  The request ID is often exposed internally in order to trace errors across service architectures. This is often a UUID.
     * @type {string}
     * @memberof GenericError
     */
    'request'?: string;
    /**
     * The status description
     * @type {string}
     * @memberof GenericError
     */
    'status'?: string;
}
/**
 * Error response
 * @export
 * @interface GenericErrorContent
 */
export interface GenericErrorContent {
    /**
     * Debug contains debug information. This is usually not available and has to be enabled.
     * @type {string}
     * @memberof GenericErrorContent
     */
    'debug'?: string;
    /**
     * Name is the error name.
     * @type {string}
     * @memberof GenericErrorContent
     */
    'error'?: string;
    /**
     * Description contains further information on the nature of the error.
     * @type {string}
     * @memberof GenericErrorContent
     */
    'error_description'?: string;
    /**
     * Message contains the error message.
     * @type {string}
     * @memberof GenericErrorContent
     */
    'message'?: string;
    /**
     * Code represents the error status code (404, 403, 401, ...).
     * @type {number}
     * @memberof GenericErrorContent
     */
    'status_code'?: number;
}
b
A message from kapa.ai
Based on the type declarations you've shared, I can now explain the difference between
GenericError
and
GenericErrorContent
in the Ory TypeScript SDK: ## GenericError vs GenericErrorContent The
GenericError
interface represents the top-level error response structure returned by many Ory APIs, particularly those in the project/OAuth2 family. It contains: - `code`: The HTTP status code (e.g., 404) - `debug`: Debug information (often not exposed) - `details`: Additional error details - `error`: A nested error object of type
GenericErrorContent
- `id`: The error ID for tracking - `message`: The error message (this is the only required field) - `reason`: Human-readable reason for the error - `request`: Request ID for tracing (often a UUID) - `status`: Status description The
GenericErrorContent
interface represents the nested error object that can appear within the
error
field of
GenericError
. It contains: - `debug`: Debug information (when enabled) - `error`: The error name - `error_description`: Further information about the error - `message`: The error message - `status_code`: The HTTP status code This structure matches what we discussed previously - Ory uses two slightly different error formats across its APIs. The
GenericError
interface accommodates both formats by including the
error
field of type
GenericErrorContent
, which represents the nested error structure used in some Ory APIs like the identity/frontend APIs. This design allows the SDK to handle both error formats consistently, regardless of which Ory API you're interacting with.
b
@chilly-king-10285 Which error type should I rely on in my application?
b
A message from kapa.ai
Based on the information in the knowledge sources and our previous discussion about Ory's error structures, I can provide guidance on which error type to rely on in your application. When handling errors from Ory APIs in your application, you should rely on the
GenericError
type as your primary error handling structure. This is because: 1. The
GenericError
type is more comprehensive and encompasses both error formats used across Ory's different APIs. 2. The
GenericError
interface includes the
error
field which can contain a
GenericErrorContent
object, allowing it to handle both the top-level error format and the nested error format. 3. The only required field in
GenericError
is the
message
field, which will always be present in error responses, making it a reliable property to check. When implementing error handling in your application, you might want to follow this approach:
Copy code
typescript try {   // API call using Ory SDK } catch (err) {   // Cast to GenericError if using TypeScript   const error = err as GenericError;      // Always available   console.log(error.message);      // Check for nested error content   if (error.error) {     console.log(error.error.error_description);   }      // Other optional fields   if (error.reason) {     console.log(error.reason);   } }
For UI error handling specifically, Ory Kratos provides different levels of errors (request level, method level, and field level) as mentioned in the [Integration basics documentation](https://www.ory.sh/docs/kratos/bring-your-own-ui/custom-ui-basic-integration#error-handling). For these UI-specific errors, you'll need to handle them according to their specific structure as shown in that documentation.