Skip to content

Commit b4127a8

Browse files
committed
Adding context support to error messages
1 parent 24d5433 commit b4127a8

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/jsonselect.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
return (new Function("return " + str))();
1616
} catch(e) {
17-
te("ijs");
17+
te("ijs", e.message);
1818
}
1919
}
2020

@@ -40,8 +40,8 @@
4040
};
4141

4242
// throw an error message
43-
function te(ec) {
44-
throw new Error(errorCodes[ec]);
43+
function te(ec, context) {
44+
throw new Error(errorCodes[ec] + " in '" + context + "'");
4545
}
4646

4747
// THE LEXER
@@ -90,9 +90,9 @@
9090
else if (m[3]) a = [off, toks.typ, m[0]];
9191
else if (m[4]) a = [off, toks.psc, m[0]];
9292
else if (m[5]) a = [off, toks.psf, m[0]];
93-
else if (m[6]) te("upc");
93+
else if (m[6]) te("upc", str);
9494
else if (m[8]) a = [off, m[7] ? toks.ide : toks.str, jsonParse(m[8])];
95-
else if (m[9]) te("ujs");
95+
else if (m[9]) te("ujs", str);
9696
else if (m[10]) a = [off, toks.ide, m[10].replace(/\\([^\r\n\f0-9a-fA-F])/g,"$1")];
9797
return a;
9898
}
@@ -156,11 +156,11 @@
156156
if (l && l[1] === '(') {
157157
lhs = exprParse2(str, l[0]);
158158
var p = exprLex(str, lhs[0]);
159-
if (!p || p[1] !== ')') te('epex');
159+
if (!p || p[1] !== ')') te('epex', str);
160160
off = p[0];
161161
lhs = [ '(', lhs[1] ];
162162
} else if (!l || (l[1] && l[1] != 'x')) {
163-
te("ee");
163+
te("ee", str + " - " + ( l[1] && l[1] ));
164164
} else {
165165
lhs = ((l[1] === 'x') ? undefined : l[2]);
166166
off = l[0];
@@ -170,7 +170,7 @@
170170
var op = exprLex(str, off);
171171
if (!op || op[1] == ')') return [off, lhs];
172172
else if (op[1] == 'x' || !op[1]) {
173-
te('bop');
173+
te('bop', str + " - " + ( op[1] && op[1] ));
174174
}
175175

176176
// tail recursion to fetch the rhs expression
@@ -238,13 +238,13 @@
238238
a = [];
239239
off = s[0];
240240
} else if (s[1] === ")") {
241-
if (!nested) te("ucp");
241+
if (!nested) te("ucp", s[1]);
242242
readParen = 1;
243243
off = s[0];
244244
break;
245245
}
246246
}
247-
if (nested && !readParen) te("mcp");
247+
if (nested && !readParen) te("mcp", str);
248248
if (am) am.push(a);
249249
var rv;
250250
if (!nested && hints.usesSiblingOp) {
@@ -322,10 +322,10 @@
322322
if (l === undefined) {
323323
break;
324324
} else if (l[1] === toks.ide) {
325-
if (s.id) te("nmi");
325+
if (s.id) te("nmi", l[1]);
326326
s.id = l[2];
327327
} else if (l[1] === toks.psc) {
328-
if (s.pc || s.pf) te("mpc");
328+
if (s.pc || s.pf) te("mpc", l[1]);
329329
// collapse first-child and last-child into nth-child expressions
330330
if (l[2] === ":first-child") {
331331
s.pf = ":nth-child";
@@ -344,33 +344,33 @@
344344
// any amount of whitespace, followed by paren, string, paren
345345
l = lex(str, (off = l[0]));
346346
if (l && l[1] === " ") l = lex(str, off = l[0]);
347-
if (!l || l[1] !== "(") te("pex");
347+
if (!l || l[1] !== "(") te("pex", str);
348348
l = lex(str, (off = l[0]));
349349
if (l && l[1] === " ") l = lex(str, off = l[0]);
350-
if (!l || l[1] !== toks.str) te("sex");
350+
if (!l || l[1] !== toks.str) te("sex", str);
351351
s.expr[2] = l[2];
352352
l = lex(str, (off = l[0]));
353353
if (l && l[1] === " ") l = lex(str, off = l[0]);
354-
if (!l || l[1] !== ")") te("epex");
354+
if (!l || l[1] !== ")") te("epex", str);
355355
} else if (l[2] === ":has") {
356356
// any amount of whitespace, followed by paren
357357
l = lex(str, (off = l[0]));
358358
if (l && l[1] === " ") l = lex(str, off = l[0]);
359-
if (!l || l[1] !== "(") te("pex");
359+
if (!l || l[1] !== "(") te("pex", str);
360360
var h = parse(str, l[0], true);
361361
l[0] = h[0];
362362
if (!s.has) s.has = [];
363363
s.has.push(h[1]);
364364
} else if (l[2] === ":expr") {
365-
if (s.expr) te("mexp");
365+
if (s.expr) te("mexp", str);
366366
var e = exprParse(str, l[0]);
367367
l[0] = e[0];
368368
s.expr = e[1];
369369
} else {
370-
if (s.pc || s.pf ) te("mpc");
370+
if (s.pc || s.pf ) te("mpc", str);
371371
s.pf = l[2];
372372
var m = nthPat.exec(str.substr(l[0]));
373-
if (!m) te("mepf");
373+
if (!m) te("mepf", str);
374374
if (m[5]) {
375375
s.a = 2;
376376
s.b = (m[5] === "odd") ? 1 : 0;
@@ -390,7 +390,7 @@
390390
}
391391

392392
// now if we didn't actually parse anything it's an error
393-
if (soff === off) te("se");
393+
if (soff === off) te("se", str);
394394

395395
return [off, s];
396396
}

0 commit comments

Comments
 (0)