javalite / javalite

JavaLite is a cohesive collection of frameworks designed from ground up to add pleasure back to your daily life
http://javalite.io
Other
856 stars 214 forks source link

Implement ability to render system errors #1296

Open ipolevoy opened 1 year ago

ipolevoy commented 1 year ago

Currently, if there is a system error and there is no route to execute, the renderSystemError(..) method is called:

https://github.com/javalite/javalite/blob/3dc111a0658b9ffc132bcc7565001beb976eff20/activeweb/src/main/java/org/javalite/activeweb/RequestDispatcher.java#L250

However, it renders an HTML error by default and the application developer has no control over this process.

A proposal is to implement the ability for an application developer to have full control over the error response. For example, while building a JSON-based API app, responding with HTML is making no sense.

ipolevoy commented 1 year ago

One idea is to add a configuration to the AppControllerConfig to configure some SystemErrorController, so that such a controller would get the error generated, but would be in a full control of a response - either direct or using a view.

ipolevoy commented 1 year ago

A better idea is to:

  1. Change the default implementation of RequestDispatcher to respond with an error:
    • in case of input Content type: JSON, respond with a simple JSON error
    • in any other case respond with text/plain
  2. Add this API to the RouteConfig:
public class RouteConfig extends AbstractRouteConfig {

    @Override
    public void init(AppContext appContext) {
      ....
       routeError().to(SystemErrorController.class).action("renderError");
      .....
     }
}

Include the SystemErrorController into the framework by default. This way, if there is a route to it, it will and the controller will look like this:

public SystemErrorController extends AppController{
   public void renderError(){
         Exception getException(); // - special method to get the exception that actually happened
   }
}

In case there is a routing error with this controller (say method missing), the usual system default behavior triggers.

The SystemErrorController will have access to all request parameters and will be able to render the response directly or via a view just like any other controller. It will also be able to set response code, and any header, including Content-type, so that the API - only-based apps can respond to internal errors in JSON, XML format and content.

ipolevoy commented 1 week ago

add documentation to the site