Skip to content
This repository
Browse code

improve JSONErrorHandler.

  • Loading branch information...
commit 131f0e546be604469d5e8586ef8ab571f1bebab8 1 parent 5a38fe3
Robert Newson authored
5 src/main/java/com/github/rnewson/couchdb/lucene/JSONErrorHandler.java
@@ -21,6 +21,8 @@
21 21 import javax.servlet.http.HttpServletRequest;
22 22 import javax.servlet.http.HttpServletResponse;
23 23
  24 +import net.sf.json.JSONObject;
  25 +
24 26 import org.mortbay.jetty.HttpConnection;
25 27 import org.mortbay.jetty.handler.ErrorHandler;
26 28
@@ -37,7 +39,8 @@
37 39 public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException {
38 40 HttpConnection connection = HttpConnection.getCurrentConnection();
39 41 connection.getRequest().setHandled(true);
40   - ServletUtils.sendJSONError(request, response, connection.getResponse().getStatus(), connection.getResponse().getReason());
  42 + ServletUtils.sendJSONError(request, response, connection.getResponse().getStatus(),
  43 + JSONObject.fromObject(connection.getResponse().getReason()));
41 44 }
42 45
43 46 }
9 src/main/java/com/github/rnewson/couchdb/lucene/util/ServletUtils.java
@@ -52,8 +52,13 @@ public static String getParameter(final HttpServletRequest req, final String par
52 52 public static void sendJSONError(final HttpServletRequest request, final HttpServletResponse response, final int code,
53 53 final String reason) throws IOException {
54 54 final JSONObject obj = new JSONObject();
55   - obj.put("code", code);
56 55 obj.put("reason", reason);
  56 + sendJSONError(request, response, code, obj);
  57 + }
  58 +
  59 + public static void sendJSONError(final HttpServletRequest request, final HttpServletResponse response, final int code,
  60 + final JSONObject error) throws IOException {
  61 + error.put("code", code);
57 62
58 63 setResponseContentTypeAndEncoding(request, response);
59 64 response.setHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate,no-cache,no-store");
@@ -61,7 +66,7 @@ public static void sendJSONError(final HttpServletRequest request, final HttpSer
61 66
62 67 final Writer writer = response.getWriter();
63 68 try {
64   - writer.write(obj.toString());
  69 + writer.write(error.toString());
65 70 writer.write("\n");
66 71 } finally {
67 72 writer.close();

0 comments on commit 131f0e5

Please sign in to comment.
Something went wrong with that request. Please try again.