|
28 | 28 | } |
29 | 29 |
|
30 | 30 | if (!response.ok) { |
| 31 | + const eventDetail = { |
| 32 | + message: "Problem with loading the content", |
| 33 | + data: null |
| 34 | + } |
| 35 | + document.dispatchEvent(new CustomEvent(`global:UnhandledHydroError`, { detail: { data: eventDetail } })); |
31 | 36 | throw new Error(`HTTP error! status: ${response.status}`); |
32 | 37 | } else { |
33 | 38 | let data = await response.text(); |
|
102 | 107 | const body = JSON.stringify(eventData.data); |
103 | 108 | const hydroEvent = el.getAttribute("x-on-hydro-event"); |
104 | 109 | const wireEventData = JSON.parse(hydroEvent); |
105 | | - await hydroRequest(el, url, 'application/json', body, null, wireEventData, operationId); |
| 110 | + await hydroRequest(el, url, 'application/json', body, 'event', wireEventData, operationId); |
106 | 111 | } |
107 | 112 |
|
108 | 113 | async function hydroBind(el) { |
|
130 | 135 | const bindAlreadyInitialized = [...binding[component.id].formData].length !== 0; |
131 | 136 |
|
132 | 137 | binding[component.id].formData.set(propertyName, value); |
133 | | - |
134 | | - if (bindAlreadyInitialized){ |
| 138 | + |
| 139 | + if (bindAlreadyInitialized) { |
135 | 140 | return Promise.resolve(); |
136 | 141 | } |
137 | | - |
| 142 | + |
138 | 143 | if (binding[component.id].timeout) { |
139 | 144 | clearTimeout(binding[component.id].timeout); |
140 | 145 | } |
|
258 | 263 | }); |
259 | 264 |
|
260 | 265 | if (!response.ok) { |
261 | | - const contentType = response.headers.get("content-type"); |
262 | | - let eventDetail = {}; |
263 | | - |
264 | | - try { |
265 | | - if (contentType && contentType.indexOf("application/json") !== -1) { |
266 | | - const json = await response.json(); |
267 | | - eventDetail.message = json?.message || "Unhandled exception"; |
268 | | - eventDetail.data = json?.data; |
269 | | - } else { |
270 | | - eventDetail.message = await response.text(); |
| 266 | + if (response.status === 400 && config.Antiforgery && response.headers.get("Refresh-Antiforgery-Token")) { |
| 267 | + const json = await response.json(); |
| 268 | + config.Antiforgery.Token = json.token; |
| 269 | + } else if (response.status === 403) { |
| 270 | + if (type !== 'event') { |
| 271 | + document.dispatchEvent(new CustomEvent(`global:UnhandledHydroError`, { detail: { data: { message: "Unauthorized access" } } })); |
| 272 | + } |
| 273 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 274 | + } else { |
| 275 | + const contentType = response.headers.get("content-type"); |
| 276 | + let eventDetail = {}; |
| 277 | + |
| 278 | + try { |
| 279 | + if (contentType && contentType.indexOf("application/json") !== -1) { |
| 280 | + const json = await response.json(); |
| 281 | + eventDetail.message = json?.message || "Unhandled exception"; |
| 282 | + eventDetail.data = json?.data; |
| 283 | + } else { |
| 284 | + eventDetail.message = await response.text(); |
| 285 | + } |
| 286 | + } catch { |
| 287 | + // ignore |
271 | 288 | } |
272 | | - } catch { |
273 | | - // ignore |
274 | | - } |
275 | 289 |
|
276 | | - document.dispatchEvent(new CustomEvent(`global:UnhandledHydroError`, { detail: { data: eventDetail } })); |
277 | | - throw new Error(`HTTP error! status: ${response.status}`); |
| 290 | + if (type !== 'event') { |
| 291 | + document.dispatchEvent(new CustomEvent(`global:UnhandledHydroError`, { detail: { data: eventDetail } })); |
| 292 | + } |
| 293 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 294 | + } |
278 | 295 | } else { |
279 | 296 | const skipOutputHeader = response.headers.get('Hydro-Skip-Output'); |
280 | 297 |
|
@@ -519,10 +536,10 @@ document.addEventListener('alpine:init', () => { |
519 | 536 | let timeout = 0; |
520 | 537 |
|
521 | 538 | const eventHandler = async (event) => { |
522 | | - if (event === 'submit' || event === 'click'){ |
| 539 | + if (event === 'submit' || event === 'click') { |
523 | 540 | event.preventDefault(); |
524 | 541 | } |
525 | | - |
| 542 | + |
526 | 543 | clearTimeout(timeout); |
527 | 544 |
|
528 | 545 | timeout = setTimeout(async () => { |
|
0 commit comments