From 727000dfaf7306d3f98559fe23a0a3a223fdc6dd Mon Sep 17 00:00:00 2001 From: Ignacio del Valle Alles Date: Mon, 27 Mar 2017 11:24:02 +0200 Subject: [PATCH] add top-level throwable logging --- .../org/brutusin/rpc/http/RpcServlet.java | 80 +++++++++++-------- .../rpc/websocket/WebsocketEndpoint.java | 5 ++ 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/rpc-impl/src/main/java/org/brutusin/rpc/http/RpcServlet.java b/rpc-impl/src/main/java/org/brutusin/rpc/http/RpcServlet.java index 4941f25..c2192da 100644 --- a/rpc-impl/src/main/java/org/brutusin/rpc/http/RpcServlet.java +++ b/rpc-impl/src/main/java/org/brutusin/rpc/http/RpcServlet.java @@ -32,7 +32,6 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; -import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -66,6 +65,8 @@ */ public final class RpcServlet extends HttpServlet { + private static final Logger LOGGER = Logger.getLogger(RpcServlet.class.getName()); + public static final String JSON_CONTENT_TYPE = "application/json"; private static final String REQ_ATT_MULTIPART_PARAMS = "multipartParams"; @@ -423,44 +424,55 @@ private int getInputStreamsNumber(RpcRequest rpcRequest, HttpAction service) thr */ private void execute(HttpServletRequest req, HttpServletResponse resp) throws IOException { - CachingInfo cachingInfo = null; - Object result = null; - Throwable throwable = null; - RpcRequest rpcRequest = null; try { - HttpActionSupportImpl.setInstance(new HttpActionSupportImpl(rpcCtx, req, resp)); - rpcRequest = getRequest(req); - result = execute(req, rpcRequest); - if (result != null && result instanceof Cacheable) { - Cacheable cacheable = (Cacheable) result; - cachingInfo = cacheable.getCachingInfo(); - result = cacheable.getValue(); + CachingInfo cachingInfo = null; + Object result = null; + Throwable throwable = null; + RpcRequest rpcRequest = null; + try { + HttpActionSupportImpl.setInstance(new HttpActionSupportImpl(rpcCtx, req, resp)); + rpcRequest = getRequest(req); + result = execute(req, rpcRequest); + if (result != null && result instanceof Cacheable) { + Cacheable cacheable = (Cacheable) result; + cachingInfo = cacheable.getCachingInfo(); + result = cacheable.getValue(); + } + } catch (Throwable th) { + throwable = th; + ensureStreamRead(req.getInputStream()); } - } catch (Throwable th) { - throwable = th; - ensureStreamRead(req.getInputStream()); - } - String reqETag = getETag(req); - addFixedHeaders(resp); - resp.setCharacterEncoding("UTF-8"); + String reqETag = getETag(req); + addFixedHeaders(resp); + resp.setCharacterEncoding("UTF-8"); - try { - if (result != null && StreamResult.class.isAssignableFrom(result.getClass())) { - serviceStream(reqETag, req, resp, (StreamResult) result, cachingInfo); - } else if (result instanceof RpcResponse) { - serviceJsonResponse(reqETag, req, resp, (RpcResponse) result, cachingInfo); - } else { - RpcResponse rpcResp = new RpcResponse(); - if (rpcRequest != null) { - rpcResp.setId(rpcRequest.getId()); + try { + if (result != null && StreamResult.class.isAssignableFrom(result.getClass())) { + serviceStream(reqETag, req, resp, (StreamResult) result, cachingInfo); + } else if (result instanceof RpcResponse) { + serviceJsonResponse(reqETag, req, resp, (RpcResponse) result, cachingInfo); + } else { + RpcResponse rpcResp = new RpcResponse(); + if (rpcRequest != null) { + rpcResp.setId(rpcRequest.getId()); + } + rpcResp.setError(ErrorFactory.getError(throwable)); + rpcResp.setResult(result); + serviceJsonResponse(reqETag, req, resp, rpcResp, cachingInfo); } - rpcResp.setError(ErrorFactory.getError(throwable)); - rpcResp.setResult(result); - serviceJsonResponse(reqETag, req, resp, rpcResp, cachingInfo); + } finally { + HttpActionSupportImpl.clear(); + deleteTempUploadDirectory(req); + } + } catch (Throwable th) { + LOGGER.log(Level.SEVERE, th.getMessage(), th); + if (th instanceof Error) { + throw (Error) th; + } else if (th instanceof RuntimeException) { + throw (RuntimeException) th; + } else if (th instanceof IOException) { + throw (IOException) th; } - } finally { - HttpActionSupportImpl.clear(); - deleteTempUploadDirectory(req); } } diff --git a/rpc-impl/src/main/java/org/brutusin/rpc/websocket/WebsocketEndpoint.java b/rpc-impl/src/main/java/org/brutusin/rpc/websocket/WebsocketEndpoint.java index 780ef99..93f538f 100644 --- a/rpc-impl/src/main/java/org/brutusin/rpc/websocket/WebsocketEndpoint.java +++ b/rpc-impl/src/main/java/org/brutusin/rpc/websocket/WebsocketEndpoint.java @@ -20,6 +20,8 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.websocket.CloseReason; import javax.websocket.Endpoint; import javax.websocket.Session; @@ -43,6 +45,7 @@ */ public class WebsocketEndpoint extends Endpoint { + private static final Logger LOGGER = Logger.getLogger(WebsocketEndpoint.class.getName()); private final Map contextMap = Collections.synchronizedMap(new HashMap()); private final Map wrapperMap = Collections.synchronizedMap(new HashMap()); @@ -74,6 +77,8 @@ public void onMessage(String message) { if (response != null) { sessionImpl.sendToPeerRaw(response); } + } catch (Throwable th) { + LOGGER.log(Level.SEVERE, th.getMessage(), th); } finally { WebsocketActionSupportImpl.clear(); }