Errors

The errors seroval throws, their codes, and how to read them in production.

Every error that seroval throws because of the data is a SerovalError. Each one has a code such as SRV008, and each code has a page that says what it means and how to fix it.

Fields

FieldDescription
nameThe class name, such as SerovalMalformedNodeError.
codeThe code from the table below. Codes never change once released.
step'parse', 'serialize' or 'deserialize'. Errors that are never wrapped have none.
pathWhere in the value a parse error happened, as keys and indexes from the root, such as ['users', 0, 'avatar']. Only parse errors and errors from binary.serialize have it. Deserialization errors have none. See Where it failed.
causeThe error that a wrapper caught.

Some errors add fields with more detail, such as tag, limit, nodeType, nodeId or detail. The page of each code lists them.

How errors are wrapped

Each function wraps any error it meets in one of three errors, by the step that failed. The original error is in the cause property.

ErrorStepThrown by
SerovalParserErrorparseEvery serialize function, when it cannot read the value.
SerovalSerializationErrorserializeEvery function that produces code, when it cannot write the output.
SerovalDeserializationErrordeserializefromJSON, fromCrossJSON and createCrossDeserializer, when the input is invalid.

A wrapper takes the code of the seroval error it wraps, so code says what went wrong. A wrapper around any other error has the generic code of its step: SRV014, SRV015 or SRV016.

Binary mode does not wrap errors. binary.serialize and binary.deserialize pass the error itself to onError, and binary.deserialize rejects with it.

JavaScript
import {
  SerovalError,
  SerovalParserError,
  SerovalUnsupportedTypeError,
  serialize,
} from 'seroval';

try {
  serialize({ callback: () => {} });
} catch (error) {
  error instanceof SerovalError; // true
  error instanceof SerovalParserError; // true
  error.code; // 'SRV001'
  error.step; // 'parse'
  error.cause instanceof SerovalUnsupportedTypeError; // true
  error.cause.value; // the function
}

Errors caused by how a function is called, not by the data, are standard errors such as RangeError. They still have a code.

Where it failed

When the serializer cannot read a value, the error has a path from the root of the value to the part that failed. The wrapper and the error it wraps have the same path. In the development build, the message ends with the path:

JavaScript
import { serialize } from 'seroval';

try {
  serialize({ users: [{ name: 'Ada', avatar: () => {} }] });
} catch (error) {
  error.code; // 'SRV001'
  error.path; // ['users', 0, 'avatar']
  error.message;
  // The value [object Function] of type "function" cannot be parsed/serialized.
  // ...
  // At $.users[0].avatar
}

binary.serialize adds the same path to the error it hands to onError. See When serialization fails.

A number in path is an array index, and a string is a property key. The production message leaves the path out, because it may contain keys from the data. Read error.path instead.

Binary mode errors

binary.deserialize reports these when its input is invalid. See Untrusted input and the validation rules.

CodeErrorCause
SRV021SerovalMalformedBinarySourceErrorThe input ended too early, or has bytes after its end.
SRV022SerovalMalformedBinaryTypeErrorA node is invalid.
SRV023SerovalUnknownBinaryTypeErrorA node has an unknown type byte.
SRV024SerovalUnexpectedBinaryTypeErrorA node refers to a value of the wrong node type.
SRV025SerovalMissingBinaryRefErrorA node refers to an id that has no value.
SRV026RangeErrormaxArrayBufferLength or maxArrayLength is not a non-negative safe integer.
SRV027SerovalArrayBufferLimitErrorAn ArrayBuffer is larger than maxArrayBufferLength.
SRV028SerovalArrayLengthLimitErrorAn array is longer than maxArrayLength.

Binary mode also uses errors from the codes below:

  • binary.serialize reports SerovalUnsupportedTypeError and SerovalDepthLimitError.
  • binary.deserialize reports SerovalMissingPluginError and SerovalMissingReferenceForIdError.

Messages in production

In the production build, each message is one line with the code, the step and the link to the page of the code:

Text
Seroval SRV008 (deserialize): https://seroval.pages.dev/docs/errors/SRV008

An error with no step leaves out the parenthesis:

Text
Seroval SRV011: https://seroval.pages.dev/docs/errors/SRV011

Production messages never contain values or keys from the data. The development build has full messages that say what was wrong. Bundlers select it through the development export condition, which most of them set in development mode. The error classes and fields are the same in both builds, so instanceof and code checks work everywhere.

Reading the catalog

seroval exports the catalog that these messages come from, for tools that show or log errors:

ExportDescription
ERROR_CODESEvery code by its short name, such as ERROR_CODES.MalformedNode, which is 'SRV008'.
DOCS_ERRORS_URLThe base URL of the code pages. The page of a code is `${DOCS_ERRORS_URL}/${code}`.
getProdMessage(code, step?)The production message of a code.
getCatalogEntry(code)The class name, title and message function of a code. Returns undefined in the production build.
getDevMessage(code, details)The development message of a code. Returns '' in the production build.

The types ErrorCode, ErrorName, SerovalErrorStep, CatalogEntry and SerovalErrorShape describe the same values.

Codes

CodeErrorMeaning
SRV001SerovalUnsupportedTypeErrorA value cannot be serialized.
SRV002SerovalUnsupportedNodeErrorA node has a type that is not supported.
SRV003SerovalMissingPluginErrorA plugin for a tag is missing.
SRV004SerovalMissingInstanceErrorA required instance is missing.
SRV005SerovalMissingReferenceErrorA reference is missing (deprecated).
SRV006SerovalMissingReferenceForIdErrorA reference id is missing.
SRV007SerovalUnknownTypedArrayErrorA typed array has a type that seroval does not know.
SRV008SerovalMalformedNodeErrorA node has the wrong shape.
SRV009SerovalConflictedNodeIdErrorTwo nodes have the same id.
SRV010SerovalDepthLimitErrorThe depth limit was reached.
SRV011SerovalAbortedErrorThe cross deserializer was aborted.
SRV012SerovalLiveStreamErrorA live stream was used in a wrong state.
SRV013SerovalPluginValidationErrorA plugin value failed its validator.
SRV014SerovalParserErrorThe parser failed with an error that is not from seroval.
SRV015SerovalSerializationErrorThe serializer failed with an error that is not from seroval.
SRV016SerovalDeserializationErrorThe deserializer failed with an error that is not from seroval.
SRV017SerovalStreamConsumedErrorA stream already has a consumer.
SRV018SerovalStreamOutputErrorThe stream parser has no output callback.
SRV019RangeErrorThe maxBase64Length option is invalid.
SRV020SerovalBase64LimitErrorAn ArrayBuffer is larger than maxBase64Length.
SRV021SerovalMalformedBinarySourceErrorThe binary input is incomplete or has extra bytes.
SRV022SerovalMalformedBinaryTypeErrorA binary node has the wrong shape.
SRV023SerovalUnknownBinaryTypeErrorA binary node has a type that is not known.
SRV024SerovalUnexpectedBinaryTypeErrorA binary node is followed by a node of the wrong type.
SRV025SerovalMissingBinaryRefErrorA binary node refers to a value that was not declared.
SRV026RangeErrorA binary limit option is invalid.
SRV027SerovalArrayBufferLimitErrorAn ArrayBuffer is larger than maxArrayBufferLength.
SRV028SerovalArrayLengthLimitErrorAn array is longer than maxArrayLength.