extractValidationException function

Object extractValidationException(
  1. GraphOperation operation,
  2. dynamic input,
  3. OperationException exception
)

Implementation

Object extractValidationException(GraphOperation operation, dynamic input, OperationException exception) {
  List<GraphQLError> graphQLErrors = [];

  final linkException = exception.linkException;

  graphQLErrors.addAll(exception.graphqlErrors);
  if (linkException is ServerException) {
    if (linkException.originalException?.runtimeType == 'SocketException') {
      return ApiException.socket(linkException.originalException as Object, StackTrace.current);
    }

    var parsedResponse = linkException.parsedResponse;
    if (parsedResponse?.errors?.isNotEmpty == true) {
      graphQLErrors.addAll(parsedResponse!.errors!);
    }
  }

  var validationErrors =
      graphQLErrors.where((element) => element.isValidationError).expand((element) => element.validationErrors).toList();

  if (validationErrors.isNotEmpty) {
    return operation.isMutate
        ? BadRequestException(
            'Validation errors:\n  - ${validationErrors.map((e) => e.toString()).join("\n  -")}', validationErrors)
        : ApiException.response(500, "Invalid data from the server",
            errors: ApiErrorPayload(
                errorType: "Invalid data assertion",
                errors: validationErrors.map((e) {
                  return ApiErrorItem(message: "${e.path.toKey()}: ${e}", body: e.toMap());
                }).toList()));
  } else {
    return ApiException.response(
        500,
        [
          for (var error in exception.graphqlErrors) error.message,
          if (exception.linkException != null) ...[
            exception.linkException!.toString(),
          ],
        ].join('\n'));
  }
}