Class ErrorHandler

java.lang.Object
org.eclipse.jetty.ee10.servlet.ErrorHandler
All Implemented Interfaces:
Request.Handler, Invocable
Direct Known Subclasses:
ErrorPageErrorHandler

public class ErrorHandler extends Object implements Request.Handler
  • Field Details

  • Constructor Details

    • ErrorHandler

      public ErrorHandler()
  • Method Details

    • errorPageForMethod

      public boolean errorPageForMethod(String method)
    • handle

      public boolean handle(Request request, Response response, Callback callback) throws Exception
      Description copied from interface: Request.Handler

      Invoked to handle the passed HTTP request and response.

      The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed.

      Exceptions thrown by this method may be subsequently handled by an error Request.Handler, if present, otherwise a default HTTP 500 error is generated and the callback completed while writing the error response.

      The simplest implementation is:

       public boolean handle(Request request, Response response, Callback callback)
       {
           callback.succeeded();
           return true;
       }
       

      A HelloWorld implementation is:

       public boolean handle(Request request, Response response, Callback callback)
       {
           response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback);
           return true;
       }
       
      Specified by:
      handle in interface Request.Handler
      Parameters:
      request - the HTTP request to handle
      response - the HTTP response to handle
      callback - the callback to complete when the handling is complete
      Returns:
      True if and only if the request will be handled, a response generated and the callback eventually called. This may occur within the scope of the call to this method, or asynchronously some time later. If false is returned, then this method must not generate a response, nor complete the callback.
      Throws:
      Exception - if there is a failure during the handling. Catchers cannot assume that the callback will be called and thus should attempt to complete the request as if a false had been returned.
      See Also:
    • generateAcceptableResponse

      protected void generateAcceptableResponse(ServletContextRequest baseRequest, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, int code, String message) throws IOException
      Generate an acceptable error response.

      This method is called to generate an Error page of a mime type that is acceptable to the user-agent. The Accept header is evaluated in quality order and the method generateAcceptableResponse(ServletContextRequest, HttpServletRequest, HttpServletResponse, int, String, String) is called for each mimetype until the response is written to or committed.

      Parameters:
      baseRequest - The base request
      request - The servlet request (may be wrapped)
      response - The response (may be wrapped)
      code - the http error code
      message - the http error message
      Throws:
      IOException - if the response cannot be generated
    • getAcceptableWriter

      @Deprecated protected Writer getAcceptableWriter(Request baseRequest, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException
      Deprecated.
      Returns an acceptable writer for an error page.

      Uses the user-agent's Accept-Charset to get response Writer. The acceptable charsets are tested in quality order if they are known to the JVM and the first known is set on ServletResponse.setCharacterEncoding(String) and the ServletResponse.getWriter() method used to return a writer. If there is no Accept-Charset header then ISO-8859-1 is used. If '*' is the highest quality known charset, then utf-8 is used.

      Parameters:
      baseRequest - The base request
      request - The servlet request (may be wrapped)
      response - The response (may be wrapped)
      Returns:
      A Writer if there is a known acceptable charset or null
      Throws:
      IOException - if a Writer cannot be returned
    • generateAcceptableResponse

      protected void generateAcceptableResponse(ServletContextRequest baseRequest, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, int code, String message, String contentType) throws IOException
      Generate an acceptable error response for a mime type.

      This method is called for each mime type in the users agent's Accept header, a response of the appropriate type is generated.

      The default implementation handles "text/html", "text/*" and "*/*". The method can be overridden to handle other types. Implementations must immediate produce a response and may not be async.

      Parameters:
      baseRequest - The base request
      request - The servlet request (may be wrapped)
      response - The response (may be wrapped)
      code - the http error code
      message - the http error message
      contentType - The mimetype to generate (may be */*or other wildcard)
      Throws:
      IOException - if a response cannot be generated
    • handleErrorPage

      protected void handleErrorPage(jakarta.servlet.http.HttpServletRequest request, Writer writer, int code, String message) throws IOException
      Throws:
      IOException
    • writeErrorPage

      protected void writeErrorPage(jakarta.servlet.http.HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException
      Throws:
      IOException
    • writeErrorPageHead

      protected void writeErrorPageHead(jakarta.servlet.http.HttpServletRequest request, Writer writer, int code, String message) throws IOException
      Throws:
      IOException
    • writeErrorPageBody

      protected void writeErrorPageBody(jakarta.servlet.http.HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException
      Throws:
      IOException
    • writeErrorPageMessage

      protected void writeErrorPageMessage(jakarta.servlet.http.HttpServletRequest request, Writer writer, int code, String message, String uri) throws IOException
      Throws:
      IOException
    • writeErrorPlain

      protected void writeErrorPlain(jakarta.servlet.http.HttpServletRequest request, PrintWriter writer, int code, String message)
    • writeErrorJson

      protected void writeErrorJson(jakarta.servlet.http.HttpServletRequest request, PrintWriter writer, int code, String message)
    • writeErrorPageStacks

      protected void writeErrorPageStacks(jakarta.servlet.http.HttpServletRequest request, Writer writer) throws IOException
      Throws:
      IOException
    • badMessageError

      public ByteBuffer badMessageError(int status, String reason, HttpFields.Mutable fields)
      Bad Message Error body

      Generate an error response body to be sent for a bad message. In this case there is something wrong with the request, so either a request cannot be built, or it is not safe to build a request. This method allows for a simple error page body to be returned and some response headers to be set.

      Parameters:
      status - The error code that will be sent
      reason - The reason for the error code (may be null)
      fields - The header fields that will be sent with the response.
      Returns:
      The content as a ByteBuffer, or null for no body.
    • getCacheControl

      public String getCacheControl()
      Get the cacheControl.
      Returns:
      the cacheControl header to set on error responses.
    • setCacheControl

      public void setCacheControl(String cacheControl)
      Set the cacheControl.
      Parameters:
      cacheControl - the cacheControl header to set on error responses.
    • isShowServlet

      public boolean isShowServlet()
      Returns:
      True if the error page will show the Servlet that generated the error
    • setShowServlet

      public void setShowServlet(boolean showServlet)
      Parameters:
      showServlet - True if the error page will show the Servlet that generated the error
    • isShowStacks

      public boolean isShowStacks()
      Returns:
      True if stack traces are shown in the error pages
    • setShowStacks

      public void setShowStacks(boolean showStacks)
      Parameters:
      showStacks - True if stack traces are shown in the error pages
    • setShowMessageInTitle

      public void setShowMessageInTitle(boolean showMessageInTitle)
      Set if true, the error message appears in page title.
      Parameters:
      showMessageInTitle - if true, the error message appears in page title
    • getShowMessageInTitle

      public boolean getShowMessageInTitle()
    • write

      protected void write(Writer writer, String string) throws IOException
      Throws:
      IOException
    • getErrorHandler

      public static Request.Handler getErrorHandler(Server server, ContextHandler context)