| title | Error Handling Patterns |
|---|---|
| description | Consistent error handling strategies for ObjectStack Protocol |
Consistent error handling strategies for ObjectStack Protocol
Errors from schema validation or business logic validation.
{
"success": false,
"error": {
"code": "VALIDATION_ERROR",
"message": "Validation failed for field 'email'",
"details": {
"field": "email",
"constraint": "format",
"value": "invalid-email",
"expected": "valid email format"
}
}
}Resource does not exist.
{
"success": false,
"error": {
"code": "NOT_FOUND",
"message": "Record not found",
"details": {
"object": "customer_account",
"id": "acc_999"
}
}
}Insufficient permissions to perform operation.
{
"success": false,
"error": {
"code": "FORBIDDEN",
"message": "Insufficient permissions to delete this record",
"details": {
"required": "DELETE",
"object": "customer_account",
"recordId": "acc_123"
}
}
}Resource conflicts (e.g., uniqueness violations).
{
"success": false,
"error": {
"code": "CONFLICT",
"message": "Email already exists",
"details": {
"field": "email",
"constraint": "unique",
"value": "user@example.com"
}
}
}All errors follow the ApiError schema:
export const ApiErrorSchema = z.object({
code: z.string(),
message: z.string(),
details: z.record(z.any()).optional(),
stack: z.string().optional(), // Only in development
});BAD_REQUEST(400) - Invalid requestUNAUTHORIZED(401) - Authentication requiredFORBIDDEN(403) - Insufficient permissionsNOT_FOUND(404) - Resource not foundCONFLICT(409) - Resource conflictVALIDATION_ERROR(422) - Validation failedINTERNAL_ERROR(500) - Server error
FIELD_REQUIRED- Required field missingFIELD_INVALID- Field validation failedDUPLICATE_VALUE- Uniqueness constraint violatedREFERENCE_INVALID- Lookup reference invalidWORKFLOW_ERROR- Workflow execution failedPERMISSION_DENIED- Specific permission denied
// ✅ Good - Includes context
{
"code": "VALIDATION_ERROR",
"message": "Field 'email' must be a valid email address",
"details": {
"field": "email",
"value": "invalid-email",
"constraint": "format"
}
}
// ❌ Bad - No context
{
"code": "VALIDATION_ERROR",
"message": "Invalid input"
}Always return errors in the standard envelope:
{
"success": false,
"error": { /* ApiError */ },
"metadata": { /* request metadata */ }
}// ✅ Good - Safe error message
{
"code": "UNAUTHORIZED",
"message": "Invalid credentials"
}
// ❌ Bad - Leaks information
{
"code": "UNAUTHORIZED",
"message": "Password incorrect for user john@example.com"
}if (process.env.NODE_ENV === 'development') {
error.stack = err.stack;
}