mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 17:47:28 +00:00

Refactor DefaultNavBar widget to update page index on item tap. Update ServerFailure class to handle 400 status code with list of errors.
81 lines
2.3 KiB
Dart
81 lines
2.3 KiB
Dart
import 'package:dio/dio.dart';
|
|
|
|
abstract class Failure {
|
|
final String errMessage;
|
|
|
|
Failure(this.errMessage);
|
|
}
|
|
|
|
class ServerFailure extends Failure {
|
|
ServerFailure(super.errMessage);
|
|
|
|
@override
|
|
String toString() {
|
|
return errMessage;
|
|
}
|
|
|
|
factory ServerFailure.fromDioError(DioException dioError) {
|
|
switch (dioError.type) {
|
|
case DioExceptionType.connectionTimeout:
|
|
return ServerFailure("Connection timeout with the Server.");
|
|
case DioExceptionType.sendTimeout:
|
|
return ServerFailure("Send timeout with the Server.");
|
|
|
|
case DioExceptionType.receiveTimeout:
|
|
return ServerFailure("Receive timeout with the Server.");
|
|
|
|
case DioExceptionType.badCertificate:
|
|
return ServerFailure("Bad certificate!");
|
|
|
|
case DioExceptionType.badResponse:
|
|
{
|
|
// var document = parser.parse(dioError.response!.data.toString());
|
|
// var message = document.body!.text;
|
|
return ServerFailure.fromResponse(
|
|
dioError.response!.statusCode!, dioError.response!.data['message']
|
|
// message
|
|
);
|
|
}
|
|
case DioExceptionType.cancel:
|
|
return ServerFailure("The request to ApiServer was canceled");
|
|
|
|
case DioExceptionType.connectionError:
|
|
return ServerFailure("No Internet Connection");
|
|
|
|
case DioExceptionType.unknown:
|
|
return ServerFailure("Unexpected Error, Please try again!");
|
|
|
|
default:
|
|
return ServerFailure("Unexpected Error, Please try again!");
|
|
}
|
|
}
|
|
|
|
factory ServerFailure.fromResponse(int statusCode, dynamic response) {
|
|
if (statusCode == 401 || statusCode == 403) {
|
|
return ServerFailure(response);
|
|
} else if (statusCode == 400) {
|
|
//response is list of errors
|
|
List<String> errors = [];
|
|
response.forEach((element) {
|
|
errors.add(element);
|
|
});
|
|
return ServerFailure(errors.join('\n'));
|
|
} else if (statusCode == 404) {
|
|
return ServerFailure("Your request not found, Please try later!");
|
|
} else if (statusCode == 500) {
|
|
return ServerFailure(response);
|
|
} else {
|
|
return ServerFailure("Opps there was an Error, Please try again!");
|
|
}
|
|
}
|
|
}
|
|
|
|
class ResponseFailure extends Failure {
|
|
ResponseFailure(super.errMessage);
|
|
|
|
@override
|
|
String toString() {
|
|
return errMessage;
|
|
}
|
|
}
|