Search in sources :

Example 1 with ScriptException

use of groovy.util.ScriptException in project groovy by apache.

the class GroovyServlet method service.

/**
     * Handle web requests to the GroovyServlet
     */
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Get the script path from the request - include aware (GROOVY-815)
    final String scriptUri = getScriptUri(request);
    // Set it to HTML by default
    response.setContentType("text/html; charset=" + encoding);
    // Set up the script context
    final ServletBinding binding = new ServletBinding(request, response, servletContext);
    setVariables(binding);
    // Run the script
    try {
        Closure closure = new Closure(gse) {

            public Object call() {
                try {
                    return ((GroovyScriptEngine) getDelegate()).run(scriptUri, binding);
                } catch (ResourceException e) {
                    throw new RuntimeException(e);
                } catch (ScriptException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        GroovyCategorySupport.use(ServletCategory.class, closure);
    } catch (RuntimeException runtimeException) {
        StringBuilder error = new StringBuilder("GroovyServlet Error: ");
        error.append(" script: '");
        error.append(scriptUri);
        error.append("': ");
        Throwable e = runtimeException.getCause();
        /*
             * Null cause?!
             */
        if (e == null) {
            error.append(" Script processing failed.\n");
            error.append(runtimeException.getMessage());
            if (runtimeException.getStackTrace().length > 0)
                error.append(runtimeException.getStackTrace()[0].toString());
            servletContext.log(error.toString());
            System.err.println(error.toString());
            runtimeException.printStackTrace(System.err);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error.toString());
            return;
        }
        /*
             * Resource not found.
             */
        if (e instanceof ResourceException) {
            error.append(" Script not found, sending 404.");
            servletContext.log(error.toString());
            System.err.println(error.toString());
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        /*
             * Other internal error. Perhaps syntax?!
             */
        servletContext.log("An error occurred processing the request", runtimeException);
        error.append(e.getMessage());
        if (e.getStackTrace().length > 0)
            error.append(e.getStackTrace()[0].toString());
        servletContext.log(e.toString());
        System.err.println(e.toString());
        runtimeException.printStackTrace(System.err);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    }
}
Also used : ScriptException(groovy.util.ScriptException) GroovyScriptEngine(groovy.util.GroovyScriptEngine) Closure(groovy.lang.Closure) ResourceException(groovy.util.ResourceException)

Example 2 with ScriptException

use of groovy.util.ScriptException in project groovy-core by groovy.

the class GroovyServlet method service.

/**
     * Handle web requests to the GroovyServlet
     */
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Get the script path from the request - include aware (GROOVY-815)
    final String scriptUri = getScriptUri(request);
    // Set it to HTML by default
    response.setContentType("text/html; charset=" + encoding);
    // Set up the script context
    final ServletBinding binding = new ServletBinding(request, response, servletContext);
    setVariables(binding);
    // Run the script
    try {
        Closure closure = new Closure(gse) {

            public Object call() {
                try {
                    return ((GroovyScriptEngine) getDelegate()).run(scriptUri, binding);
                } catch (ResourceException e) {
                    throw new RuntimeException(e);
                } catch (ScriptException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        GroovyCategorySupport.use(ServletCategory.class, closure);
    } catch (RuntimeException runtimeException) {
        StringBuilder error = new StringBuilder("GroovyServlet Error: ");
        error.append(" script: '");
        error.append(scriptUri);
        error.append("': ");
        Throwable e = runtimeException.getCause();
        /*
             * Null cause?!
             */
        if (e == null) {
            error.append(" Script processing failed.\n");
            error.append(runtimeException.getMessage());
            if (runtimeException.getStackTrace().length > 0)
                error.append(runtimeException.getStackTrace()[0].toString());
            servletContext.log(error.toString());
            System.err.println(error.toString());
            runtimeException.printStackTrace(System.err);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error.toString());
            return;
        }
        /*
             * Resource not found.
             */
        if (e instanceof ResourceException) {
            error.append(" Script not found, sending 404.");
            servletContext.log(error.toString());
            System.err.println(error.toString());
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        /*
             * Other internal error. Perhaps syntax?!
             */
        servletContext.log("An error occurred processing the request", runtimeException);
        error.append(e.getMessage());
        if (e.getStackTrace().length > 0)
            error.append(e.getStackTrace()[0].toString());
        servletContext.log(e.toString());
        System.err.println(e.toString());
        runtimeException.printStackTrace(System.err);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    }
}
Also used : ScriptException(groovy.util.ScriptException) GroovyScriptEngine(groovy.util.GroovyScriptEngine) Closure(groovy.lang.Closure) ResourceException(groovy.util.ResourceException)

Aggregations

Closure (groovy.lang.Closure)2 GroovyScriptEngine (groovy.util.GroovyScriptEngine)2 ResourceException (groovy.util.ResourceException)2 ScriptException (groovy.util.ScriptException)2