package org.signaut.jetty.deploy.providers.couchdb;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.server.handler.ErrorHandler;
/**
 * Display the error as a json object
 * 
 */
class JsonErrorHandler extends ErrorHandler {

    @Override
    protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message,
            boolean showStacks) throws IOException {
        // TODO Auto-generated method stub
        final Throwable exception = (Throwable) request.getAttribute("javax.servlet.error.exception");
        if (exception == null) {
            message = String.format("{\"status\": %d, \"uri\":\"%s\"," + "\"message\": %s}", code,
                                    request.getRequestURI(), jsonStr(message));
        } else {
            message = String.format("{\"status\": %d, \"uri\":\"%s\"," + "\"message\": %s, " + "\"exception\": { "
                                            + "\"exception\": \"%s\", \"message\": %s} }", code,
                                    request.getRequestURI(), jsonStr(message),
                                    exception.getClass().getCanonicalName(), jsonStr(exception.getMessage()));
        }
        writer.write(message);
    }

    private final String jsonStr(String string) {
        return string == null ? null : "\"" + string + "\"";
    }
}

@@ -612,16 +612,120 @@
 tion%22);%0A
+        final String user = request.getUserPrincipal()==null?null:request.getUserPrincipal().getName();%0A
         
@@ -810,37 +810,47 @@
  %5C%22uri%5C%22:%5C%22%25s%5C%22,
-%22 + %22
+ %5C%22user%5C%22: %25s, 
 %5C%22message%5C%22: %25s%7D
@@ -910,32 +910,47 @@
 getRequestURI(),
+ jsonStr(user),
  jsonStr(message
@@ -1039,21 +1039,31 @@
 :%5C%22%25s%5C%22,
-%22 + %22
+ %5C%22user%5C%22: %25s, 
 %5C%22messag
@@ -1075,13 +1075,8 @@
 %25s, 
-%22 + %22
 %5C%22ex
@@ -1239,32 +1239,47 @@
 getRequestURI(),
+ jsonStr(user),
  jsonStr(message

Add user to error messages