Type guard for roll-parser errors. Checks instanceof first, then a brand
carried on the error's prototype — so it still matches when instanceof
cannot, namely an error from an iframe or vm context, or from a second copy
of the library in node_modules at this version or newer.
Use it as the outer filter in every catch: anything it rejects came from
somewhere else and should be rethrown. The brand is what makes that sound — a
foreign error is never accepted just for carrying a code that happens to
collide with one of ours.
What it answers is "did this come from us", not "whose fault was it". A true
covers bad notation, a bad options object, and a broken invariant in here
alike, so it is the wrong test to hang a user-facing message on. Reach for
isNotationError for that.
Only this library's own error prototype carries the brand, so holding it is
proof of origin, and the code is trusted rather than re-validated: an error
from a newer minor passes with a code this build has never heard of, which is
what RollParserErrorCode being an open union already implies. A value
that forges the brand is out of scope, as it is for any brand check.
The one boundary it cannot cross is a worker. postMessage and
structuredClone rebuild an Error from message and stack alone,
discarding code, name, and the prototype with it, so the value that
arrives is no longer recognizable as anything. Send error.code yourself as
part of the message payload if the other side needs it.
Type guard for roll-parser errors. Checks
instanceoffirst, then a brand carried on the error's prototype — so it still matches wheninstanceofcannot, namely an error from an iframe orvmcontext, or from a second copy of the library innode_modulesat this version or newer.Use it as the outer filter in every
catch: anything it rejects came from somewhere else and should be rethrown. The brand is what makes that sound — a foreign error is never accepted just for carrying acodethat happens to collide with one of ours.What it answers is "did this come from us", not "whose fault was it". A
truecovers bad notation, a bad options object, and a broken invariant in here alike, so it is the wrong test to hang a user-facing message on. Reach for isNotationError for that.Only this library's own error prototype carries the brand, so holding it is proof of origin, and the
codeis trusted rather than re-validated: an error from a newer minor passes with a code this build has never heard of, which is what RollParserErrorCode being an open union already implies. A value that forges the brand is out of scope, as it is for any brand check.The one boundary it cannot cross is a worker.
postMessageandstructuredClonerebuild anErrorfrommessageandstackalone, discardingcode,name, and the prototype with it, so the value that arrives is no longer recognizable as anything. Senderror.codeyourself as part of the message payload if the other side needs it.