Skip to content

Commit

Permalink
chore: better error handling in AliceChopperAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Jun 9, 2024
1 parent da3afa1 commit 554b341
Showing 1 changed file with 51 additions and 22 deletions.
73 changes: 51 additions & 22 deletions packages/alice_chopper/lib/alice_chopper_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert' show utf8;
import 'dart:io' show HttpHeaders;

import 'package:alice/core/alice_adapter.dart';
import 'package:alice/core/alice_utils.dart';
import 'package:alice/model/alice_http_call.dart';
import 'package:alice/model/alice_http_error.dart';
import 'package:alice/model/alice_http_request.dart';
Expand Down Expand Up @@ -57,33 +58,61 @@ class AliceChopperAdapter with AliceAdapter implements Interceptor {
..response = AliceHttpResponse(),
);

final Response<BodyType> response = await chain.proceed(chain.request);
try {
final Response<BodyType> response = await chain.proceed(chain.request);

aliceCore.addResponse(
AliceHttpResponse()
..status = response.statusCode
..body = response.body ?? ''
..size = switch (response.body) {
dynamic body when body is String => utf8.encode(body).length,
dynamic body when body is List<int> => body.length,
dynamic body when body == null => 0,
_ => utf8.encode(body.toString()).length,
}
..time = DateTime.now()
..headers = <String, String>{
for (final MapEntry<String, String> entry in response.headers.entries)
entry.key: entry.value
},
requestId,
);
aliceCore.addResponse(
AliceHttpResponse()
..status = response.statusCode
..body = response.body ?? ''
..size = switch (response.body) {
dynamic body when body is String => utf8.encode(body).length,
dynamic body when body is List<int> => body.length,
dynamic body when body == null => 0,
_ => utf8.encode(body.toString()).length,
}
..time = DateTime.now()
..headers = <String, String>{
for (final MapEntry<String, String> entry
in response.headers.entries)
entry.key: entry.value
},
requestId,
);

if (!response.isSuccessful || response.error != null) {
if (!response.isSuccessful || response.error != null) {
aliceCore.addError(
AliceHttpError()..error = response.error,
requestId,
);
}

return response;
} catch (error, stackTrace) {
/// Log error to Alice log
AliceUtils.log(error.toString());

/// Add empty response to Alice core
aliceCore.addResponse(
AliceHttpResponse()
..status = 500
..body = error.toString()
..size = utf8.encode(error.toString()).length
..time = DateTime.now()
..headers = <String, String>{},
requestId,
);

/// Add error to Alice core
aliceCore.addError(
AliceHttpError()..error = response.error,
AliceHttpError()
..error = error
..stackTrace = stackTrace,
requestId,
);
}

return response;
/// Rethrow error
rethrow;
}
}
}

0 comments on commit 554b341

Please sign in to comment.