Skip to content

Use a Struct for parser errors to expose the position information programatically #27

Description

@JoshuaWilkes

I have started experimenting with a feature that requires returning an error the to user if a when cedar expression is invalid. At the moment I use something like this to test the expression.

selectorPolicy, err := cedar.NewPolicySet("", []byte(fmt.Sprintf(`permit(principal,action,resource) when { %s };`, whenExpression)))

I would like to be able to use the error result programatically to provide feedback about the exact error position and details. Likely adding some syntax highlighting in my app.

The errors returned by the current version of the parser use fmt.Errorf(), if this was swapped for a struct alongs the lines of this, it could be used to extract the exact details of the error.

type ScanError struct {
	Message  string
	Position Position
}

func (e ScanError) Error() string {
	return fmt.Sprintf("%v: %s", e.Position, e.Message)
}

func (s *scanner) error(msg string) {
	s.tokEnd = s.srcPos - s.lastCharLen // make sure token text is terminated
	s.err = ScanError{
		Message:  msg,
		Position: s.position,
	}
}

I'm happy to contribute some code here, though I note you have some recent work pending to stabilise the AST and parsing code so will hold off for now.

Edit: I see that the new version v0.2.0 has been released to I will check that out and see if there is a way forward here :)

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