feat(api): add lightweight Dio request/response logging interceptor#9
feat(api): add lightweight Dio request/response logging interceptor#9
Conversation
📝 WalkthroughWalkthroughA debug-only Dio InterceptorWrapper was added in the DI setup to log request/response/error metadata (excluding bodies) when running in debug mode; a CHANGELOG entry documenting this addition was also added. Changes
Sequence Diagram(s)sequenceDiagram
participant App as App
participant DI as DI setup
participant Dio as Dio Client
participant Interceptor as Debug Interceptor
participant Logger as AppLogger
App->>DI: initialize setupDI()
DI->>Dio: register/debug-add Interceptor
App->>Dio: make HTTP request
Dio->>Interceptor: onRequest(metadata)
Interceptor->>Logger: log request metadata
Dio->>Interceptor: onResponse(metadata)
Interceptor->>Logger: log response metadata
Dio->>Interceptor: onError(metadata)
Interceptor->>Logger: log error metadata
Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
lib/core/di/di.dart (2)
66-71: Consider loggingerror.typefor network-level failuresWhen there's no HTTP response (timeouts, connection refused, cancellation), the log currently emits
x - METHOD URIwith no indication of why it failed. Addingerror.typemakes these cases immediately recognisable.♻️ Proposed fix
onError: (DioException error, handler) { final request = error.requestOptions; - di<AppLogger>().w( - 'x ${error.response?.statusCode ?? '-'} ${request.method} ${request.uri} ${error.message ?? ''}', - ); + logger.w( + 'x ${error.response?.statusCode ?? '-'} [${error.type.name}] ${request.method} ${request.uri} ${error.message ?? ''}', + ); return handler.next(error); },🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/core/di/di.dart` around lines 66 - 71, The onError handler for DioException should include network-level failure info by logging error.type in addition to the existing fields; update the onError block (the DioException error handler that constructs the log in di<AppLogger>().w using request, error.response?.statusCode and error.message) to append or interpolate error.type so timeouts, cancellations and connection failures are identifiable in the log message.
53-75: ResolveAppLoggeronce outside the interceptor callbacks
di<AppLogger>()is called on every request, response, and error. While it is a cheap singleton lookup, capturing it once before theInterceptorsWrapperis cleaner and more idiomatic.♻️ Proposed refactor
if (kDebugMode) { + final logger = di<AppLogger>(); dio.interceptors.add( InterceptorsWrapper( onRequest: (options, handler) { - di<AppLogger>().d('-> ${options.method} ${options.uri}'); + logger.d('-> ${options.method} ${options.uri}'); return handler.next(options); }, onResponse: (response, handler) { - di<AppLogger>().d( + logger.d( '<- ${response.statusCode} ${response.requestOptions.method} ${response.requestOptions.uri}', ); return handler.next(response); }, onError: (DioException error, handler) { final request = error.requestOptions; - di<AppLogger>().w( + logger.w( 'x ${error.response?.statusCode ?? '-'} ${request.method} ${request.uri} ${error.message ?? ''}', ); return handler.next(error); }, ), ); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/core/di/di.dart` around lines 53 - 75, The interceptor repeatedly resolves di<AppLogger>() inside onRequest, onResponse, and onError; capture the AppLogger once before creating the InterceptorsWrapper (e.g., final logger = di<AppLogger>()), then use logger in the onRequest, onResponse, and onError callbacks when adding the InterceptorsWrapper to dio.interceptors via dio.interceptors.add; update references to di<AppLogger>() in those callbacks to use the local logger variable.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@lib/core/di/di.dart`:
- Around line 66-71: The onError handler for DioException should include
network-level failure info by logging error.type in addition to the existing
fields; update the onError block (the DioException error handler that constructs
the log in di<AppLogger>().w using request, error.response?.statusCode and
error.message) to append or interpolate error.type so timeouts, cancellations
and connection failures are identifiable in the log message.
- Around line 53-75: The interceptor repeatedly resolves di<AppLogger>() inside
onRequest, onResponse, and onError; capture the AppLogger once before creating
the InterceptorsWrapper (e.g., final logger = di<AppLogger>()), then use logger
in the onRequest, onResponse, and onError callbacks when adding the
InterceptorsWrapper to dio.interceptors via dio.interceptors.add; update
references to di<AppLogger>() in those callbacks to use the local logger
variable.
…AppLogger once outside the interceptor callbacks
🚀 Summary
Added minimal Dio logging interceptor for development to make HTTP debugging easier and cleaner.
✨ Changes
🧪 Verification
flutter analyze