handleError method

  1. @override
ErrorDetail handleError(
  1. String? errorId,
  2. ErrorDetail? error
)

Returns the ErrorDetail after manipulating an existing one.

Method that handles errors by parsing the raw response or custom input.

If other errors are to be added, then they will be handled here as if-else or switch-case.

Each error needs to be supplied an errorId, or null if a Generic Error were to be returned.

If error is passed, it can be manipulated within this method and return the modified ErrorDetail object.

Implementation

@override
ErrorDetail handleError(String? errorId, ErrorDetail? error) {
  error ??= RenovationController.genericError(error);

  if (error.info?.data == null) {
    error = handleError(null, error);
  } else {
    switch (errorId) {
      case 'verify_otp':
        errorId = error.info?.data?.status;
        error = handleError(errorId, error);
        break;
      case 'invalid_pin':
        error
          ..type = RenovationError.AuthenticationError
          ..title = 'Wrong OTP'
          ..description = 'Entered OTP is wrong'
          ..info = ((error.info ?? Information())
            ..httpCode = 401
            ..cause = 'Wrong OTP entered'
            ..suggestion =
                'Enter correct OTP received by SMS or generate a new OTP');
        break;
      case 'user_not_found':
      case 'no_linked_user':
      case 'no_pin_for_mobile':
        error
          ..type = RenovationError.NotFoundError
          ..title = 'User not found'
          ..info = ((error.info ?? Information())
            ..httpCode = 404
            ..cause =
                'User is either not registered or does not have a mobile number'
            ..suggestion = 'Create user or add a mobile number');
        break;

      case 'pin_login':
        error
          ..title = 'Incorrect Pin'
          ..info = ((error.info ?? Information())
            ..cause = 'Wrong PIN is entered'
            ..suggestion = 'Re-enter the PIN correctly');
        break;

      case 'login':
        if (error.info?.data.message is Map) {
          switch (error.info?.data.message['message']) {
            case 'User disabled or missing':
              error = handleError('login_user_non_exist', error);
              break;
            case 'Incorrect password':
              error = handleError('login_incorrect_password', error);
              break;
            default:
              error = handleError(null, error);
          }
        } else {
          error = handleError(null, error);
        }
        break;
      case 'login_incorrect_password':
        error
          ..title = 'Incorrect Password'
          ..type = RenovationError.AuthenticationError
          ..info = ((error.info ?? Information())
            ..cause = error.info?.data.message['message']
            ..suggestion = 'Create the new user or enable it if disabled');
        break;
      case 'login_user_non_exist':
        error
          ..title = 'User not found'
          ..type = RenovationError.NotFoundError
          ..info = ((error.info ?? Information())
            ..httpCode = 404
            ..cause = error.info?.data.message['message']
            ..suggestion = 'Create the new user or enable it if disabled');
        break;
      case 'send_otp':
        if (error.type != RenovationError.BackendSettingError) {
          error = handleError(null, error);
        }
        break;

      case 'change_pwd':
        if (error.type == RenovationError.AuthenticationError) {
          error
            ..title = 'Invalid Password'
            ..info = ((error.info ?? Information())
              ..cause = 'Wrong old password'
              ..suggestion =
                  'Check that the current password is correct, or reset the password');
        } else if (error.info?.httpCode == 417 &&
            (error.info?.data.serverMessages as String)
                .contains('Invalid Password')) {
          error
            ..title = 'Weak Password'
            ..info = ((error.info ?? Information())
              ..cause = 'Password does not pass the policy'
              ..suggestion =
                  'Use stronger password, including uppercase, digits and special characters');
        }
        break;
      // No specific errors
      case 'get_current_user_roles':
      case 'logout':
      default:
        error = RenovationController.genericError(error);
    }
  }
  return error;
}