N03
Error
Requests
N03 — A try with nowhere to pass its error
try gives a result's value, or returns its error from the action it is in, as the action's own. The action must say what it fails with, and that must be able to hold the error: the same type, an HttpError into an error enum's http case, or another error enum through its http case once its other cases are handled.
What the compiler said
error[N03]: `load` passes a failure up with `try`, and says nothing of what it fails with --> src/App.wf:9:5 | 9 | action load(id: String) { | ^^^^^^^^^^^^^^^^^^^^^^^^^ = help: Say so: `action load(…) -> !` = docs: https://webfluent.monzeromer.dev/errors/N03
Your editor offers this fix: Declare -> !. wf fix --code N03 applies it to every place it fits.
The fix
Broken
type User { id: String, name: String }
api Backend(base: "/api") {
get user(id: String) at "users/{id}" -> User!
}
store People {
state name = ""
action load(id: String) {
let user = try await Backend.user(id: id)
name = user.name
}
}
page P(path: "/", title: "T", description: "D") {
use People
Heading("People").h1
Button("Load") { on click { People.load("1") } }
Text(People.name)
}
Fixed
type User { id: String, name: String }
api Backend(base: "/api") {
get user(id: String) at "users/{id}" -> User!
}
store People {
state name = ""
action load(id: String) -> ! {
let user = try await Backend.user(id: id)
name = user.name
}
}
page P(path: "/", title: "T", description: "D") {
use People
Heading("People").h1
Button("Load") { on click { People.load("1") } }
Text(People.name)
}
Declare what the action fails with, or match the result where
the call is made and handle each case there.
Each example is a whole program: it is all of src/App.wf in a project with no other .wf file (trying an example).
Why this rule exists
Error.
try r gives the value of the result r when it is .ok, and when it is
.err it returns the error from the action at once, as the action's own.
So the action says what it fails with — -> T ! E, -> T!, -> ! E or
-> ! — and that has to be able to hold the error:
- the same error type is passed up as it is;
- an
HttpError(from a call declared-> T!) becomes an error enum's
.http(e);
- another error enum passes up only through its
httpcase: any other
case of it must be handled first, with match, since the action's enum
has no case to put it in.
In a handler, outside any action, try reports a failure where the page
reports errors instead.
lints cannot lower it: it would ship a broken page.