0
\$\begingroup\$

I have a class ErrorForward which is basically a utility.

It's used in Servlets, inside the catch block.

It forwards request to /error mapping and sets the exception as a request attribute, to be used later in the request lifecycle i.e an error handler.

ErrorForward and forward feel like terrible names for the class and method, what should they be called?

What works best with the standard Java naming conventions?

public class ErrorForward {

  private static String[] EXCEPTION_ATTRIBUTES = {
      RequestDispatcher.ERROR_EXCEPTION, "org.springframework.web.servlet.DispatcherServlet.EXCEPTION"
  };

  public void forward(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
      Exception exception) throws ServletException, IOException {
    for (String attribute : EXCEPTION_ATTRIBUTES) {
      httpServletRequest.setAttribute(attribute, exception);
    }
    httpServletRequest.getRequestDispatcher("/error").forward(httpServletRequest, httpServletResponse);
  }
}

Which is used in many Java Servlets e.g:

public class MyServlet extends HttpServlet {

    private final ErrorForward errorForward = new ErrorForward();

    @Override
    protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) {

        try {
            // do things
        } catch (Exception e) {
            errorForward.forward(request, response, e);
        }
    }

    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
        // do things
    }
}
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.