Skip to content

Instantly share code, notes, and snippets.

@Vanethos
Created September 5, 2019 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vanethos/e98fc087d6a91da97b515536204b6def to your computer and use it in GitHub Desktop.
Save Vanethos/e98fc087d6a91da97b515536204b6def to your computer and use it in GitHub Desktop.
class LoggingInterceptors extends Interceptor {
@override
FutureOr<dynamic> onRequest(RequestOptions options) {
print(
"--> ${options.method != null ? options.method.toUpperCase() : 'METHOD'} ${"" + (options.baseUrl ?? "") + (options.path ?? "")}");
print("Headers:");
options.headers.forEach((k, v) => print('$k: $v'));
if (options.queryParameters != null) {
print("queryParameters:");
options.queryParameters.forEach((k, v) => print('$k: $v'));
}
if (options.data != null) {
print("Body: ${options.data}");
}
print(
"--> END ${options.method != null ? options.method.toUpperCase() : 'METHOD'}");
return options;
}
@override
FutureOr<dynamic> onError(DioError dioError) {
print(
"<-- ${dioError.message} ${(dioError.response?.request != null ? (dioError.response.request.baseUrl + dioError.response.request.path) : 'URL')}");
print(
"${dioError.response != null ? dioError.response.data : 'Unknown Error'}");
print("<-- End error");
}
@override
FutureOr<dynamic> onResponse(Response response) {
print(
"<-- ${response.statusCode} ${(response.request != null ? (response.request.baseUrl + response.request.path) : 'URL')}");
print("Headers:");
response.headers?.forEach((k, v) => print('$k: $v'));
print("Response: ${response.data}");
print("<-- END HTTP");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment