Skip to content

Error swallowed when using ConstExpr with a custom func returning an error #238

@ghost

Description

Error is swallowed when using expr.ConstExpr(...) optimization with a custom function that has a signature which returns an error: func(i int) (int, error).

This example works correctly if expr.ConstExpr(...) setting is omitted, or optimizations are turned off. The error would be returned by expr.Run(...).

package main

import (
	"errors"
	"fmt"

	"github.com/antonmedv/expr"
)

func main() {
	env := map[string]interface{}{
		"double": func(i int) (int, error) {
			if i < 0 {
				return 0, errors.New("value cannot be less than zero")
			}
			return i * 2, nil
		},
	}

	options := []expr.Option{
		expr.Env(env),
		expr.ConstExpr("double"),
	}

	// err == nil here. Error is swallowed.
	compiledRule, err := expr.Compile("double(-1)", options...)
	if err == nil {
		fmt.Printf("expected expr.Compile to return error")
	} else {
		fmt.Println("success")
	}

	// err == nil here as well.
	_, err = expr.Run(compiledRule, env)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions