Error handling and diagnostics

Read diagnostics and handle failures. Code examples in the DocStudio developer reference.

Diagnostics

Every core function reports problems as diagnostics instead of throwing. A diagnostic has a code, a severity (error or warning), a message and a JSON Pointer path.

Rules of thumb

  • Check result.ok or the presence of result.tree before using a compile result.
  • Treat error diagnostics as failures and warning diagnostics as information to log.
  • Use path to find the node: the pointer starts at the template, for example /body/0/content/1.
  • Do not parse messages. Messages are for people, codes are stable.

Other failure shapes

API Failure
compile, render diagnostics array, never an exception
PDF client and service { ok: false, status, diagnostics }
Storage ports { ok: false, code, message }
Editor commands { ok: false, reason, diagnostics } and the state does not change
Host HTTP handler A status code and { ok: false, error: { code, message } } with fixed text

Every diagnostic code is listed in Diagnostics.