/**
 * Copyright 2015 power721 (power0721@gmail.com)
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jfinal.ext.render;

import com.jfinal.render.JsonRender;

import java.util.HashMap;
import java.util.Map;

/**
 * Render error to json response.
 */
public class JsonErrorRender extends JsonRender {

    private static final Map<Integer, String> ERROR_CODES = new HashMap<Integer, String>();

    static {
        ERROR_CODES.put(400, "Bad Request");
        ERROR_CODES.put(401, "Unauthorized");
        ERROR_CODES.put(403, "Forbidden");
        ERROR_CODES.put(404, "Not Found");
        ERROR_CODES.put(406, "Not Acceptable");
        ERROR_CODES.put(410, "Gone");
        ERROR_CODES.put(429, "Too Many Requests");
        ERROR_CODES.put(500, "Internal Server Error");
        ERROR_CODES.put(503, "Service Unavailable");
    }

    private final int errorCode;

    /**
     * construct the error render by the error code
     *
     * @param errorCode the error code
     */
    public JsonErrorRender(int errorCode) {
        super();
        this.errorCode = errorCode;
    }

    /**
     * render the HTTP response
     */
    @Override
    public void render() {
        String error = ERROR_CODES.get(errorCode);
        request.setAttribute("error", error != null ? error :
                "Internal Server Error(invalid error code" + errorCode + ")");
        response.setStatus(errorCode);
        super.render();
    }

}

@@ -685,24 +685,71 @@
 sonRender;%0A%0A
+import javax.servlet.http.HttpServletResponse;%0A
 import java.
@@ -762,16 +762,16 @@
 ashMap;%0A
-
 import j
@@ -1012,11 +1012,42 @@
 put(
-400
+HttpServletResponse.SC_BAD_REQUEST
 , %22B
@@ -1088,11 +1088,43 @@
 put(
-401
+HttpServletResponse.SC_UNAUTHORIZED
 , %22U
@@ -1166,11 +1166,40 @@
 put(
-403
+HttpServletResponse.SC_FORBIDDEN
 , %22F
@@ -1238,11 +1238,40 @@
 put(
-404
+HttpServletResponse.SC_NOT_FOUND
 , %22N
@@ -1310,11 +1310,45 @@
 put(
-406
+HttpServletResponse.SC_NOT_ACCEPTABLE
 , %22N
@@ -1392,11 +1392,35 @@
 put(
-410
+HttpServletResponse.SC_GONE
 , %22G
@@ -1454,62 +1454,52 @@
 put(
-429, %22Too Many Requests%22);%0A        ERROR_CODES.put(500
+HttpServletResponse.SC_INTERNAL_SERVER_ERROR
 , %22I
@@ -1550,11 +1550,50 @@
 put(
-503
+HttpServletResponse.SC_SERVICE_UNAVAILABLE
 , %22S

use status code from HttpServletResponse