Skip to content

Commit 5fd338b

Browse files
committed
Add context to try fallbacks
1 parent 52e84e3 commit 5fd338b

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

defaultMethods.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ const defaultMethods = {
293293
let lastError
294294
for (let i = 0; i < arr.length; i++) {
295295
try {
296-
item = executeInLoop ? engine.run(arr[i], _1, { above: _2 }) : arr[i]
296+
// Todo: make this message thing more robust.
297+
if (lastError) item = engine.run(arr[i], { error: lastError.message || lastError.constructor.name }, { above: [null, _1, _2] })
298+
else item = executeInLoop ? engine.run(arr[i], _1, { above: _2 }) : arr[i]
297299
return item
298300
} catch (e) {
299301
// Do nothing
@@ -312,7 +314,9 @@ const defaultMethods = {
312314
let lastError
313315
for (let i = 0; i < arr.length; i++) {
314316
try {
315-
item = executeInLoop ? await engine.run(arr[i], _1, { above: _2 }) : arr[i]
317+
// Todo: make this message thing more robust.
318+
if (lastError) item = await engine.run(arr[i], { error: lastError.message || lastError.constructor.name }, { above: [null, _1, _2] })
319+
else item = executeInLoop ? await engine.run(arr[i], _1, { above: _2 }) : arr[i]
316320
return item
317321
} catch (e) {
318322
// Do nothing
@@ -970,11 +974,11 @@ defaultMethods['/'].compile = function (data, buildState) {
970974
if (Array.isArray(data)) {
971975
return `(${data.map((i, x) => {
972976
let res = numberCoercion(i, buildState)
973-
if (x) res = `(${res}|| (() => { throw new Error() })() )`
977+
if (x) res = `(${res}|| (() => { throw new Error('NaN') })() )`
974978
return res
975979
}).join(' / ')})`
976980
}
977-
return `(${buildString(data, buildState)}).reduce((a,b) => (+precoerceNumber(a))/(+precoerceNumber(b) || (() => { throw new Error() })() ))`
981+
return `(${buildString(data, buildState)}).reduce((a,b) => (+precoerceNumber(a))/(+precoerceNumber(b) || (() => { throw new Error('NaN') })() ))`
978982
}
979983
// @ts-ignore Allow custom attribute
980984
defaultMethods['*'].compile = function (data, buildState) {

suites/try.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,45 @@
6161
},
6262
"result": 10,
6363
"data": null
64+
},
65+
{
66+
"description": "The context switches for the try coalescing to the previous error",
67+
"rule": {
68+
"try": [
69+
{ "if": [true, { "error": "Some error" }, null] },
70+
{ "val": [] }
71+
]
72+
},
73+
"result": { "error": "Some error"},
74+
"data": null
75+
},
76+
{
77+
"description": "Try can work further up the AST with Exceptions, and return the error",
78+
"rule": {
79+
"try": [{
80+
"if": [
81+
true,
82+
{ "map": [[1,2,3], {"/": [0, 0] }]},
83+
null
84+
]
85+
}, { "val": [] }]
86+
},
87+
"result": { "error": "NaN" },
88+
"data": null
89+
},
90+
"# Not Proposed",
91+
{
92+
"description": "Try can work further up the AST with Exceptions; Grabbing other Context value",
93+
"rule": {
94+
"try": [{
95+
"if": [
96+
true,
97+
{ "map": [[1,2,3], {"/": [0, 0] }]},
98+
null
99+
]
100+
}, { "val": [[2], "fallback"] }]
101+
},
102+
"result": "Hello",
103+
"data": { "fallback": "Hello" }
64104
}
65105
]

0 commit comments

Comments
 (0)