Feature Request: HTTP Exception Handling #425
Replies: 2 comments
-
The way I was thinking about it was that the 404 handler would be the function provided to justpy as a parameter. All the valid URLs are handled as routes and when no route is matched, the function is run. Let me know what you think. For the 500 exception, there does need to be a change and I will add that. |
Beta Was this translation helpful? Give feedback.
-
I think that sounds like the best way to handle the 404. When I followed the trail through justpy.routing.Route to starlette.routing.compile_path(), I got the impression that Starlette falls back to matching everything to '/' ... and I both a) didn't see a quick workaround and b) reckoned I was missing something, because that just seems like a silly rule to have. I figured I'd leave it for you. :) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a interest for custom HTTP Exception pages — specifically 404 and 500. I tried passing exception_handlers to the Starlette() instance, as specified at https://www.starlette.io/exceptions/, but I could not get it to work. I’m guessing you can, but meanwhile, I went and kludged together a workaround. Notes, below, in case anyone finds it useful.
Additional import in justpy.py- necessary for both 404 and 500
from starlette.responses import HTMLResponse
Additional parameters to justpy.justpy() — http_404 and http_500. These should be set to a function that, when called, returns HTML.
Again, I’ve set this up to pass HTML, so anything that generates HTML — a string, rendering a Jinja template, etc. — could go here.
And I pass them to my justpy instance
Note, I’m not passing a func to justpy(). More on that below.
500 handler
The block that begins at line 141 and starts with
if inspect.iscoroutinefunction(func_to_run):
has been modified as follows
404 handler
After the loop through Route.instances (lines 134-138), this has been added
Note, that this works by testing for initial_func, defined a bit higher on the page, and therefore only works if NO function is passed to justpy.justpy(). I believe that addressing that case might involve a new approach to matching routes, that doesn’t match ‘/‘ by default, and I do not yet grasp the architecture enough to fiddle with that knob.
Beta Was this translation helpful? Give feedback.
All reactions