Skip to content

Commit 9990f46

Browse files
authored
add context for errors (#822)
1 parent e466abe commit 9990f46

File tree

13 files changed

+23
-23
lines changed

13 files changed

+23
-23
lines changed

src/legends.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function legend(options = {}) {
2727
);
2828
}
2929
}
30-
throw new Error("unknown legend type");
30+
throw new Error("unknown legend type; no scale found");
3131
}
3232

3333
export function exposeLegends(scales, defaults = {}) {

src/plot.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function plot(options = {}) {
5858

5959
// Initialize the marks’ channels, indexing them by mark and scale as needed.
6060
for (const mark of marks) {
61-
if (stateByMark.has(mark)) throw new Error("duplicate mark");
61+
if (stateByMark.has(mark)) throw new Error("duplicate mark; each mark must be unique");
6262
const markFacets = facets === undefined ? undefined
6363
: mark.facet === "auto" ? mark.data === facet.data ? facetsIndex : undefined
6464
: mark.facet === "include" ? facetsIndex
@@ -240,7 +240,7 @@ export class Mark {
240240
}
241241
if (name == null) throw new Error("missing channel name");
242242
const key = `${name}`;
243-
if (key === "__proto__") throw new Error("illegal channel name");
243+
if (key === "__proto__") throw new Error(`illegal channel name: ${key}`);
244244
if (names.has(key)) throw new Error(`duplicate channel: ${key}`);
245245
names.add(key);
246246
return true;
@@ -292,7 +292,7 @@ class Render extends Mark {
292292
constructor(render) {
293293
super();
294294
if (render == null) return;
295-
if (typeof render !== "function") throw new TypeError("invalid mark");
295+
if (typeof render !== "function") throw new TypeError("invalid mark; missing render function");
296296
this.render = render;
297297
}
298298
render() {}

src/scales.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function Scales(channels, {
4545
insetLeft = inset !== undefined ? inset : key === "x" ? globalInsetLeft : 0 // not fx
4646
} = scaleOptions || {};
4747
if (transform == null) transform = undefined;
48-
else if (typeof transform !== "function") throw new Error("invalid scale transform");
48+
else if (typeof transform !== "function") throw new Error(`invalid scale transform; not a function`);
4949
scale.percent = !!percent;
5050
scale.transform = transform;
5151
if (key === "x" || key === "fx") {
@@ -385,10 +385,10 @@ export function scale(options = {}) {
385385
for (const key in options) {
386386
if (!registry.has(key)) continue; // ignore unknown properties
387387
if (!isScaleOptions(options[key])) continue; // e.g., ignore {color: "red"}
388-
if (scale !== undefined) throw new Error("ambiguous scale definition");
388+
if (scale !== undefined) throw new Error("ambiguous scale definition; multiple scales found");
389389
scale = exposeScale(normalizeScale(key, options[key]));
390390
}
391-
if (scale === undefined) throw new Error("invalid scale definition");
391+
if (scale === undefined) throw new Error("invalid scale definition; no scale found");
392392
return scale;
393393
}
394394

src/scales/quantitative.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export function ScaleThreshold(key, channels, {
150150
range = interpolate !== undefined ? quantize(interpolate, domain.length + 1) : registry.get(key) === color ? ordinalRange(scheme, domain.length + 1) : undefined,
151151
reverse
152152
}) {
153-
if (!pairs(domain).every(([a, b]) => ascending(a, b) <= 0)) throw new Error("non-ascending domain");
153+
if (!pairs(domain).every(([a, b]) => ascending(a, b) <= 0)) throw new Error(`the ${key} scale has a non-ascending domain`);
154154
if (reverse) range = reverseof(range); // domain ascending, so reverse range
155155
return {type: "threshold", scale: scaleThreshold(domain, range === undefined ? [] : range).unknown(unknown), domain, range};
156156
}

src/style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export function* groupIndex(I, position, {z}, channels) {
244244
export function maybeClip(clip) {
245245
if (clip === true) return "frame";
246246
if (clip == null || clip === false) return false;
247-
throw new Error(`clip method not implemented: ${clip}`);
247+
throw new Error(`invalid clip method: ${clip}`);
248248
}
249249

250250
export function applyIndirectStyles(selection, mark, {width, height, marginLeft, marginRight, marginTop, marginBottom}) {

src/transforms/bin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,15 @@ function maybeThresholds(thresholds, interval) {
242242
case "sturges": return thresholdSturges;
243243
case "auto": return thresholdAuto;
244244
}
245-
throw new Error("invalid thresholds");
245+
throw new Error(`invalid thresholds: ${thresholds}`);
246246
}
247247
return thresholds; // pass array, count, or function to bin.thresholds
248248
}
249249

250250
// Unlike the interval transform, we require a range method, too.
251251
function maybeRangeInterval(interval) {
252252
interval = maybeInterval(interval);
253-
if (!isInterval(interval)) throw new Error("invalid interval");
253+
if (!isInterval(interval)) throw new Error(`invalid interval: ${interval}`);
254254
return interval;
255255
}
256256

src/transforms/group.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export function maybeReduce(reduce, value) {
223223
case "y1": return reduceY1;
224224
case "y2": return reduceY2;
225225
}
226-
throw new Error("invalid reduce");
226+
throw new Error(`invalid reduce: ${reduce}`);
227227
}
228228

229229
export function maybeSubgroup(outputs, Z, F, S) {

src/transforms/interval.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function maybeInterval(interval) {
1616
range: (lo, hi) => range(Math.ceil(lo / n), hi / n).map(x => n * x)
1717
};
1818
}
19-
if (typeof interval.floor !== "function" || typeof interval.offset !== "function") throw new Error("invalid interval");
19+
if (typeof interval.floor !== "function" || typeof interval.offset !== "function") throw new Error("invalid interval; missing floor or offset function");
2020
return interval;
2121
}
2222

src/transforms/map.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function maybeMap(map) {
4646
case "rank": return mapFunction(rank);
4747
case "quantile": return mapFunction(rankQuantile);
4848
}
49-
throw new Error("invalid map");
49+
throw new Error(`invalid map: ${map}`);
5050
}
5151

5252
function rankQuantile(V) {
@@ -58,7 +58,7 @@ function mapFunction(f) {
5858
return {
5959
map(I, S, T) {
6060
const M = f(take(S, I));
61-
if (M.length !== I.length) throw new Error("mismatched length");
61+
if (M.length !== I.length) throw new Error("map function returned a mismatched length");
6262
for (let i = 0, n = I.length; i < n; ++i) T[I[i]] = M[i];
6363
}
6464
};

src/transforms/normalize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function normalize(basis) {
2828
case "sum": return normalizeSum;
2929
case "extent": return normalizeExtent;
3030
}
31-
throw new Error("invalid basis");
31+
throw new Error(`invalid basis: ${basis}`);
3232
}
3333

3434
function normalizeBasis(basis) {

0 commit comments

Comments
 (0)