From 1035818108cacdfec2b813a5a770f483ff49253b Mon Sep 17 00:00:00 2001 From: Alec Gibson <12036746+alecgibson@users.noreply.github.com> Date: Tue, 5 Oct 2021 09:48:59 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20Throw=20a=20more=20useful=20mess?= =?UTF-8?q?age=20when=20subtypes=20are=20not=20invertible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `invert()` method assumes that all subtypes are invertible. However, the OT type `invert()` method is [optional][1], so not all types will have it implemented (`rich-text` is a notable example). This change checks for presence of the `invert()` function, and throws a more descriptive error if it's not present. [1]: https://github.com/ottypes/docs#optional-properties --- lib/json0.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/json0.js b/lib/json0.js index 9f538ee..abea8ff 100644 --- a/lib/json0.js +++ b/lib/json0.js @@ -70,6 +70,9 @@ json.invertComponent = function(c) { // handle subtype ops if (c.t && subtypes[c.t]) { + if (typeof subtypes[c.t].invert !== 'function') { + throw new Error("Subtype '" + c.t + "' is not invertible"); + } c_.t = c.t; c_.o = subtypes[c.t].invert(c.o); }