anze3db,
@anze3db@fosstodon.org avatar

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?

video/mp4

dunsap,
@dunsap@fosstodon.org avatar

@anze3db If I'm not wrong the response-targets extension can be used for that? 🙂
https://htmx.org/extensions/response-targets/

anze3db,
@anze3db@fosstodon.org avatar

@dunsap oh I didn't know about this extension. Looks cool, thanks for sharing!

benjaoming,
@benjaoming@social.data.coop avatar
anze3db,
@anze3db@fosstodon.org avatar

@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!

aral,
@aral@mastodon.ar.al avatar

@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)

aral,
@aral@mastodon.ar.al avatar

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.

(2/2)

anze3db,
@anze3db@fosstodon.org avatar

@aral Benjamin pointed out that there is an extension that allows you to define hx-target-error: https://social.data.coop/@benjaoming/112439119762935983

aral,
@aral@mastodon.ar.al avatar

@anze3db Ah nice :)

simeon,
@simeon@indiehackers.social avatar

@aral @anze3db That’s what I ended up doing which, while not ideal, was OK because I needed some basic interactivity as well.

anze3db,
@anze3db@fosstodon.org avatar

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.

video/mp4

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • ngwrru68w68
  • rosin
  • GTA5RPClips
  • osvaldo12
  • love
  • Youngstown
  • slotface
  • khanakhh
  • everett
  • kavyap
  • mdbf
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • megavids
  • InstantRegret
  • normalnudes
  • tacticalgear
  • cubers
  • ethstaker
  • modclub
  • cisconetworking
  • Durango
  • anitta
  • Leos
  • tester
  • provamag3
  • JUstTest
  • All magazines