Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 46e6efe

Browse files
committed
add error function
1 parent f719530 commit 46e6efe

5 files changed

Lines changed: 42 additions & 0 deletions

File tree

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Welcome to the Rudi documentation :smile:
1414
* [`delete`](functions/core-delete.md) – removes a key from an object or an item from a vector
1515
* [`do`](functions/core-do.md) – eval a sequence of statements where only one expression is valid
1616
* [`empty?`](functions/core-empty.md) – returns true when the given value is empty-ish (0, false, null, "", ...)
17+
* [`error`](functions/core-error.md) – returns an error
1718
* [`has?`](functions/core-has.md) – returns true if the given symbol's path expression points to an existing value
1819
* [`if`](functions/core-if.md) – evaluate one of two expressions based on a condition
1920
* [`set`](functions/core-set.md) – set a value in a variable/document, only really useful with ! modifier (set!)

docs/functions/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ applied to all functions (so technically `eq?!` is valid, though weird looking).
1818
* [`delete`](../functions/core-delete.md) – removes a key from an object or an item from a vector
1919
* [`do`](../functions/core-do.md) – eval a sequence of statements where only one expression is valid
2020
* [`empty?`](../functions/core-empty.md) – returns true when the given value is empty-ish (0, false, null, "", ...)
21+
* [`error`](../functions/core-error.md) – returns an error
2122
* [`has?`](../functions/core-has.md) – returns true if the given symbol's path expression points to an existing value
2223
* [`if`](../functions/core-if.md) – evaluate one of two expressions based on a condition
2324
* [`set`](../functions/core-set.md) – set a value in a variable/document, only really useful with ! modifier (set!)

docs/functions/core-error.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# error
2+
3+
`error` creates a new error and returns it, i.e. this function always "fails".
4+
Errors are constructed using Go's `fmt.Errorf`, so sprintf-style formatting is
5+
available.
6+
7+
## Examples
8+
9+
* `(error "invalid choice")` -> error `"invalid choice"`
10+
* `(error "too many replicas: %d" .replicas)` -> error `"too man replicas: 3"`
11+
12+
## Forms
13+
14+
### `(error message)`
15+
16+
* `message` is an arbitrary expression.
17+
18+
`error` evaluates the the message and coalesces it to a string. When successful,
19+
a new error with the message is created and returned.
20+
21+
### `(error fmt args+)`
22+
23+
* `fmt` is an arbitrary expression.
24+
* `args` are one ore more expressions.
25+
26+
`error` evaluates the the format and coalesces it to a string. When successful,
27+
it evaluates all further arguments and passes their results straight into
28+
`fmt.Errorf` and returns the newly created error.

pkg/builtin/core.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,17 @@ func isEmptyFunction(ctx types.Context, args []any) (any, error) {
307307
return !boolified, nil
308308
}
309309

310+
// (error MSG:string)
311+
// (error FMT:string ARGS+)
312+
func errorFunction(ctx types.Context, args []any) (any, error) {
313+
format, err := ctx.Coalesce().ToString(args[0])
314+
if err != nil {
315+
return nil, err
316+
}
317+
318+
return nil, fmt.Errorf(format, args[1:]...)
319+
}
320+
310321
// (strictly EXPR+)
311322
func strictlyFunction(ctx types.Context, args []ast.Expression) (any, error) {
312323
return coalescingChangerFunction(ctx, args, coalescing.NewStrict())

pkg/builtin/functions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var (
1414
"delete": deleteFunction{},
1515
"do": util.NewRawFunction(doFunction, "eval a sequence of statements where only one expression is valid").MinArgs(1),
1616
"empty?": util.NewLiteralFunction(isEmptyFunction, "returns true when the given value is empty-ish (0, false, null, \"\", ...)").MinArgs(1).MaxArgs(1),
17+
"error": util.NewLiteralFunction(errorFunction, "returns an error").MinArgs(1),
1718
"has?": util.NewRawFunction(hasFunction, "returns true if the given symbol's path expression points to an existing value").MinArgs(1).MaxArgs(1),
1819
"if": util.NewRawFunction(ifFunction, "evaluate one of two expressions based on a condition").MinArgs(2).MaxArgs(3),
1920
"set": util.NewRawFunction(setFunction, "set a value in a variable/document, only really useful with ! modifier (set!)").MinArgs(2).MaxArgs(2),

0 commit comments

Comments
 (0)