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

io.crnk.jpa.internal.AbstractJpaExceptionMapper Maven / Gradle / Ivy

There is a newer version: 2.6.20180522184741
Show newest version
package io.crnk.jpa.internal;

import io.crnk.core.engine.document.ErrorData;
import io.crnk.core.engine.error.ErrorResponse;
import io.crnk.core.engine.error.ExceptionMapper;
import io.crnk.core.engine.error.JsonApiExceptionMapper;
import io.crnk.core.engine.http.HttpStatus;
import io.crnk.core.module.Module.ModuleContext;
import io.crnk.core.utils.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

abstract class AbstractWrappedExceptionMapper implements ExceptionMapper {

	private Logger LOGGER = LoggerFactory.getLogger(getClass());

	protected ModuleContext context;

	public AbstractWrappedExceptionMapper(ModuleContext context) {
		this.context = context;
	}

	@Override
	public ErrorResponse toErrorResponse(E exception) {
		Throwable cause = exception.getCause();
		if (cause != null) {
			Optional mapper = context.getExceptionMapperRegistry().findMapperFor(cause.getClass());
			if (mapper.isPresent()) {
				return mapper.get().toErrorResponse(cause);
			}
		}

		LOGGER.error("failed to process request due to jpa exception", exception);

		// no mapper found, return default error
		int status = getStatus();
		ErrorData errorData = ErrorData.builder().setStatus(Integer.toString(status))
				.setTitle(exception.getMessage()).build();
		return ErrorResponse.builder().setSingleErrorData(errorData).setStatus(status).build();
	}

	protected int getStatus() {
		return HttpStatus.INTERNAL_SERVER_ERROR_500;
	}

	@Override
	public E fromErrorResponse(ErrorResponse errorResponse) {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean accepts(ErrorResponse errorResponse) {
		return false;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy