Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

import 'package:dio/dio.

dart';

class DioException {

String getDioError(DioError dioError) {


switch (dioError.type) {

case DioErrorType.response:
return _handleStatusCode(dioError.response!);
case DioErrorType.other:
if (dioError.message.contains('SocketException')) {
return 'No Internet.';
}else{
return 'Unexpected error occurred.';
}
default:
return 'Something went wrong';

}
}

String _handleStatusCode(Response response) {


switch (response.statusCode) {
case 400:
return 'Bad request.';
case 401:
return 'Authentication failed.';
case 403:
return 'The authenticated user is not allowed to access the specified API
endpoint.';
case 404:
return 'The requested resource does not exist.';
case 405:
return 'Method not allowed. Please check the Allow header for the allowed
HTTP methods.';
case 415:
return 'Unsupported media type. The requested content type or version
number is invalid.';
case 422:
return 'Data validation failed.';
case 429:
return 'Too many requests.';
case 500:
return 'Internal server error.';
default:
return 'Oops something went wrong!';
}
}

class AuthExceptionHandler {
static handleException(e) {
print(e.code);
var status;
switch (e.code) {
case "ERROR_INVALID_EMAIL":
status = AuthResultStatus.invalidEmail;
break;
case "ERROR_WRONG_PASSWORD":
status = AuthResultStatus.wrongPassword;
break;
case "ERROR_USER_NOT_FOUND":
status = AuthResultStatus.userNotFound;
break;
case "ERROR_USER_DISABLED":
status = AuthResultStatus.userDisabled;
break;
case "ERROR_TOO_MANY_REQUESTS":
status = AuthResultStatus.tooManyRequests;
break;
case "ERROR_OPERATION_NOT_ALLOWED":
status = AuthResultStatus.operationNotAllowed;
break;
case "ERROR_EMAIL_ALREADY_IN_USE":
status = AuthResultStatus.emailAlreadyExists;
break;
default:
status = AuthResultStatus.undefined;
}
return status;
}

///
/// Accepts AuthExceptionHandler.errorType
///
static generateExceptionMessage(exceptionCode) {
String errorMessage;
switch (exceptionCode) {
case AuthResultStatus.invalidEmail:
errorMessage = "Your email address appears to be malformed.";
break;
case AuthResultStatus.wrongPassword:
errorMessage = "Your password is wrong.";
break;
case AuthResultStatus.userNotFound:
errorMessage = "User with this email doesn't exist.";
break;
case AuthResultStatus.userDisabled:
errorMessage = "User with this email has been disabled.";
break;
case AuthResultStatus.tooManyRequests:
errorMessage = "Too many requests. Try again later.";
break;
case AuthResultStatus.operationNotAllowed:
errorMessage = "Signing in with Email and Password is not enabled.";
break;
case AuthResultStatus.emailAlreadyExists:
errorMessage =
"The email has already been registered. Please login or reset your
password.";
break;
default:
errorMessage = "An undefined Error happened.";
}
return errorMessage;
}
}

You might also like