public Response toResponse()

in core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/RestServiceExceptionMapper.java [90:163]


    public Response toResponse(final Exception ex) {
        if (ex instanceof NotFoundException) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Exception thrown", ex);
            } else {
                LOG.warn("{} thrown: {}", NotFoundException.class.getSimpleName(), ex.getMessage());
            }
        } else {
            LOG.error("Exception thrown", ex);
        }

        ResponseBuilder builder;

        if (ex instanceof AccessDeniedException) {
            // leaves the default exception processing to Spring Security
            builder = null;
        } else if (ex instanceof SyncopeClientException sce) {
            builder = sce.isComposite()
                    ? getSyncopeClientCompositeExceptionResponse(sce.asComposite())
                    : getSyncopeClientExceptionResponse(sce);
        } else if (ex instanceof DelegatedAdministrationException
                || ExceptionUtils.getRootCause(ex) instanceof DelegatedAdministrationException) {

            builder = builder(ClientExceptionType.DelegatedAdministration, ExceptionUtils.getRootCauseMessage(ex));
        } else if (ex instanceof EntityExistsException || ex instanceof DuplicateException
                || ((ex instanceof PersistenceException || ex instanceof DataIntegrityViolationException)
                && (ex.getCause() instanceof EntityExistsException || ex.getMessage().contains("already exists")))) {

            builder = builder(ClientExceptionType.EntityExists, getPersistenceErrorMessage(
                    ex instanceof PersistenceException || ex instanceof DataIntegrityViolationException
                            ? ex.getCause() : ex));
        } else if (ex instanceof DataIntegrityViolationException || ex instanceof UncategorizedDataAccessException) {
            builder = builder(ClientExceptionType.DataIntegrityViolation, getPersistenceErrorMessage(ex));
        } else if (ex instanceof ConnectorException) {
            builder = builder(ClientExceptionType.ConnectorException, ExceptionUtils.getRootCauseMessage(ex));
        } else if (ex instanceof NotFoundException) {
            builder = builder(ClientExceptionType.NotFound, ExceptionUtils.getRootCauseMessage(ex));
        } else {
            builder = processInvalidEntityExceptions(ex);
            if (builder == null) {
                builder = processBadRequestExceptions(ex);
            }
            // process JAX-RS validation errors
            if (builder == null && ex instanceof final ValidationException validationException) {
                builder = builder(validationEM.toResponse(validationException)).
                        header(RESTHeaders.ERROR_CODE, ClientExceptionType.RESTValidation.name()).
                        header(RESTHeaders.ERROR_INFO, ClientExceptionType.RESTValidation.getInfoHeaderValue(
                                ExceptionUtils.getRootCauseMessage(ex)));
            }
            // process web application exceptions
            if (builder == null && ex instanceof final WebApplicationException webApplicationException) {
                builder = builder(webApplicationException.getResponse()).
                        header(RESTHeaders.ERROR_CODE, ClientExceptionType.Unknown.name()).
                        header(RESTHeaders.ERROR_INFO, ClientExceptionType.Unknown.getInfoHeaderValue(
                                ExceptionUtils.getRootCauseMessage(ex)));
            }
            // ...or just report as InternalServerError
            if (builder == null) {
                builder = Response.status(Response.Status.INTERNAL_SERVER_ERROR).
                        header(RESTHeaders.ERROR_CODE, ClientExceptionType.Unknown.name()).
                        header(RESTHeaders.ERROR_INFO, ClientExceptionType.Unknown.getInfoHeaderValue(
                                ExceptionUtils.getRootCauseMessage(ex)));

                ErrorTO error = new ErrorTO();
                error.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
                error.setType(ClientExceptionType.Unknown);
                error.getElements().add(ExceptionUtils.getRootCauseMessage(ex));

                builder.entity(error);
            }
        }

        return Optional.ofNullable(builder).map(ResponseBuilder::build).orElse(null);
    }