Reference
Error Handling
Error handling in dnax Framework.
dnax Framework provides a standardized error handling system.
AppError Class
import { AppError, fn } from '@dnax/core';
throw new AppError('Something went wrong', {
code: 'CUSTOM_ERROR',
status: 400,
meta: { details: '...' }
});
Error Response Format
All errors return a consistent JSON format:
{
"message": "Error description",
"code": "ERROR_CODE",
"meta": {}
}
Built-in Error Codes
| Code | Status | Description |
|---|---|---|
TENANT_ID_REQUIRED | 400 | Missing tenant_id |
COLLECTION_REQUIRED | 400 | Missing collection |
ACTION_REQUIRED | 400 | Missing action |
INVALID_JSON_BODY | 400 | Invalid JSON body |
TENANT_NOT_FOUND | 400 | Tenant not found |
COLLECTION_NOT_FOUND | 400 | Collection not found |
ACTION_NOT_FOUND | 400 | Action not found |
AUTH_NOT_ENABLED | 400 | Auth not enabled |
AUTH_HANDLER_NOT_DEFINED | 400 | Auth handler missing |
INVALID_TOKEN_ASSIGNED | 401 | Invalid token |
INVALID_TOKEN | 401 | JWT invalid |
ACCESS_DENIED | 401 | Access denied |
VALIDATION_ERROR | 400 | Schema validation |
INTERNAL_SERVER_ERROR | 500 | Internal error |
Throw Custom Errors
In custom actions:
actions: {
myAction: async ({ data, error }) => {
if (!data.required) {
throw error('Field required', {
code: 'REQUIRED_FIELD',
status: 400,
});
}
},
}
Global Error Handler
Errors are automatically caught and formatted:
try {
// API logic
} catch (err) {
const isAppError = err instanceof AppError;
return c.json({
message: isAppError ? err.message : 'Internal server error',
code: isAppError ? err.code : 'INTERNAL_SERVER_ERROR',
meta: isAppError ? err.meta : undefined,
}, isAppError ? err.status : 500);
}