getErrorMessage method

void getErrorMessage(
  1. Exception error,
  2. bool handleAuth
)

Implementation

void getErrorMessage(Exception error, bool handleAuth) {
  String? errorTitle;
  String? errorBody;
  if (error is DioError) {
    switch (error.type) {
      case DioErrorType.response:
        final dioError = error;
        if (dioError is DioError) {
          final title = (error.response?.data is Map)
              ? error.response?.data['message']
              : '';

          errorTitle = title != null && title.toString().trim().isNotEmpty
              ? title.toString()
              : error.response?.statusMessage;
          errorBody = (error.response?.data is Map) &&
                  dioError.response?.data['errors'] != null
              ? getErrorBody(dioError.response?.data['errors'])
              : '';
          if (dioError.response?.statusCode == 401 && handleAuth) {
            onUnauthenticated?.call();
          }
        }
        break;
      case DioErrorType.cancel:
        errorTitle = 'Request to API server was cancelled';
        break;
      case DioErrorType.connectTimeout:
        errorTitle = 'Connection timeout with API server';
        break;
      case DioErrorType.receiveTimeout:
        errorTitle = 'Receive timeout in connection with API server';
        break;
      case DioErrorType.sendTimeout:
        errorTitle = 'Send timeout with server';
        break;
      case DioErrorType.other:
        errorTitle = error.toString();
        break;
    }
  } else {
    errorTitle = 'Unexpected error occurred ${error.toString()}';
  }
  onRegularError?.call(errorTitle.toString(), errorBody, error);

  if (debugMode) {
    printWrapped(errorTitle.toString());
    printWrapped(errorBody.toString());
  }
}