Interseptor for Chopper Client
Thu Oct 31 2024 06:38:16 GMT+0000 (Coordinated Universal Time)
Saved by
@Samuel1347
import 'dart:async';
import 'package:chopper/chopper.dart';
import 'dart:io';
import 'api_errors.dart';
class ErrorHandlerInterceptor implements Interceptor {
@override
FutureOr<Response<BodyType>> intercept<BodyType>(
Chain<BodyType> chain) async {
try {
final response = await chain.proceed(chain.request);
if (!response.isSuccessful) {
final statusCode = response.statusCode;
switch (statusCode) {
case 400:
case 403:
case 405:
case 409:
throw ExceptionWithMessage("Ошибка: ${response.error.toString()}");
case 401:
throw UnauthorisedException();
default:
throw Exception("Неизвестная ошибка: ${response.error}");
}
}
return response;
} on SocketException {
throw const ExceptionWithMessage(
"Сетевая ошибка: Проверьте подключение.");
} on ExceptionWithMessage catch (e) {
throw ExceptionWithMessage("Сообщение об ошибке: ${e.message}");
} on Exception catch (e) {
throw Exception("Общая ошибка: $e");
}
}
}
content_copyCOPY
Comments