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

catch does not work with fetch statement with if #588

Closed
shaozi opened this issue Nov 7, 2024 · 2 comments
Closed

catch does not work with fetch statement with if #588

shaozi opened this issue Nov 7, 2024 · 2 comments

Comments

@shaozi
Copy link

shaozi commented Nov 7, 2024

The if in fetch caused the catch not working.

What is wrong

Setup

client:

 <button
            class="btn"
            _="
               on click fetch /api/toggle/1
                        if it is 'on' then add .on to me else remove .on from me end
                        catch e log e
                        "
          >
           Toggle
          </button>

server: returnss a 404 page not found error.

Result

However, the catch e log e is never triggered.

What is working

But if I replace the if with a log, it works.

 <button
            class="btn"
            _="
               on click fetch /api/toggle/1
                        log it
                        catch e log e
                        "
          >
          Toggle
          </button>
@1cg
Copy link
Contributor

1cg commented Dec 18, 2024

believe it or not fetch() doesn't throw on a 404:

https://stackoverflow.com/questions/38235715/fetch-reject-promise-and-catch-the-error-if-status-is-not-ok

so what's happening here is that no exception is happening. In the second example it's the log it that is printing the response.

@1cg 1cg closed this as completed Dec 18, 2024
@shaozi
Copy link
Author

shaozi commented Dec 21, 2024

aha!
So in fetch how to check the return code?

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

2 participants