Skip to content
Browse files

improve JSONErrorHandler.

  • Loading branch information...
1 parent 5a38fe3 commit 131f0e546be604469d5e8586ef8ab571f1bebab8 Robert Newson committed Jul 7, 2010
View
5 src/main/java/com/github/rnewson/couchdb/lucene/JSONErrorHandler.java
@@ -21,6 +21,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import net.sf.json.JSONObject;
+
import org.mortbay.jetty.HttpConnection;
import org.mortbay.jetty.handler.ErrorHandler;
@@ -37,7 +39,8 @@
public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException {
HttpConnection connection = HttpConnection.getCurrentConnection();
connection.getRequest().setHandled(true);
- ServletUtils.sendJSONError(request, response, connection.getResponse().getStatus(), connection.getResponse().getReason());
+ ServletUtils.sendJSONError(request, response, connection.getResponse().getStatus(),
+ JSONObject.fromObject(connection.getResponse().getReason()));
}
}
View
9 src/main/java/com/github/rnewson/couchdb/lucene/util/ServletUtils.java
@@ -52,16 +52,21 @@ public static String getParameter(final HttpServletRequest req, final String par
public static void sendJSONError(final HttpServletRequest request, final HttpServletResponse response, final int code,
final String reason) throws IOException {
final JSONObject obj = new JSONObject();
- obj.put("code", code);
obj.put("reason", reason);
+ sendJSONError(request, response, code, obj);
+ }
+
+ public static void sendJSONError(final HttpServletRequest request, final HttpServletResponse response, final int code,
+ final JSONObject error) throws IOException {
+ error.put("code", code);
setResponseContentTypeAndEncoding(request, response);
response.setHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate,no-cache,no-store");
response.setStatus(code);
final Writer writer = response.getWriter();
try {
- writer.write(obj.toString());
+ writer.write(error.toString());
writer.write("\n");
} finally {
writer.close();

0 comments on commit 131f0e5

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