Example usage for org.apache.commons.lang3.exception ExceptionContext setContextValue

List of usage examples for org.apache.commons.lang3.exception ExceptionContext setContextValue

Introduction

In this page you can find the example usage for org.apache.commons.lang3.exception ExceptionContext setContextValue.

Prototype

ExceptionContext setContextValue(String label, Object value);

Source Link

Document

Sets a contextual label-value pair into this context.

Usage

From source file:com.joyent.manta.http.HttpHelper.java

/**
 * Appends context attributes for the HTTP request and HTTP response objects
 * to a {@link ExceptionContext} instance.
 *
 * @param exception exception to append to
 * @param request HTTP request object//w  w  w.j  a  va  2 s .  com
 * @param response HTTP response object
 */
static void annotateContextedException(final ExceptionContext exception, final HttpRequest request,
        final HttpResponse response) {
    Validate.notNull(exception, "Exception context object must not be null");

    if (request != null) {
        final String requestId = extractRequestId(response);
        exception.setContextValue("requestId", requestId);

        final String requestDump = reflectionToString(request, SHORT_PREFIX_STYLE);
        exception.setContextValue("request", requestDump);
        exception.setContextValue("requestMethod", request.getRequestLine().getMethod());
        exception.setContextValue("requestURL", request.getRequestLine().getUri());
        final String requestHeaders = asString(request.getAllHeaders());
        exception.setContextValue("requestHeaders", requestHeaders);
        exception.setContextValue("loadBalancerAddress", MDC.get("mantaLoadBalancerAddress"));
    }

    if (response != null) {
        final String responseDump = reflectionToString(response, SHORT_PREFIX_STYLE);
        exception.setContextValue("response", responseDump);
        final String responseHeaders = asString(response.getAllHeaders());
        exception.setContextValue("responseHeaders", responseHeaders);

        final StatusLine statusLine = response.getStatusLine();

        if (statusLine != null) {
            exception.setContextValue("responseStatusCode", statusLine.getStatusCode());
            exception.setContextValue("responseStatusReason", statusLine.getReasonPhrase());
        }
    }
}

From source file:com.joyent.manta.util.MantaUtils.java

/**
 * Adds the passed exceptions as properties to a {@link ContextedException}
 * instance.//  w  ww.j  a  v  a 2s  . c  o m
 *
 * @param contexted exception to attach exceptions to
 * @param exceptions exceptions to attach
 */
public static void attachExceptionsToContext(final ExceptionContext contexted,
        final Iterable<? extends Exception> exceptions) {
    int count = 1;
    for (Exception e : exceptions) {
        final String label = String.format("exception_%d", count++);
        contexted.setContextValue(label, e);
    }
}

From source file:com.joyent.manta.client.crypto.MantaEncryptedObjectInputStream.java

/**
 * Annotates a {@link ContextedException} with the details of this class in order to aid
 * in debugging./*from  w  w w .  j a  v  a  2s .  c om*/
 *
 * @param exception exception to annotate
 */
private void annotateException(final ExceptionContext exception) {
    exception.setContextValue("path", getPath());
    exception.setContextValue("etag", this.getEtag());
    exception.setContextValue("lastModified", this.getLastModifiedTime());
    exception.setContextValue("ciphertextContentLength", super.getContentLength());
    exception.setContextValue("plaintextContentLength", this.getContentLength());
    exception.setContextValue("plaintextBytesRead", this.plaintextBytesRead);
    exception.setContextValue("cipherId", getHeaderAsString(MantaHttpHeaders.ENCRYPTION_CIPHER));
    exception.setContextValue("cipherDetails", this.cipherDetails);
    exception.setContextValue("cipherInputStream", this.cipherInputStream);
    exception.setContextValue("authenticationEnabled", this.authenticateCiphertext);
    exception.setContextValue("threadName", Thread.currentThread().getName());
    exception.setContextValue("requestId", this.getRequestId());

    if (this.cipher != null && this.cipher.getIV() != null) {
        exception.setContextValue("iv", Hex.encodeHexString(this.cipher.getIV()));
    }

    if (this.hmac != null) {
        exception.setContextValue("hmacAlgorithm", this.hmac.getAlgorithmName());
    } else {
        exception.setContextValue("hmacAlgorithm", "null");
    }
}

From source file:com.joyent.manta.client.multipart.ServerSideMultipartManager.java

/**
 * Appends context attributes for the HTTP request and HTTP response objects
 * to a {@link ExceptionContext} instance using values relevant to this
 * class./*from   w  ww  .j  av  a2 s.co  m*/
 *
 * @param exception exception to append to
 * @param request HTTP request object
 * @param response HTTP response object
 * @param objectPath path to the object being operated on
 * @param requestBody contents of request body as byte array
 */
private void annotateException(final ExceptionContext exception, final HttpRequest request,
        final HttpResponse response, final String objectPath, final byte[] requestBody) {
    HttpHelper.annotateContextedException(exception, request, response);

    if (objectPath != null) {
        exception.setContextValue("objectPath", objectPath);
    }

    if (requestBody != null) {
        exception.setContextValue("requestBody", new String(requestBody, StandardCharsets.UTF_8));
    }
}