package i5.las2peer.restMapper;

import java.util.logging.Level;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import i5.las2peer.logging.L2pLogger;

@Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {

	private final L2pLogger logger = L2pLogger.getInstance(GenericExceptionMapper.class);

	@Override
	public Response toResponse(Throwable e) {
		// FIXME use format from WebConnector#sendUnexpectedErrorResponse
		logger.log(Level.INFO, "Request failed: " + e.toString(), e);
		int code = Status.INTERNAL_SERVER_ERROR.getStatusCode();
		if (e instanceof WebApplicationException) {
			WebApplicationException webEx = (WebApplicationException) e;
			code = webEx.getResponse().getStatus();
		}
		return Response.status(code).entity(new ExceptionEntity(code, e)).type(MediaType.APPLICATION_JSON_TYPE).build();
	}

}

@@ -59,16 +59,182 @@
 Level;%0A%0A
+import javax.ws.rs.BadRequestException;%0Aimport javax.ws.rs.ForbiddenException;%0Aimport javax.ws.rs.InternalServerErrorException;%0Aimport javax.ws.rs.NotFoundException;%0A
 import j
@@ -449,24 +449,273 @@
 .Provider;%0A%0A
+import i5.las2peer.api.execution.ResourceNotFoundException;%0Aimport i5.las2peer.api.execution.ServiceAccessDeniedException;%0Aimport i5.las2peer.api.execution.InvocationBadArgumentException;%0Aimport i5.las2peer.api.execution.ServiceInvocationException;%0A
 import i5.la
@@ -1161,16 +1161,580 @@
 Code();%0A
+%09%09// map ServiceInvocationExceptions to WebApplicationExceptions%0A%09%09if (e instanceof ServiceInvocationException) %7B%0A%09%09%09if (e instanceof InvocationBadArgumentException) %7B%0A%09%09%09%09e = new BadRequestException(e.getMessage(), e.getCause());%0A%09%09%09%7D else if (e instanceof ResourceNotFoundException) %7B%0A%09%09%09%09e = new NotFoundException(e.getMessage(), e.getCause());%0A%09%09%09%7D else if (e instanceof ServiceAccessDeniedException) %7B%0A%09%09%09%09e = new ForbiddenException(e.getMessage(), e.getCause());%0A%09%09%09%7D else %7B%0A%09%09%09%09e = new InternalServerErrorException(%22Exception during RMI call%22, e);%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09if (e 

improve auto exception mapping in GenericExceptionMapper