All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.hibernate.reactive.rest.data.panache.runtime.RestDataPanacheExceptionMapper Maven / Gradle / Ivy

There is a newer version: 3.17.0
Show newest version
package io.quarkus.hibernate.reactive.rest.data.panache.runtime;

import jakarta.ws.rs.core.Response;

import org.hibernate.HibernateException;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;

import io.quarkus.rest.data.panache.RestDataPanacheException;
import io.smallrye.mutiny.CompositeException;

public class RestDataPanacheExceptionMapper {

    private static final Logger LOGGER = Logger.getLogger(RestDataPanacheExceptionMapper.class);

    @ServerExceptionMapper({ RestDataPanacheException.class, CompositeException.class })
    public RestResponse mapExceptions(Exception exception) {
        LOGGER.warnf(exception, "Mapping an unhandled %s", exception.getClass().getSimpleName());
        RestResponse response = throwableToResponse(exception, exception.getMessage());
        if (response == null) {
            response = RestResponse.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), exception.getMessage());
        }

        return response;
    }

    private RestResponse throwableToResponse(Throwable throwable, String message) {
        if (throwable instanceof org.hibernate.exception.ConstraintViolationException
                || throwable instanceof HibernateException) {
            return RestResponse.status(Response.Status.CONFLICT.getStatusCode(), message);
        }

        if (throwable instanceof jakarta.validation.ConstraintViolationException) {
            return RestResponse.status(Response.Status.BAD_REQUEST.getStatusCode(), message);
        }

        if (throwable instanceof CompositeException) {
            CompositeException compositeException = (CompositeException) throwable;
            for (Throwable cause : compositeException.getCauses()) {
                RestResponse response = throwableToResponse(cause, message);
                if (response != null) {
                    return response;
                }
            }

        } else if (throwable.getCause() != null) {
            return throwableToResponse(throwable.getCause(), message);
        }

        return null;
    };
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy