aboutsummaryrefslogtreecommitdiff
path: root/src/gleam_stdlib.js
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2021-09-09 22:09:06 +0100
committerLouis Pilfold <louis@lpil.uk>2021-09-09 22:09:06 +0100
commita595d4be50fe7e4e5a946bf5ed5878fa7bd42b64 (patch)
tree68c0077a2f7d6f9302768bfc7aea53feafc15eca /src/gleam_stdlib.js
parent16da5a271ae7a7decbf8424d9b2acad01343622e (diff)
downloadgleam_stdlib-a595d4be50fe7e4e5a946bf5ed5878fa7bd42b64.tar.gz
gleam_stdlib-a595d4be50fe7e4e5a946bf5ed5878fa7bd42b64.zip
Improve error messages
Diffstat (limited to 'src/gleam_stdlib.js')
-rw-r--r--src/gleam_stdlib.js19
1 files changed, 11 insertions, 8 deletions
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js
index 7ecbe11..b743c03 100644
--- a/src/gleam_stdlib.js
+++ b/src/gleam_stdlib.js
@@ -518,24 +518,27 @@ export function decode_bit_string(data) {
: decoder_error("BitString", data);
}
+function decode_tuple_error(size, data) {
+ return decoder_error(
+ `Tuple of at least ${size} element${size == 1 ? "" : "s"}`,
+ data
+ );
+}
+
export function decode_element(data, index) {
- let error = (size) =>
- decoder_error(
- `Tuple of at least ${size} element${size == 1 ? "" : "s"}`,
- data
- );
- if (!Array.isArray(data)) return error(index < 0 ? 1 : index + 1);
+ if (!Array.isArray(data))
+ return decode_tuple_error(index < 0 ? Math.abs(index) : index + 1, data);
if (index >= 0) {
if (index < data.length) {
return new Ok(data[index]);
} else {
- return error(index + 1);
+ return decode_tuple_error(index + 1, data);
}
} else {
if (Math.abs(index) <= data.length) {
return new Ok(data[data.length + index]);
} else {
- return error(Math.abs(index));
+ return decode_tuple_error(Math.abs(index), data);
}
}
}