-
Notifications
You must be signed in to change notification settings - Fork 30
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
how to handle error and redirect? #47
Comments
Hey @laptopmutia, Request.JS is just a wrapper around the Fetch API, so you should be able to handle errors as you would handle them with the Fetch API. See those examples here to see if it helps
Fetch API does follow redirects by default, but it won't render the redirected page so what you will have to do very likely is access the response of the redirect and propose a visit with Turbo, something like patch(`/sellings/${sellingId}/add-item`, {
body: { "selling": { "inventory_id": sCode } },
contentType: "application/json",
responseKind: "turbo-stream"
}).then((res) => {
if (res.redirected) {
Turbo.visit(new URL(res.url.toString(), document.baseURI))
}
}) I haven't tested the code above, but something like that may work. Just have in mind that this will end up making two requests to the redirected location: 1st -> Fetch API because it follows the redirect (if you set |
I just confused why the turbo stream is not rendered with 422 apparently the default behavior for stream processing is 200 ok I fix my code wiht this
is this code good? I kinda hesitate with the placement of async and await |
Yeah, Request.JS does only process a turbo stream if the response is
It is, you should be good with this. We could improve the docs to mention that the turbo stream will only be processed if the response is |
I think I prefer the later, but not sure about it since all the example about turbo and turbo-rails never use it, they tend to response the failed request with html page/response |
Yes, I don't see either why we wouldn't process the turbo stream. |
@marcelolx - My code is slightly different. const p = await post(to_url, { body: JSON.stringify(params) })
if (p.redirected) {
const redirect_url = p.response.url.toString()
Turbo.visit(new URL(redirect_url, document.baseURI))
} Why does turbo do all the magic body-tag swapping if I create a vanilla rails form - but here I need to do the heavy lifting? How can I tie the response into Turbo's generic behavior. like Turbo.magic(response) Which should handle the redirect - ie 1 request not 2 - and it should behave as normal for |
@brentgreeff Because it is Turbo that intercepts the form submission, submits it, and then handles the response while Request.JS is only a wrapper around the Fetch API (doing all this magic ). As far as I know, Turbo does not offer anything like that to handle the response of a request made outside of Turbo. Couldn't you submit a form and let Turbo intercept the form submission and consequently handle the response of it? |
@marcelolx - Yea, I do have examples of that in my code, but this is one of those forms in a table stories - I have 1 form per row. - not sure if it would be valid. - I need to collect values from different cells and collate params to generate my request.
|
@brentgreeff Yeah, I did look yesterday at Turbo and yes, it uses Fetch to make the requests. For some reason, I couldn't understand yet, Turbo is able to access the response of the redirected location and avoid another request to the redirected path but I couldn't figure out how (I did not invest much time, so I probably was missing something). If you find a solution, please share! We already process turbo streams, so if we could handle redirects (tell turbo to handle it somehow) it definitely would be valuable. btw, I'll take a look into this again once I have some free time. |
@marcelolx - thanks for investing the time. - |
any examples?
so I have rails application with turbo and turbo stream
then I use stimulus js with request.js to send a patch request but I don't know how to handle the error
here is my controller
I have tried to add this
format.turbo_stream { flash.now[:error] = "Item not found." }
it could render the flash message just fine
but I felt its wrong since its return 200 ok instead 422 error
The text was updated successfully, but these errors were encountered: