One thing that I'm missing with HTMX and Django is proper error handling.
By default HTMX swallows any response that is not a 2xx, which might leave your users hanging on an endless loading state.
Does anyone have a good solution for this that can be reused between projects?
As a stopgap solution I solved this by returning a 200 response with an error content, but I am curious how others deal with errors and HTMX.
@anze3db If I'm not wrong the `response-targets` extension can be used for that?
https://htmx.org/extensions/response-targets/
@dunsap oh I didn't know about this extension. Looks cool, thanks for sharing!
@anze3db I’m not sure there’s a better way than what you’re doing right now that doesn’t involve listening for the responseError event and writing client-side JS. They could add hx-error-swap / hx-error-target, for example, to implement this. Might be worth suggesting. (1/2)
Also, not immediately relevant to your current issue but something that might be worth considering for the future: using the htmx websocket extension, you can basically implement a streaming HTML approach (example using Kitten: https://ar.al/2024/03/08/streaming-html/) where you can just stream errors to the page as they happen.
#htmx #webSocket #Kitten #SmallWeb #StreamingHTML #errorHandling (2/2)
@aral Benjamin pointed out that there is an extension that allows you to define hx-target-error: https://social.data.coop/@benjaoming/112439119762935983
@anze3db Ah nice :)
@anze3db TIL: "toasts" - maybe this is what you want? https://blog.benoitblanchon.fr/django-htmx-toasts/
Or (with Go and HTMX) https://themurph.hashnode.dev/go-beyond-the-basics-mastering-toast-notifications-with-go-and-htmx
@benjaoming For this particular example I think a local error text is more appropriate than a global toast, but I will also need toasts for other things so this is useful, thanks again!