static Optional mapCloudExceptionToResponseCode()

in service/common/src/main/java/org/apache/polaris/service/exception/IcebergExceptionMapper.java [224:264]


  static Optional<Integer> mapCloudExceptionToResponseCode(Throwable t) {
    if (!(t instanceof S3Exception
        || t instanceof AzureException
        || t instanceof StorageException)) {
      return Optional.empty();
    }

    if (containsAnyAccessDeniedHint(t.getMessage())) {
      return Optional.of(Status.FORBIDDEN.getStatusCode());
    }

    int httpCode = extractHttpCodeFromCloudException(t);
    Status httpStatus = Status.fromStatusCode(httpCode);
    Status.Family httpFamily = Status.Family.familyOf(httpCode);

    if (httpStatus == Status.NOT_FOUND) {
      return Optional.of(Status.BAD_REQUEST.getStatusCode());
    }
    if (httpStatus == Status.UNAUTHORIZED) {
      return Optional.of(Status.FORBIDDEN.getStatusCode());
    }
    if (httpStatus == Status.BAD_REQUEST
        || httpStatus == Status.FORBIDDEN
        || httpStatus == Status.REQUEST_TIMEOUT
        || httpStatus == Status.TOO_MANY_REQUESTS
        || httpStatus == Status.GATEWAY_TIMEOUT) {
      return Optional.of(httpCode);
    }
    if (httpFamily == Status.Family.REDIRECTION) {
      // Currently Polaris doesn't know how to follow redirects from cloud providers, thus clients
      // shouldn't expect it to.
      // This is a 4xx error to indicate that the client may be able to resolve this by changing
      // some data, such as their catalog's region.
      return Optional.of(422);
    }
    if (httpFamily == Status.Family.SERVER_ERROR) {
      return Optional.of(Status.BAD_GATEWAY.getStatusCode());
    }

    return Optional.of(Status.INTERNAL_SERVER_ERROR.getStatusCode());
  }