summaryrefslogtreecommitdiffstats
path: root/toolkit/components/uniffi-js/js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /toolkit/components/uniffi-js/js
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/uniffi-js/js')
-rw-r--r--toolkit/components/uniffi-js/js/UniFFI.sys.mjs39
1 files changed, 39 insertions, 0 deletions
diff --git a/toolkit/components/uniffi-js/js/UniFFI.sys.mjs b/toolkit/components/uniffi-js/js/UniFFI.sys.mjs
new file mode 100644
index 0000000000..c754cc9339
--- /dev/null
+++ b/toolkit/components/uniffi-js/js/UniFFI.sys.mjs
@@ -0,0 +1,39 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// This JS module contains shared functionality for the generated UniFFI JS
+// code.
+
+// TypeError for UniFFI calls
+//
+// This extends TypeError to add support for recording a nice description of
+// the item that fails the type check. This is especially useful for invalid
+// values nested in objects/arrays/maps, etc.
+//
+// To accomplish this, the FfiConverter.checkType methods of records, arrays,
+// maps, etc. catch UniFFITypeError, call `addItemDescriptionPart()` with a
+// string representing the child item, then re-raise the exception. We then
+// join all the parts together, in reverse order, to create item description
+// strings like `foo.bar[123]["key"]`
+export class UniFFITypeError extends TypeError {
+ constructor(reason) {
+ super();
+ this.reason = reason;
+ this.itemDescriptionParts = [];
+ }
+
+ addItemDescriptionPart(part) {
+ this.itemDescriptionParts.push(part);
+ }
+
+ itemDescription() {
+ const itemDescriptionParts = [...this.itemDescriptionParts];
+ itemDescriptionParts.reverse();
+ return itemDescriptionParts.join("");
+ }
+
+ get message() {
+ return `${this.itemDescription()}: ${this.reason}`;
+ }
+}