Overview
The DonaSF API implements consistent error handling across all layers, from database operations through business logic to HTTP responses. Understanding these patterns is essential for proper API integration and troubleshooting.Error Response Format
All API endpoints return errors in a consistent format:Standard Error Response
Success Response with Result Flag
Many endpoints return a result flag to indicate success/failure:HTTP Status Codes
The API uses standard HTTP status codes to indicate the outcome of requests:200 OK
Request succeeded. Returns data or success message.
400 Bad Request
Invalid input, missing parameters, or business rule violation.
404 Not Found
Requested resource does not exist in the database.
500 Internal Server Error
Unhandled exception or database connection failure (implicit).
Error Handling by Layer
Controller Layer (API)
Controllers validate input and transform business layer errors into HTTP responses.Input Validation Errors
Missing Required Parameters:Business Logic Errors
Error from Business Layer:Data Not Found Errors
Empty Result Set:Exception Handling
Try-Catch Wrapper:Business Logic Layer
Business logic classes catch exceptions and return errors viaInfoCompartidaCapas.
Standard Pattern
Error Propagation
Errors from the data layer are caught and propagated throughInfoCompartidaCapas.error:
Data Access Layer
The data layer handles SQL exceptions and connection errors.Connection Errors
SQL Exceptions
SQL Server exceptions (constraint violations, deadlocks, etc.) are thrown and caught in the business layer.Common Error Scenarios
1. Invalid Input Parameters
2. Resource Not Found
3. Duplicate Entry
4. Database Connection Failure
5. Foreign Key Constraint Violation
6. Data Operation Failure
7. Server Communication Error
Error Handling Best Practices
Check Business Layer Errors First
Check Business Layer Errors First
Always check
resultados.error immediately after calling business logic methods:Validate DataTable Results
Validate DataTable Results
Always check if DataTable is null or empty before accessing rows:
Use Field<T> for BIT Columns
Use Field<T> for BIT Columns
When reading BIT (boolean) columns from SQL Server, use
Field<bool>() to avoid casting errors:Wrap Complex Operations in Try-Catch
Wrap Complex Operations in Try-Catch
Use try-catch blocks in controllers for operations with multiple steps:
Return Meaningful Error Messages
Return Meaningful Error Messages
Provide context in error messages to help clients understand what went wrong:
Handle Null Responses
Handle Null Responses
Check for null responses from business layer before processing:
Debugging Errors
Reading Error Messages
Error messages follow specific patterns based on the source:Business logic or validation error from the business layer
Spanish error message indicating a failed operation or missing data
Exception caught during a specific operation (e.g., “Error al buscar”)
Missing or invalid input parameter
Common SQL Server Errors
| Error Pattern | Cause | Solution |
|---|---|---|
UNIQUE KEY constraint | Duplicate value in unique column | Check for existing records before insert |
FOREIGN KEY constraint | Referenced record doesn’t exist | Verify parent record exists |
CHECK constraint | Value violates check rule | Review constraint rules (e.g., folio_inicio < folio_fin) |
Cannot insert NULL | Required field is null | Provide all required fields |
Connection timeout | Database unreachable | Check connection string and network |
Related Documentation
- System Architecture - Error handling across layers
- Data Model - Database constraints and validation
- API Reference - Endpoint-specific error responses