Skip to content

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 ​

json
{ "ok": false, "kind": "validation", "message": "有欄位需要修正。", "errors": { "email": ["Already taken"] }, "data": {}, "pipeline": [] }
kindHTTPWhenWhat the browser does automatically
validation200LinkerResponse::validation, or call returns a 422 errorDraws errors next to the matching fields
policy200 / 401 / 403 / 404Not logged in (401), signature mismatch or a different user (403), an unregistered component (404), an unexposed method, a Referer mismatch, call returns 401/403A toast (showing message)
service200The lock couldn't be acquired, not_found(), call returns 404A toast (showing message)
expired409The instance is missing, expired or not this user's; the URL expiredA warning toast asking the user to reload
conflict409rev mismatch (the response carries the server's rev); call returns 409A warning toast
fatal200call returns any other errorA fixed-text toast "系統發生錯誤,已記錄。" (an error occurred and was logged) + an error report
network—Produced only in the browser: fetch failed, 502/503/504A 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:

HTTPSourceThe browser treats it as
419An expired CSRF token (Laravel's web middleware)expired "頁面驗證已過期,請重新整理後再試。" (page verification expired, reload)
429Throttling: 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 statuskindCommon sources
401policy "請先登入。"laravel::abort!(401)
403policyA failed user.authorize(...), abort!(403)
404service "找不到這筆資料。"Model::find_or_fail, .or_abort(404)
409conflictabort!(409), a custom error with #[status(409)]
422validation (with no errors)abort!(422)
Anything elsefatalDatabase 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, matching name to the keys of errors. 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] and aria-invalid, so "the last response wins".
  • The message is written with textContent into <span class="form-input-error" data-linker-error="field">, inserted after the field, and the field gets aria-invalid="true".

Is the bad field in a hidden step? Bring the page back with catch:

rust
LinkerResponse::validation([("subject", "A subject is required.")])
    .catch(Action::state_set("wizard", "step", 1))

When the connection or the server is gone ​

Situationkind / reasonThe user seesReported
fetch failed (offline, DNS)network / offlineA toast + a site-wide offline noticeNo
502/503/504 that isn't Linker JSONnetwork / unavailableA toastNo
500 or unparseablefatalA toastYes

Five rules:

  1. fatal and network messages are fixed text (the linker group in resources/js/cg-lang.json); server exception messages never appear on screen. policy / service / expired / conflict show the response's message, so write those messages for users.

  2. Input stays on screen and isn't written to persistent storage. Failures don't run then, so form.reset doesn't happen.

  3. 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>
  4. Tokens aren't swapped just to resend. 419 is still expired.

  5. The offline notice is handled by linker/connectivity.js, with its text in the translation file.

Compared: the traditional way vs Linker ​

js
// 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(); }
jinja
{# Linker: this is all #}
{{ cg_button("Create", type="submit", attrs={"data-linker-call": "create", "data-linker-args": "form"}) }}