Skip to content

Commit 65fd351

Browse files
jhorbulykandrew-coleman
authored andcommitted
More $error() functionality (#353)
* Add more test cases for $error() * Add documentation for $error() function. * Respond to PR comments.
1 parent 26584f5 commit 65fd351

File tree

8 files changed

+35
-11
lines changed

8 files changed

+35
-11
lines changed

docs/object-functions.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Returns an array containing the keys in the object. If the argument is an array
1313
__Signature:__ `$lookup(object, key)`
1414

1515
Returns the value associated with `key` in `object`. If the first argument is an array of objects, then all of the objects in the array are searched, and the values associated with all occurrences of `key` are returned.
16-
16+
1717

1818
## `$spread()`
1919
__Signature:__ `$spread(object)`
@@ -39,7 +39,7 @@ The `function` parameter will get invoked with two arguments:
3939

4040
`function(value, name)`
4141

42-
where the `value` parameter is the value of each name/value pair in the object and `name` is its name. The `name` parameter is optional.
42+
where the `value` parameter is the value of each name/value pair in the object and `name` is its name. The `name` parameter is optional.
4343

4444
__Examples__
4545

@@ -51,4 +51,9 @@ __Examples__
5151
"Street: Hursley Park",
5252
"City: Winchester",
5353
"Postcode: SO21 2JN"
54-
]
54+
]
55+
56+
## `$error()`
57+
__Signature:__`$error(message)`
58+
59+
Deliberately throws an error with an optional `message`

src/jsonata.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,8 @@ var jsonata = (function() {
18101810
"D3133": "The 'name' modifier can only be applied to months and days in the date/time picture string, not {{value}}",
18111811
"D3134": "The timezone integer format specifier cannot have more than four digits",
18121812
"D3135": "No matching closing bracket ']' in date/time picture string",
1813-
"D3136": "The date/time picture string is missing specifiers required to parse the timestamp"
1813+
"D3136": "The date/time picture string is missing specifiers required to parse the timestamp",
1814+
"D3137": "{{{message}}}"
18141815
};
18151816

18161817
/**

test/test-suite/groups/function-error/case000.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"expr": "Account.Order[0].Product[0].Price > 35 ? Account.Order[0].Product[0].Price : $error('Too Expensive')",
33
"dataset": "dataset5",
44
"bindings": {},
5-
"code": "D3137"
5+
"error": {
6+
"code": "D3137",
7+
"message": "Too Expensive"
8+
}
69
}

test/test-suite/groups/function-error/case003.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"expr": "Account.Order[0].Product[0].Price > 34 ? $error('Too Expensive') : Account.Order[0].Product[0].Price",
33
"dataset": "dataset5",
44
"bindings": {},
5-
"code": "D3137"
5+
"error": {
6+
"code": "D3137",
7+
"message": "Too Expensive"
8+
}
69
}

test/test-suite/groups/function-error/case005.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"expr": "$count(Account.Order[0].Product) < 3 ? $error('Not enough products in orders')",
33
"dataset": "dataset5",
44
"bindings": {},
5-
"code": "D3137"
5+
"error": {
6+
"code": "D3137",
7+
"message": "Not enough products in orders"
8+
}
69
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
2-
"expr": "($msg='My Message'; $error($msg); true)",
2+
"expr": "($msg:='My Message'; $error($msg); true)",
33
"dataset": "dataset5",
44
"bindings": {},
5-
"code": "D3137"
5+
"error": {
6+
"code": "D3137",
7+
"message": "My Message"
8+
}
69
}

test/test-suite/groups/function-error/case009.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"expr": "$error()",
33
"dataset": "dataset5",
44
"bindings": {},
5-
"code": "D3137"
5+
"error": {
6+
"code": "D3137",
7+
"message": "$error() function evaluated"
8+
}
69
}

test/test-suite/groups/function-error/case010.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"expr": "$error(foo)",
33
"dataset": "dataset5",
44
"bindings": {},
5-
"code": "D3137"
5+
"error": {
6+
"code": "D3137",
7+
"message": "$error() function evaluated"
8+
}
69
}

0 commit comments

Comments
 (0)