Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting to handle error response same as success #2564

Closed
mozey opened this issue May 24, 2024 · 5 comments
Closed

Setting to handle error response same as success #2564

mozey opened this issue May 24, 2024 · 5 comments

Comments

@mozey
Copy link
Contributor

mozey commented May 24, 2024

When the response status is >= 400, the htmx:responseError event must be used, see this comment

Wouldn't it make sense to have a setting where htmx will still process the response as usual even for error responses?

For example, say I make a requests like this

<button hx-get="/contact/123" hx-trigger="click">Load contact</button>

And the response from the server, with status code 404, is this

<p id="err" hx-swap-oob="true" class="error">Contact 123 not found</p>

I would like htmx to still process hx-swap-oob the same as status code 200

Also posted here

@Telroshan
Copy link
Collaborator

Hey, you might want to look into the response-targets extension, which could suit your needs here

@mozey
Copy link
Contributor Author

mozey commented May 25, 2024

@Telroshan Thanks for the link, I've actually already written my own version of hx-target-error, and this extension could work for me. It also has other features that might be useful.

I don't understand why the Hypermedia API reponse must be handled differently for error status codes?

Maybe there is a way to do this?

addEventListener("htmx:responseError", (e) => {
  // Handle event same as success code
  htmx.handle(e)
})

@Telroshan
Copy link
Collaborator

There was discussion to add a similar feature to the lib's core but it didn't make it through.

However, please note htmx 2 will come with a responseHandling config property that will allow you to swap errors by default (opt-in). (It seems we have forgotten to add documentation for this feature yet, so bear with me for the link to the source code directly)

@mozey mozey closed this as completed May 27, 2024
@silmeth
Copy link

silmeth commented May 28, 2024

The main page of the docs also has a section on Modifying swapping behavior with events where it’s recommended to use the htmx::beforeSwap event, setting evt.detail.shouldSwap = true; to make it it processed normally. You can also which status codes should be treated this way.

(Myself, I’d still prefer to be able to declare any response should be handled directly in the triggering element with an attribute than writing a snippet of custom JS, though – but adding a simple even handler isn’t too bad)

@mozey
Copy link
Contributor Author

mozey commented May 28, 2024

@silmeth Thanks for the tip, to display errors by default, I'm setting shouldSwap as per the docs

addEventListener("htmx:beforeSwap", (e) => {
  if (e.detail.xhr.status >= 400) {
    e.detail.shouldSwap = true
  }
})

To override where the error is displayed, I can set hx-target-error from the response-targets extension

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants