Error Response Format
All API errors return a consistent JSON structure defined by theApiErrorDto class.
ApiErrorDto Structure
ISO 8601 timestamp when the error occurred (with timezone offset)
HTTP status code (e.g., 400, 404, 500)
HTTP status reason phrase (e.g., “Bad Request”, “Not Found”)
Human-readable error message describing what went wrong
The request URI that caused the error
Example Error Response
HTTP Status Codes
The Secure Link API uses the following HTTP status codes:Success Codes
| Code | Status | Description |
|---|---|---|
200 | OK | Request succeeded (GET requests, stats endpoints) |
201 | Created | Link created successfully (POST requests) |
204 | No Content | Link revoked successfully (DELETE requests) |
302 | Found | Redirect link resolved (when Accept header is not JSON) |
Client Error Codes
| Code | Status | Description |
|---|---|---|
400 | Bad Request | Validation error or malformed request |
401 | Unauthorized | Password required or invalid password |
404 | Not Found | Link not found |
410 | Gone | Link expired, revoked, or view limit reached |
Server Error Codes
| Code | Status | Description |
|---|---|---|
500 | Internal Server Error | Unexpected server error occurred |
Error Scenarios
Link Not Found (404)
Returned when the requested short code does not exist in the database.Link Expired (410)
Returned when the link has expired (time-based or view limit).- Time-based expiration (
expiresAtpassed) - View limit reached (
viewCount >= maxViews)
Link Revoked (410)
Returned when the link has been manually revoked.Password Required (401)
Returned when accessing a password-protected link without providing the password.Invalid Password (401)
Returned when the provided password does not match the link’s password.Validation Error (400)
Returned when request validation fails (e.g., invalid URL, invalid date format).Internal Server Error (500)
Returned when an unexpected error occurs. Includes a unique error ID for tracking.Error Tracing
Correlation IDs
Every request is assigned a unique correlation ID via theX-Correlation-Id header. This ID is:
- Automatically generated for each request
- Included in all server logs using MDC (Mapped Diagnostic Context)
- Used to trace requests across the application
The correlation ID appears in square brackets in every log entry, making it easy to trace a specific request through the entire system.
Error Reference IDs
For500 Internal Server Error responses, a unique errorId (UUID) is generated and included in:
- The error response message
- Server logs
Audit Trail
All link access attempts (successful or failed) are recorded in the audit system with the following information:- Short code
- Access result (SUCCESS, NOT_FOUND, EXPIRED, REVOKED, PASSWORD_REQUIRED, INVALID_PASSWORD, etc.)
- IP address
- User agent
- Timestamp
- Security monitoring
- Access analytics
- Debugging access issues
Exception Handling
TheGlobalExceptionHandler class handles all exceptions globally using Spring’s @RestControllerAdvice.
Handled Exception Types
- Generic Exceptions → 500 Internal Server Error
- MethodArgumentNotValidException → 400 Bad Request
- ResponseStatusException → Status code from exception
Best Practices
- Log Correlation IDs: Always include the correlation ID when reporting issues
- Save Error IDs: For 500 errors, save the error reference ID from the message
- Check Audit Logs: Use the audit trail to investigate access failures
- Monitor Status Codes: Track 410 responses to understand link expiration patterns
- Validate Early: Client-side validation can prevent most 400 errors
