xp-forge / frontend

Web frontends
1 stars 1 forks source link

Exception handling API #25

Closed thekid closed 3 years ago

thekid commented 3 years ago

Implements features suggested in #24.

Examples

// Catch web.Error instances and map them to errors/{status}, all other exceptions
// will continue to display the basic xp-forge/web error pages
new Frontend($handlers, $templates, handling: (new Exceptions())->catch(Error::class));

// Report database connection and Neo4J query errors as 503 ("Service Unavailable"),
// supply catch-all to handle web.Errors as errors/{status} and all others as errors/500
new Frontend($handlers, $templates, handling: (new Exceptions())
  ->catch(SQLConnectException::class, 503)
  ->catch(QueryFailed::class, 503)
  ->catch(Throwable::class)
);

Logging can be done in the templates, e.g.:

{{#> layout}}
  {{#*inline "title"}}Internal error{{/inline}}
  {{#*inline "content"}}
    <div class="page-header">
      <h1>Skill Graph <small>{{t "Error 500"}}</small></h1>
    </div>

    <div class="ui error message">
      <div class="header">{{t "Internal error"}}</div>
      <p>{{cause.message}}</p>
    </div>

    {{! Exception including stack trace will be rendered to the server console !}}
    {{log request.uri.path "~" cause level="error"}}
  {{/inline}}
  {{#*inline "script"}}{{/inline}}
{{/layout}}

Note: Failure to find an error template will result in an 500 Internal Server Error being raised and displayed in the basic xp-forge/web error page