Overview
The OdontologyApp API uses standard HTTP status codes to indicate the success or failure of requests. All error responses include a JSON body with error details.Standard Error Response Format
Most errors follow this structure:A human-readable error message, typically in Spanish
An optional error code for programmatic handling (e.g., “FORBIDDEN”)
Validation Errors
Validation errors (using Zod schema validation) include anerrors array:
HTTP Status Codes
The API uses the following HTTP status codes:Success Codes (2xx)
Meaning: Request succeededUsed for: Successful GET, POST, PUT, PATCH, DELETE operationsExample:
Client Error Codes (4xx)
Meaning: Invalid request dataCommon causes:
- Missing required fields
- Invalid data format
- Validation errors
- Duplicate entries (email, cedula)
Meaning: Authentication required or failedCommon causes:Solution: Log in via
- No session cookie present
- Session expired
- Invalid credentials
- User not found
/api/auth/login to obtain a valid sessionMeaning: Authenticated but lacking required permissionsCommon causes:Solution: Contact an administrator to request the necessary permissions
- User role doesn’t have permission for this action
- Attempting to access admin-only endpoints
Meaning: Resource not foundCommon causes:
- Invalid endpoint URL
- Resource ID doesn’t exist
Server Error Codes (5xx)
Meaning: Unexpected server errorCommon causes:Action: Check server logs for detailed error information. The error is logged to the console.
- Database connection failure
- Unhandled exceptions
- Stored procedure errors
Common Error Scenarios
Authentication Errors
Missing Credentials (400)
Missing Credentials (400)
Endpoint: Solution: Include both
POST /api/auth/loginTrigger: Username or password not providedusername and password in request bodyUser Not Found (401)
User Not Found (401)
Endpoint: Solution: Verify the username is correct
POST /api/auth/loginTrigger: Username doesn’t exist in databaseIncorrect Password (401)
Incorrect Password (401)
Endpoint: Solution: Verify the password is correct
POST /api/auth/loginTrigger: Password doesn’t match stored bcrypt hashNot Authorized (401)
Not Authorized (401)
Permission Errors
Insufficient Permissions (403)
Insufficient Permissions (403)
Endpoint: Any protected endpointTrigger: User lacks required permission (e.g., VIEW_PATIENTS, CREATE_PATIENTS)Solution: Request permission from administrator or use an admin account
Validation Errors
Zod Validation Failed (400)
Zod Validation Failed (400)
Endpoint: Solution: Fix the specified fields according to the schema requirements
POST /api/patients, and others using schema validationTrigger: Request data doesn’t match Zod schema requirementsDuplicate Entry (400)
Duplicate Entry (400)
Endpoint: Solution: Use a different email/cedula or update the existing patient
POST /api/patientsTrigger: Email or cedula already exists in database (MySQL ER_DUP_ENTRY error)Missing Required Field (400)
Missing Required Field (400)
Endpoint: Various endpointsTrigger: Required field or query parameter missingSolution: Include all required fields in the request
Database Errors
Stored Procedure Error (400)
Stored Procedure Error (400)
Endpoint: Various endpointsTrigger: MySQL stored procedure throws error with SQLSTATE 45000Example: A stored procedure might reject invalid data with a custom messageSolution: Follow the error message guidance or check data validity
Database Operation Failed (500)
Database Operation Failed (500)
Endpoint: Any endpoint with database operationsTrigger: Database query fails unexpectedlySolution: Check server logs for detailed error. Verify database connection and stored procedures.
Error Logging
Console Logging
All errors are logged to the console with context:Login Logging
Successful logins are logged to the database viasp_create_log. If login logging fails, it’s caught silently:
Global Error Handler
The application has a global error handler inhooks.server.js:
Best Practices for Error Handling
1. Check Status Codes
Always check the HTTP status code before parsing the response:2. Handle Different Error Types
3. Graceful Degradation
Some endpoints return partial data even on error:4. Retry Logic for Server Errors
For 5xx errors, implement retry logic:Debugging Tips
Check Server Console
All API errors are logged to the server console with context. Check the terminal running the dev server.
Inspect Network Tab
Use browser DevTools Network tab to see the exact request/response, including headers and cookies.
Verify Session Cookie
Check that the
session cookie is being sent with requests. It should appear in the Cookie header.Error Response Reference
400 Bad Request
- Missing required fields
- Validation errors
- Duplicate entries
- Invalid data format
401 Unauthorized
- No session cookie
- Session expired
- Invalid credentials
- User not found
403 Forbidden
- Insufficient permissions
- Role-based access denied
- Admin-only endpoint
500 Server Error
- Database connection failed
- Stored procedure error
- Unhandled exception
- System error
Next Steps
API Overview
Return to the API overview for endpoint documentation
