Skip to content

Commit

Permalink
fixes #637 refactor the LightHttpHandler to ensure the auditInfo is c…
Browse files Browse the repository at this point in the history
…alled by both default methods (#638)
  • Loading branch information
stevehu committed Dec 30, 2019
1 parent dac3bf2 commit f615ba0
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions handler/src/main/java/com/networknt/handler/LightHttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public interface LightHttpHandler extends HttpHandler {
String ERROR_NOT_DEFINED = "ERR10042";

// Handler can save errors and stack traces for auditing. Default: false
static final String CONFIG_NAME = "handler";
static final String AUDIT_ON_ERROR = "auditOnError";
static final String AUDIT_STACK_TRACE = "auditStackTrace";
String CONFIG_NAME = "handler";
String AUDIT_ON_ERROR = "auditOnError";
String AUDIT_STACK_TRACE = "auditStackTrace";

public static HandlerConfig config = (HandlerConfig) Config.getInstance().getJsonObjectConfig(CONFIG_NAME, HandlerConfig.class);
static boolean auditOnError = config != null ? config.getAuditOnError() : false;
static boolean auditStackTrace = config != null ? config.getAuditStackTrace() : false;
HandlerConfig config = (HandlerConfig) Config.getInstance().getJsonObjectConfig(CONFIG_NAME, HandlerConfig.class);
boolean auditOnError = config != null ? config.getAuditOnError() : false;
boolean auditStackTrace = config != null ? config.getAuditStackTrace() : false;

/**
* This method is used to construct a standard error status in JSON format from an error code.
Expand All @@ -65,43 +65,37 @@ default void setExchangeStatus(HttpServerExchange exchange, String code, final O
// There is no entry in status.yml for this particular error code.
status = new Status(ERROR_NOT_DEFINED, code);
}
setExchangeStatus(exchange, status);
}

/**
* There are situations that the downstream service returns an error status response and we just
* want to bubble up to the caller and eventually to the original caller.
*
* @param exchange HttpServerExchange
* @param status error status
*/
default void setExchangeStatus(HttpServerExchange exchange, Status status) {
exchange.setStatusCode(status.getStatusCode());
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
status.setDescription(status.getDescription().replaceAll("\\\\", "\\\\\\\\"));
exchange.getResponseSender().send(status.toString());
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
logger.error(status.toString() + " at " + elements[2].getClassName() + "." + elements[2].getMethodName() + "(" + elements[2].getFileName() + ":" + elements[2].getLineNumber() + ")");

// In normal case, the auditInfo shouldn't be null as it is created by OpenApiHandler with
// endpoint and openapiOperation available. This handler will enrich the auditInfo.
@SuppressWarnings("unchecked")
Map<String, Object> auditInfo = exchange.getAttachment(AttachmentConstants.AUDIT_INFO);
if(auditInfo == null) {
auditInfo = new HashMap<>();
exchange.putAttachment(AttachmentConstants.AUDIT_INFO, auditInfo);
}
}

// save info for auditing purposes in case of an error
if(auditOnError)
auditInfo.put(Constants.STATUS, status);
auditInfo.put(Constants.STATUS, status);
if(auditStackTrace) {
auditInfo.put(Constants.STACK_TRACE, Arrays.toString(elements));
}
}

/**
* There are situations that the downstream service returns an error status response and we just
* want to bubble up to the caller and eventually to the original caller.
*
* @param exchange HttpServerExchange
* @param status error status
*/
default void setExchangeStatus(HttpServerExchange exchange, Status status) {
exchange.setStatusCode(status.getStatusCode());
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
status.setDescription(status.getDescription().replaceAll("\\\\", "\\\\\\\\"));
exchange.getResponseSender().send(status.toString());
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
logger.error(status.toString() + " at " + elements[2].getClassName() + "." + elements[2].getMethodName() + "(" + elements[2].getFileName() + ":" + elements[2].getLineNumber() + ")");
auditInfo.put(Constants.STACK_TRACE, Arrays.toString(elements));
}
}
}

0 comments on commit f615ba0

Please sign in to comment.