Skip to content
GalleryOpen Garphield

Events and errors

const stop = graph.on("nodeClick", ({ nodeId }) => {
detailPanel.show(nodeId);
});

Node ids are strings at the browser-driver boundary.

graph.on("stepChange", ({ index, title }) => {
chapterNav.select(index, title);
});

The event fires when the current story step changes through the host or the embedded player.

A document can load with parts the embed could not use. setDocument resolves with a warnings list instead of rejecting, so the rest of the network still shows:

const { warnings } = await graph.setDocument(project);
if (warnings.length > 0) {
showDocumentNotice(warnings.map(({ path, message }) => `${path}: ${message}`));
}

Each warning has a code, the path of the skipped part in the document you sent, and a message:

Code Skipped
TARGET_MISMATCH A binding whose source is a node field on a link channel, or a link field on a node channel. Set source.target to match the channel.
TYPE_MISMATCH A binding whose result type the channel does not take.
UNKNOWN_CHANNEL A binding on a channel this version does not have.
INVALID_DOCUMENT A binding entry that is not a binding at all, or a saved Group by that can’t be read (structure.groups with no levels, more than four, a repeat, an algorithm level that does not give nodes categories, or no links).
UNKNOWN_SOURCE A saved Group by level that names a field no node has a value for. The network shows without rings.

Codes and paths match those document validation reports. The warnings describe only the document the host sent, so every host receives them.

graph.on("error", ({ message }) => {
showGraphError(message);
});

Method calls reject when a project or parameter is invalid, a command is not granted, or the renderer does not reply before the timeout. When the embed refuses a command it ran, such as selecting a node the document does not have, the error is a GarphieldProtocolError whose code says why:

import { GarphieldProtocolError } from "garphield";
try {
await graph.select([nodeId]);
} catch (error) {
if (error instanceof GarphieldProtocolError && error.code === "VALIDATION") {
showMissingNode(nodeId);
}
}
try {
await graph.setDocument(candidate);
} catch (error) {
console.error("Garphield rejected the project", error);
}

Keep the unsubscribe functions returned by on(). Call them, then call destroy() before permanently removing the iframe.