handleErrorMessage function

String handleErrorMessage(
  1. dynamic e, {
  2. String? key,
})

Implementation

String handleErrorMessage(dynamic e, {String? key}) {
  String errorMessageKey = key ?? 'error_messages';

  if (e is DioException) {
    try {
      //if the response data is null, return the default error message
      if (e.response?.data == null ||
          e.response?.data == "" ||
          e.response?.data is! Map) {
        return ErrorHandler.handle(e).failure.message;
      }

      if (e.response?.data[errorMessageKey] is String) {
        //if the response data is a string, return it
        return e.response?.data[errorMessageKey];
      } else if (e.response?.data[errorMessageKey] is List) {
        //if the response data is a list, return the first item as string
        return e.response?.data[errorMessageKey].first as String;
      } else {
        //if the response data is not a string or list, return the default error message
        return ErrorHandler.handle(e).failure.message;
      }
    } catch (e) {
      return ErrorHandler.handle(e).failure.message;
    }
  } else {
    return ErrorHandler.handle(e).failure.message;
  }
}