Skip to content

restannotations: error handling cannot be customized #384

@Lessy

Description

@Lessy

If an exception is thrown inside my RESTFull method then AbstractRestResource catches it, writes 500 and "Internal Error" to the response and that's it. It is very hard to determine the "real" exception:

    private Object invokeMappedMethod(Method method, List<?> parametersValues, WebResponse response)
    {
        try
        {
            return method.invoke(this, parametersValues.toArray());
        }
        catch (Exception e)
        {
            response.setStatus(500);
            response.write("General server error.");

            log.debug("Error invoking method '" + method.getName() + "'");
        }

        return null;
    }

So I would like to propose to enable customizing the error handling like this:

    private Object invokeMappedMethod(Method method, List<?> parametersValues, WebResponse response)
    {
        try
        {
            return method.invoke(this, parametersValues.toArray());
        }
        catch (Exception e)
        {
            handleException(e, response);
            log.debug("Error invoking method '" + method.getName() + "'");
        }

        return null;
    }

    /**
     * Handle Exception. Default: set response Status to 500. Override this method to implement
     * customized error handling
     * @param e The Exception
     * @param response Response-Object
     */
    protected void handleException(Exception e, WebResponse response)
    {
        response.setStatus(500);
        response.write("General server error.");
    }

Default behavior is the same as before but handleException can be overwritten and customized.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions