xp-forge / frontend

Web frontends
1 stars 1 forks source link

Error handling #24

Closed thekid closed 3 years ago

thekid commented 3 years ago

Currently, errors raised from handlers yield a default error page, which is quite basic (see also xp-forge/web#53 and xp-forge/web#39):

image

To be able to handle these situations more gracefully, I propose the following changes:

1 - Map the above to 404

This error occurs when the delegates in use do not return a target for a given HTTP method and path.

2 - Map exceptions

All exceptions other than web.Error instances are mapped to the errorcode 500. We could be more specific about this:

$frontend= (new Frontend(new HandlersIn('org.example.example.web'), $templates))
  ->handling((new Exceptions())
    ->catch(SQLException::class, 503)  // uses template "error/503" w/ status 503, includes stack trace
    ->catch(LinkExpired::class, 404)   // uses template "error/404" w/ status 404, no stack trace for 4XX errors!
    ->catch(InvalidOrder::class, fn($e) => View::error(503, 'invalid-order')),
    ->catch(Throwable::class)          // catch-all, uses errors/{status} for web.Error, errors/500 for others
  )
;

Instances of Exceptions could be returned by a reusable library you use in all your projects.

thekid commented 3 years ago

Released in https://github.com/xp-forge/frontend/releases/tag/v3.4.0