Errors
Written once, works everywhere. This is where the design saves the most code. The browser's handling is handleFailure in linker.js, the same file as in the PHP version.
Failure shape
{ "ok": false, "kind": "validation", "message": "有欄位需要修正。", "errors": { "email": ["Already taken"] }, "data": {}, "pipeline": [] }| kind | HTTP | When | What the browser does automatically |
|---|---|---|---|
validation | 200 | LinkerResponse::validation, or call returns a 422 error | Draws errors next to the matching fields |
policy | 200 / 401 / 403 / 404 | Not logged in (401), signature mismatch or a different user (403), an unregistered component (404), an unexposed method, a Referer mismatch, call returns 401/403 | A toast (showing message) |
service | 200 | The lock couldn't be acquired, not_found(), call returns 404 | A toast (showing message) |
expired | 409 | The instance is missing, expired or not this user's; the URL expired | A warning toast asking the user to reload |
conflict | 409 | rev mismatch (the response carries the server's rev); call returns 409 | A warning toast |
fatal | 200 | call returns any other error | A fixed-text toast "系統發生錯誤,已記錄。" (an error occurred and was logged) + an error report |
network | — | Produced only in the browser: fetch failed, 502/503/504 | A fixed-text toast + a site-wide offline notice |
Malformed requests (an invalid method, a non-integer rev) get 422, in Laravel's format (message + errors).
Two more statuses don't come from Linker itself, and the browser translates them too:
| HTTP | Source | The browser treats it as |
|---|---|---|
| 419 | An expired CSRF token (Laravel's web middleware) | expired "頁面驗證已過期,請重新整理後再試。" (page verification expired, reload) |
| 429 | Throttling: 120 per minute on the Linker endpoints (Throttle::per_minute(120) in runtime::routes) | service "操作過於頻繁,請稍後再試。" (too many requests) |
Modules write zero lines for any of this.
What an Err from call becomes
The laravel::Error that call returns is translated by HTTP status:
| Error status | kind | Common sources |
|---|---|---|
| 401 | policy "請先登入。" | laravel::abort!(401) |
| 403 | policy | A failed user.authorize(...), abort!(403) |
| 404 | service "找不到這筆資料。" | Model::find_or_fail, .or_abort(404) |
| 409 | conflict | abort!(409), a custom error with #[status(409)] |
| 422 | validation (with no errors) | abort!(422) |
| Anything else | fatal | Database errors, laravel::Error::msg, anything bubbling up through ? |
fatal is printed to stderr (linker: …). With APP_DEBUG=true the error message goes into the response's message, but the browser still shows only the fixed text; look at the response in DevTools for details.
For field errors, return validation(...)
abort!(422) has no errors, so the browser can't draw field errors. For validation failures, return LinkerResponse::validation([("field", "message")]).
Field errors
paintFieldErrors:
- Looks for fields only inside the nearest
<form>around the triggering element, matchingnameto the keys oferrors. If the triggering element isn't in a form, no field errors are drawn (the call still counts as failed). - Every call first removes the old
[data-linker-error]andaria-invalid, so "the last response wins". - The message is written with
textContentinto<span class="form-input-error" data-linker-error="field">, inserted after the field, and the field getsaria-invalid="true".
Is the bad field in a hidden step? Bring the page back with catch:
LinkerResponse::validation([("subject", "A subject is required.")])
.catch(Action::state_set("wizard", "step", 1))When the connection or the server is gone
| Situation | kind / reason | The user sees | Reported |
|---|---|---|---|
| fetch failed (offline, DNS) | network / offline | A toast + a site-wide offline notice | No |
| 502/503/504 that isn't Linker JSON | network / unavailable | A toast | No |
| 500 or unparseable | fatal | A toast | Yes |
Five rules:
fatal and network messages are fixed text (the
linkergroup inresources/js/cg-lang.json); server exception messages never appear on screen. policy / service / expired / conflict show the response'smessage, so write those messages for users.Input stays on screen and isn't written to persistent storage. Failures don't run
then, soform.resetdoesn't happen.Writes are never resent automatically. The request lost to a disconnect may already have been written, and resending would make two rows. Only explicitly marked reads are refetched when the connection comes back:
html<button data-linker-call="show" data-linker-args='{"page":2}' data-linker-refresh-on-reconnect>Refresh</button>Tokens aren't swapped just to resend. 419 is still
expired.The offline notice is handled by
linker/connectivity.js, with its text in the translation file.
Compared: the traditional way vs Linker
// Traditional: written again in every module
const res = await fetch('/api/customers', { method: 'POST', body });
if (res.status === 422) { paintErrors((await res.json()).errors); }
else if (res.status === 403) { toast('No permission'); }
else if (res.status === 409) { toast('Please reload'); }
else if (!res.ok) { showErrorCard(); }
else { reloadList(); }{# Linker: this is all #}
{{ cg_button("Create", type="submit", attrs={"data-linker-call": "create", "data-linker-args": "form"}) }}