Skip to content

Commit acfef8d

Browse files
committed
Add the ability for error to throw an error object
1 parent 08137ef commit acfef8d

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

defaultMethods.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-throw-literal */
12
// @ts-check
23
'use strict'
34

@@ -114,7 +115,9 @@ const defaultMethods = {
114115
return res
115116
},
116117
error: (type) => {
117-
throw new Error(type)
118+
if (Array.isArray(type)) type = type[0]
119+
if (typeof type === 'object') throw type
120+
throw { error: type }
118121
},
119122
max: (data) => Math.max(...data),
120123
min: (data) => Math.min(...data),
@@ -342,7 +345,7 @@ const defaultMethods = {
342345
for (let i = 0; i < arr.length; i++) {
343346
try {
344347
// Todo: make this message thing more robust.
345-
if (lastError) item = engine.run(arr[i], { error: lastError.message || lastError.constructor.name }, { above: [null, _1, _2] })
348+
if (lastError) item = engine.run(arr[i], { error: lastError.error || lastError.message || lastError.constructor.name }, { above: [null, _1, _2] })
346349
else item = executeInLoop ? engine.run(arr[i], _1, { above: _2 }) : arr[i]
347350
return item
348351
} catch (e) {
@@ -363,7 +366,7 @@ const defaultMethods = {
363366
for (let i = 0; i < arr.length; i++) {
364367
try {
365368
// Todo: make this message thing more robust.
366-
if (lastError) item = await engine.run(arr[i], { error: lastError.message || lastError.constructor.name }, { above: [null, _1, _2] })
369+
if (lastError) item = await engine.run(arr[i], { error: lastError.error || lastError.message || lastError.constructor.name }, { above: [null, _1, _2] })
367370
else item = executeInLoop ? await engine.run(arr[i], _1, { above: _2 }) : arr[i]
368371
return item
369372
} catch (e) {

suites/try.json

+24
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,30 @@
122122
"result": "NaN",
123123
"data": null
124124
},
125+
{
126+
"description": "Handles NaN Explicitly",
127+
"rule": {
128+
"try": [
129+
{ "if": [{ "/": [1, { "val": "x" }] }, { "error": "Some error" }, null] },
130+
{
131+
"if": [{ "===": [{ "val": "error" }, "NaN"]}, "Handled", { "error": { "val": [] } }]
132+
}
133+
]
134+
},
135+
"result": "Handled",
136+
"data": { "x": 0 }
137+
},
138+
{
139+
"description": "Did not NaN, so it errored",
140+
"rule": {
141+
"try": [
142+
{ "if": [{ "/": [1, { "val": "x" }] }, { "error": "Some error" }, null] },
143+
{ "if": [{ "===": [{ "val": "error" }, "NaN"]}, "Handled", { "error": { "val": [] } }] }
144+
]
145+
},
146+
"error": true,
147+
"data": { "x": 1 }
148+
},
125149
"# Not Proposed",
126150
{
127151
"description": "Try can work further up the AST with Exceptions; Grabbing other Context value",

0 commit comments

Comments
 (0)