0% found this document useful (0 votes)
10K views

Dio Error

This document defines classes for handling exceptions from the Dio HTTP client library and authentication exceptions. The DioException class contains methods for parsing DioError objects to return user-friendly error messages. The AuthExceptionHandler class contains static methods for parsing authentication exception codes and messages.

Uploaded by

Babyn Shrestha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10K views

Dio Error

This document defines classes for handling exceptions from the Dio HTTP client library and authentication exceptions. The DioException class contains methods for parsing DioError objects to return user-friendly error messages. The AuthExceptionHandler class contains static methods for parsing authentication exception codes and messages.

Uploaded by

Babyn Shrestha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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