com.safelayer.rap.rest.impl.ExceptionHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pki-connector-restapi Show documentation
Show all versions of pki-connector-restapi Show documentation
The PKI Connector RESTAPI is a library that helps developing new PKI Connectors for TrustedX
The newest version!
package com.safelayer.rap.rest.impl;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Map;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.slf4j.Logger;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.safelayer.rap.json.JsonExceptionMessage;
public class ExceptionHandler {
static public Response handleError(Logger logger, Status status, Exception e, Map details) throws JsonProcessingException {
logger.error(e.getLocalizedMessage(), e);
String stackTrace = getStackTrace(e);
details.put("stackTrace", stackTrace);
ObjectMapper objectMapper = new ObjectMapper();
JsonExceptionMessage message = new JsonExceptionMessage(e.getClass().getSimpleName(), e.getMessage(), details);
return Response.status(status).entity(objectMapper.writeValueAsString(message)).build();
}
private static String getStackTrace(Exception e) {
String stackTrace = "";
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
e.printStackTrace(ps);
stackTrace = os.toString("UTF-8");
}
catch (Exception ex) {
}
return stackTrace;
}
}