facil-io / cstl

facil.io C STL - a Server Toolbox Library for C, including JSON processing, hash maps, dynamic arrays, binary strings and more.
https://facil.io
MIT License
68 stars 6 forks source link

Consider adding an `on_error` callback to `fio_http_settings_s` #25

Closed michaellenaghan closed 11 months ago

michaellenaghan commented 11 months ago

Both facil and its users might return, say, 404 errors. Consider adding an on_error callback to fio_http_settings_s so that users can create consistent styles for those error responses. For example, for some errors (like 404) the user might create a user-facing page that matches the style of the rest of the site.

I can think of some issues to consider.

First, I think that (by default) on_error should always be handled by the main thread. Otherwise, for example, the server might not be able to generate error responses on a heavily loaded server (i.e., if all threads are blocked).

Second, I think that an attempt to send an error from the on_error handler should, say, fall back to a very simple default error handler. The main point, really, is: consider recursion.

boazsegev commented 11 months ago

Did you look at the fio_http_send_error_response function?

It sends a pre-defined error file if it exists and (if non is found) sends an error response.

If you place a 404.html file in the root of the application it will be sent when using fio_http_send_error_response.

michaellenaghan commented 11 months ago

I saw it last week, and then apparently forgot about it. :-)

I'm used to thinking in terms of dynamically generated error pages for sites given templates, etc., but that's fine; error pages can be generated and serialized during startup. Best of both worlds?

Thanks!