Skip to content

Commit

Permalink
add top-level throwable logging
Browse files Browse the repository at this point in the history
  • Loading branch information
idelvall committed Mar 27, 2017
1 parent 57c8097 commit 727000d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
80 changes: 46 additions & 34 deletions rpc-impl/src/main/java/org/brutusin/rpc/http/RpcServlet.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
}

Expand Down
Expand Up @@ -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;
Expand All @@ -43,6 +45,7 @@
*/
public class WebsocketEndpoint extends Endpoint {

private static final Logger LOGGER = Logger.getLogger(WebsocketEndpoint.class.getName());
private final Map<String, WebsocketContext> contextMap = Collections.synchronizedMap(new HashMap());
private final Map<String, SessionImpl> wrapperMap = Collections.synchronizedMap(new HashMap());

Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 727000d

Please sign in to comment.