TF-1115 Add RemoteExceptionThrower
This commit is contained in:
@@ -22,6 +22,7 @@ import 'package:tmail_ui_user/features/manage_account/data/network/manage_accoun
|
||||
import 'package:tmail_ui_user/features/manage_account/data/network/vacation_api.dart';
|
||||
import 'package:tmail_ui_user/features/session/data/network/session_api.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart';
|
||||
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
|
||||
|
||||
class NetworkBindings extends Bindings {
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import 'package:core/core.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:tmail_ui_user/features/base/base_bindings.dart';
|
||||
import 'package:tmail_ui_user/features/session/data/datasource/session_datasource.dart';
|
||||
@@ -7,6 +6,7 @@ import 'package:tmail_ui_user/features/session/data/network/session_api.dart';
|
||||
import 'package:tmail_ui_user/features/session/data/repository/session_repository_impl.dart';
|
||||
import 'package:tmail_ui_user/features/session/domain/repository/session_repository.dart';
|
||||
import 'package:tmail_ui_user/features/session/domain/usecases/get_session_interactor.dart';
|
||||
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
|
||||
|
||||
class SessionBindings extends BaseBindings {
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
abstract class ExceptionThrower {
|
||||
void throwException(dynamic exception);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/error/error_type.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/error/method/error_method_response.dart';
|
||||
|
||||
abstract class RemoteException with EquatableMixin implements Exception {
|
||||
static const connectError = 'Connect error';
|
||||
|
||||
final String? message;
|
||||
final int? code;
|
||||
|
||||
const RemoteException({this.code, this.message});
|
||||
}
|
||||
|
||||
class UnknownError extends RemoteException {
|
||||
const UnknownError({int? code, String? message}) : super(code: code, message: message);
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class ConnectError extends RemoteException {
|
||||
const ConnectError() : super(message: RemoteException.connectError);
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class MethodLevelErrors extends RemoteException {
|
||||
final ErrorType type;
|
||||
|
||||
const MethodLevelErrors(
|
||||
this.type,
|
||||
{String? message}
|
||||
) : super(message: message);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [type, message];
|
||||
}
|
||||
|
||||
class CannotCalculateChangesMethodResponseException extends MethodLevelErrors {
|
||||
CannotCalculateChangesMethodResponseException({String? message}) : super(ErrorMethodResponse.cannotCalculateChanges, message: message);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/error/method/error_method_response.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/error/method/exception/error_method_response_exception.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
|
||||
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';
|
||||
import 'package:tmail_ui_user/main/exceptions/remote_exception.dart';
|
||||
|
||||
class RemoteExceptionThrower extends ExceptionThrower {
|
||||
|
||||
@override
|
||||
void throwException(dynamic exception) {
|
||||
if (exception is DioError) {
|
||||
switch (exception.type) {
|
||||
case DioErrorType.connectTimeout:
|
||||
throw const ConnectError();
|
||||
default:
|
||||
if (exception.response?.statusCode == 502) {
|
||||
throw BadGateway();
|
||||
} else {
|
||||
throw UnknownError(
|
||||
code: exception.response?.statusCode,
|
||||
message: exception.response?.statusMessage);
|
||||
}
|
||||
}
|
||||
} else if (exception is ErrorMethodResponseException) {
|
||||
final errorResponse = exception.errorResponse as ErrorMethodResponse;
|
||||
if (errorResponse is CannotCalculateChangesMethodResponse) {
|
||||
throw CannotCalculateChangesMethodResponseException();
|
||||
} else {
|
||||
throw MethodLevelErrors(
|
||||
errorResponse.type,
|
||||
message: errorResponse.description);
|
||||
}
|
||||
} else {
|
||||
throw UnknownError(message: exception.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user