Class ErrorHandler

java.lang.Object
org.eclipse.jetty.server.handler.ErrorHandler
All Implemented Interfaces:
Request.Handler, Invocable
Direct Known Subclasses:
ErrorHandler, ErrorHandler, ReHandlingErrorHandler

@ManagedObject public class ErrorHandler extends Object implements Request.Handler
Handler for Error pages An ErrorHandler is registered with Server.setErrorHandler(Request.Handler). It is called by the Response.writeError(Request, Response, Callback, int, String) to generate an error page.
  • Field Details

  • Constructor Details

    • ErrorHandler

      public ErrorHandler()
  • Method Details

    • errorPageForMethod

      public boolean errorPageForMethod(String method)
    • writeError

      public boolean writeError(Request request, Response response, Callback callback, int code)
      Write an error response, or signal that the error will be handled by a down stream handler.

      The default implementation calls Response.writeError(Request, Response, Callback, int) and returns true.

      ErrorHandler extensions may override this method to modify the state of the Request and/or Response so that when the method returns false an error page will be generated by a subsequent Handler. For example, Servlet implementations override this method to set attributes that trigger a Servlet `sendError` call when the ServletHandler invokes the ServletChannel.

      Parameters:
      request - The request.
      response - The response.
      callback - The callback to call when the response is written.
      code - The error status code.
      Returns:
      true if the error response was written; false if the state of the request and/or response has been changed so that by continuing normal handling an error response will be generated by a down stream handler.
    • 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:
    • generateCacheControl

      protected void generateCacheControl(Response response)
    • generateResponse

      protected void generateResponse(Request request, Response response, int code, String message, Throwable cause, Callback callback) throws IOException
      Throws:
      IOException
    • generateAcceptableResponse

      protected boolean generateAcceptableResponse(Request request, Response response, Callback callback, String contentType, List<Charset> charsets, int code, String message, Throwable cause) throws IOException
      Throws:
      IOException
    • computeBufferSize

      protected int computeBufferSize(Request request)
    • writeErrorHtml

      protected void writeErrorHtml(Request request, Writer writer, Charset charset, int code, String message, Throwable cause) throws IOException
      Throws:
      IOException
    • writeErrorHtmlMeta

      protected void writeErrorHtmlMeta(Request request, Writer writer, Charset charset) throws IOException
      Throws:
      IOException
    • writeErrorHtmlHead

      protected void writeErrorHtmlHead(Request request, Writer writer, int code, String message) throws IOException
      Throws:
      IOException
    • writeErrorHtmlBody

      protected void writeErrorHtmlBody(Request request, Writer writer, int code, String message, Throwable cause) throws IOException
      Throws:
      IOException
    • writeErrorHtmlMessage

      protected void writeErrorHtmlMessage(Request request, Writer writer, int code, String message, Throwable cause, String uri) throws IOException
      Throws:
      IOException
    • htmlRow

      protected void htmlRow(Writer writer, String tag, Object value) throws IOException
      Throws:
      IOException
    • writeErrorPlain

      protected void writeErrorPlain(Request request, PrintWriter writer, int code, String message, Throwable cause)
    • writeErrorOrigin

      protected void writeErrorOrigin(String origin, Consumer<String> consumer)
    • writeErrorJson

      protected void writeErrorJson(Request request, PrintWriter writer, int code, String message, Throwable cause)
    • writeErrorHtmlStacks

      protected void writeErrorHtmlStacks(Request request, Writer writer) throws IOException
      Throws:
      IOException
    • badMessageError

      @Deprecated(since="12.0.8", forRemoval=true) public ByteBuffer badMessageError(int status, String reason, HttpFields.Mutable fields)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Do not override. No longer invoked by Jetty.
      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

      @ManagedAttribute("The value of the Cache-Control response header") 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.
    • isShowStacks

      @ManagedAttribute("Whether the error page shows the stack trace") 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
    • isShowCauses

      @ManagedAttribute("Whether the error page shows the exception causes") public boolean isShowCauses()
      Returns:
      True if exception causes are shown in the error pages
    • setShowCauses

      public void setShowCauses(boolean showCauses)
      Parameters:
      showCauses - True if exception causes are shown in the error pages
    • isShowMessageInTitle

      @ManagedAttribute("Whether the error message is shown in the error page title") public boolean isShowMessageInTitle()
    • 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
    • isShowOrigin

      public boolean isShowOrigin()
    • setShowOrigin

      public void setShowOrigin(boolean showOrigin)
    • getDefaultResponseMimeType

      @ManagedAttribute("Mime type to be used when a client does not specify an Accept header, or the request did not fully parse") public String getDefaultResponseMimeType()
      Returns:
      The mime type to be used when a client does not specify an Accept header, or the request did not fully parse
    • setDefaultResponseMimeType

      public void setDefaultResponseMimeType(String defaultResponseMimeType)
      Parameters:
      defaultResponseMimeType - The mime type to be used when a client does not specify an Accept header, or the request did not fully parse
    • write

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

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

      @ManagedAttribute("Buffer size for entire error response") public int getBufferSize()
      Returns:
      Buffer size for entire error response. If error page is bigger than buffer size, it will be truncated. With a -1 meaning that a heuristic will be used (e.g. min(8K, httpConfig.bufferSize))
    • setBufferSize

      public void setBufferSize(int bufferSize)
      Parameters:
      bufferSize - Buffer size for entire error response. If error page is bigger than buffer size, it will be truncated. With a -1 meaning that a heuristic will be used (e.g. min(8K, httpConfig.bufferSize))
    • toString

      public String toString()
      Overrides:
      toString in class Object