28 lines
834 B
Dart
28 lines
834 B
Dart
import 'package:dio/dio.dart';
|
|
|
|
import '../../models/pb/errors/errors.pb.dart' as kerrors;
|
|
|
|
class KratosErrorInterceptor extends Interceptor {
|
|
@override
|
|
void onError(DioException err, ErrorInterceptorHandler handler) {
|
|
final response = err.response;
|
|
final raw = response?.data;
|
|
if (response != null && raw is List<int> && raw.isNotEmpty) {
|
|
try {
|
|
final status = kerrors.Status.fromBuffer(raw);
|
|
if (status.code != 0 || status.reason.isNotEmpty) {
|
|
response.data = <String, dynamic>{
|
|
'code': status.code,
|
|
'reason': status.reason,
|
|
'message': status.message,
|
|
'metadata': status.metadata,
|
|
};
|
|
}
|
|
} catch (_) {
|
|
// A non-Kratos error body keeps its original representation.
|
|
}
|
|
}
|
|
handler.next(err);
|
|
}
|
|
}
|