Debugging a running page
Where a running page reports a mistake the types do not catch: a panic on the server while the page is built, a line in the browser's console, and a failed call that the page can show.
Debugging a running page
A mistake the types cannot hold panics while the page is built: two elements with one Id, a computed field that no Page::computed call names, or a list item used outside its closure. The server prints the panic with its message and answers nothing, so the browser reports an empty response. A test that renders each page with Page::render finds these before a visitor does, as Testing handlers shows.
use hypersig::html::{Id, div, input, label};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Signup {
email: String,
backup: String,
}
const EMAIL: Id = Id::new("email");
let page = Page::new("Sign up", Signup { email: String::new(), backup: String::new() }).child(
div()
.child(label().label_for(&EMAIL).text("Email"))
.child(input().id(EMAIL).bind(Signup::email()))
// A second field copied from the first still carries its id.
.child(input().id(EMAIL).bind(Signup::backup())),
);
// panicked: two elements carry the id `email` where the runtime can reach
// both, so its statements act on whichever comes first: give one of them
// another name
let _ = page.render();What panics while the page is built
Page::render, and Page::simulate with it, panics on each of these:
- Two elements that carry one
NodeRefname, oneFileInputname, or one id fromId::new, with or without keys, where the runtime can reach both. Each item of a list may carry the same name, and two ids fromId::from_textmay repeat. - An element inside a list template whose id is set with
El::id. - A field marked
#[signals(computed)]that noPage::computedcall names, two calls that name one signal, or a computed signal that reads itself or one declared after it. A page that reads a computed field of a nested struct that no call names panics too. - An expression or a statement that reads a list item outside every list that renders it.
- A signal struct that holds an integer outside -2^53 to 2^53, which the browser would round, or one whose
Serializeimpl returns an error. - In a debug build, a
Page::langthat names the language of anIntlLocalewhile the page formats only with others.
The browser's console
In the browser, the runtime writes each problem it meets to the console with the prefix hypersig:, such as an expression that threw, an element patch whose id is not on the page, or two list items with one key. Open the developer tools' console first when a button does nothing.
hypersig: POST /save failed with 409: Someone else saved these notes first.
hypersig: no element with id "todo-3" to patch| Line | What happened | What the page does |
|---|---|---|
hypersig: METHOD PATH failed with ... | An action, a stream, or an upload got a status outside 2xx, or no answer | Sets the call's error and status signals, as the next section shows |
hypersig: no element with id "..." to patch | An element patch or a fragment names an id that is not on the page | Leaves the page as it was |
hypersig: duplicate key ... in a list; matching it by position | Two items of a keyed list have one key | Matches the second item by its position |
hypersig: no element with ... | A statement of a NodeRef, such as focus, names an element that is not on the page | Runs nothing |
hypersig: no file input named "..." | An upload names a FileInput that is not on the page | Sends the arguments without files |
hypersig: and an error | An expression or a statement threw | Skips that binding or statement |
A call that fails
pub fn error(self, message: Sig<S, String>) -> Self
pub fn status(self, status: Sig<S, i32>) -> SelfAn action or a stream that answers with a status outside 2xx, or gets no answer, fails. The runtime then writes hypersig: METHOD PATH failed with and the status and body to the console. It dispatches hs-error on <body>, a CustomEvent whose detail holds the status and the message, for a script such as an error tracker. It also sets the signals the call names: Call::error gets the body of the answer, or the status and its reason when the body is empty, or the browser's message when no answer arrived, and Call::status gets the status, or 0 when no answer arrived. A handler that fails with Result<_, E> writes the sentence the page shows, and the page tells one failure from another by comparing the status with a StatusCode, not by reading the text.
use hypersig::html::{Role, button, div, p};
use hypersig::{Action, Patches, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Notes {
problem: String,
failure: i32,
}
hypersig::routes! {
fn actions(Router<()>);
action SAVE: Action<Notes> = post "/save" => save;
}
async fn save((): (), (): ()) -> Result<Patches<Notes>, (StatusCode, &'static str)> {
Err((StatusCode::CONFLICT, "Someone else saved these notes first."))
}
let conflict = Notes::failure().equals(i32::from(StatusCode::CONFLICT.as_u16()));
let page = div::<Notes>()
.child(
button()
.on_click(SAVE.call().error(Notes::problem()).status(Notes::failure()))
.text("Save"),
)
.child(p().role(Role::Alert).bind_text(Notes::problem()))
.child(p().show(conflict).text("Reload the page to see their version."));error takes a String signal and status an i32 signal of the same page, so a call cannot write a status into a text field. Busy and failed calls covers the indicator and the other signals of a call.
use hypersig::html::button;
use hypersig::{Action, Patches, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Notes {
problem: String,
failure: i32,
}
hypersig::routes! {
fn actions(Router<()>);
action SAVE: Action<Notes> = post "/save" => save;
}
// error[E0308]: mismatched types: expected `Sig<Notes, i32>`, found `Sig<Notes, String>`
let save = button::<Notes>().on_click(SAVE.call().status(Notes::problem()));What a debug build adds
A tab showing a page from a debug build reloads once a new server answers in place of the one that rendered the page, as after cargo hypersig dev rebuilds it, and keeps the signals the visitor changed, where the route and the struct's fields are the same. A page from a release build does neither. The development server covers cargo hypersig dev.
Next
- Testing handlers: rendering each page in a test, so its panics show before a visitor's request.
- Errors and logging: the status and the sentence a handler answers a failure with, and what the server logs.
- Upgrading: the errors a newer release raises in code written for an older one.