Exception Handling
Graceful response handling
WebExceptionHandler: centralized@RestControllerAdvicethat captures every API exception and wraps it into the standard envelope. Every handled exception is logged with the request info and trace ID.ApiResponse: standardizes the response format. A normal API response carriesdatawithcode200; an error response carries{code, message, data, error, traceId}—messageis the generic category label of the response code,erroris the user-facing reason (i18n-resolved against the caller’s language at throw time), andtraceIdechoes the request’sX-B3-TraceId(generated server-side when absent) so support can correlate the response with the server logs.- Business errors vs system errors:
BusinessException(code 440) messages are designed to be shown to end users — clients should displayerrordirectly. System errors (code 500+) never carry the raw exception message outside debug mode — clients show the generic message plus thetraceIdreference; in debug mode (X-Debugheader or a debug-enabled environment)errorcarries the full exception text. - Custom Exceptions (inheriting from
BaseException): associated with specificresponseCodevalues and loglevel; the message supports{0}-style placeholders and is translated viaI18nat construction. Throw withthrow new xxxException(...).
Exception Capture and Monitoring
Sentry Integration
Modify the configuration file sentry.properties
Last updated on