getValidationMessages method

Map<String, List<String>> getValidationMessages(
  1. DioError error
)

Resolves validation errors from DioError response

Implementation

Map<String, List<String>> getValidationMessages(DioError error) {
  try {
    if (error.response?.data != null) {
      Map<String, List<String>> errorsMap = {};

      if (restApiClientOptions.resolveValidationErrorsMap != null) {
        errorsMap =
            restApiClientOptions.resolveValidationErrorsMap!(error.response);
      } else {
        error.response!.data['validationErrors']?.forEach((key, value) =>
            errorsMap[key] =
                value?.map<String>((x) => x as String)?.toList());
        if (error.response!.data['errors'] != null) {
          final errors = MapEntry<String, List<String>>(
              '',
              error.response!.data['errors']
                      ?.map<String>((error) => error as String)
                      ?.toList() ??
                  ['']);
          errorsMap.addAll(Map.fromEntries([errors]));
        }
      }

      return errorsMap;
    }
  } catch (e) {
    print(e);
  }
  return {};
}