github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/compiler/eval/errors.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the Apache License Version 2.0.
     3  // This product includes software developed at Datadog (https://www.datadoghq.com/).
     4  // Copyright 2016-present Datadog, Inc.
     5  
     6  // Package eval holds eval related files
     7  package eval
     8  
     9  import (
    10  	"fmt"
    11  	"reflect"
    12  	"strings"
    13  
    14  	"github.com/alecthomas/participle/lexer"
    15  )
    16  
    17  // ErrNonStaticPattern when pattern operator is used on a non static value
    18  type ErrNonStaticPattern struct {
    19  	Field Field
    20  }
    21  
    22  func (e ErrNonStaticPattern) Error() string {
    23  	return fmt.Sprintf("unable to apply pattern on non static value `%s`", e.Field)
    24  }
    25  
    26  // ErrInvalidPattern is returned for an invalid regular expression
    27  type ErrInvalidPattern struct {
    28  	Pattern string
    29  }
    30  
    31  func (e ErrInvalidPattern) Error() string {
    32  	return fmt.Sprintf("invalid pattern `%s`", e.Pattern)
    33  }
    34  
    35  // ErrInvalidRegexp is returned for an invalid regular expression
    36  type ErrInvalidRegexp struct {
    37  	Regexp string
    38  }
    39  
    40  func (e ErrInvalidRegexp) Error() string {
    41  	return fmt.Sprintf("invalid regexp `%s`", e.Regexp)
    42  }
    43  
    44  // ErrAstToEval describes an error that occurred during the conversion from the AST to an evaluator
    45  type ErrAstToEval struct {
    46  	Pos  lexer.Position
    47  	Text string
    48  }
    49  
    50  func (r *ErrAstToEval) Error() string {
    51  	return fmt.Sprintf("%s: %s", r.Text, r.Pos)
    52  }
    53  
    54  // NewError returns a new ErrAstToEval error
    55  func NewError(pos lexer.Position, format string, vars ...interface{}) *ErrAstToEval {
    56  	return &ErrAstToEval{Pos: pos, Text: fmt.Sprintf(format, vars...)}
    57  }
    58  
    59  // NewTypeError returns a new ErrAstToEval error when an invalid type was used
    60  func NewTypeError(pos lexer.Position, kind reflect.Kind) *ErrAstToEval {
    61  	return NewError(pos, "%s expected", kind)
    62  }
    63  
    64  // NewArrayTypeError returns a new ErrAstToEval error when an invalid type was used
    65  func NewArrayTypeError(pos lexer.Position, arrayKind reflect.Kind, kind reflect.Kind) *ErrAstToEval {
    66  	return NewError(pos, "%s of %s expected", arrayKind, kind)
    67  }
    68  
    69  // NewCIDRTypeError returns a new ErrAstToEval error when an invalid type was used
    70  func NewCIDRTypeError(pos lexer.Position, arrayKind reflect.Kind, kind interface{}) *ErrAstToEval {
    71  	return NewError(pos, "%s of %s expected", arrayKind, reflect.TypeOf(kind))
    72  }
    73  
    74  // NewOpUnknownError returns a new ErrAstToEval error when an unknown operator was used
    75  func NewOpUnknownError(pos lexer.Position, op string) *ErrAstToEval {
    76  	return NewError(pos, "operator `%s` unknown", op)
    77  }
    78  
    79  // NewOpError returns a new ErrAstToEval error when an operator was used in an invalid manner
    80  func NewOpError(pos lexer.Position, op string, err error) *ErrAstToEval {
    81  	return NewError(pos, "operator `%s` error: %s", op, err)
    82  }
    83  
    84  // NewRegisterMultipleFields returns a new ErrAstToEval error when a register is used across multiple fields
    85  func NewRegisterMultipleFields(pos lexer.Position, regID RegisterID, err error) *ErrAstToEval {
    86  	return NewError(pos, "register `%s` error: %s", regID, err)
    87  }
    88  
    89  // NewRegisterNameNotAllowed returns a new ErrAstToEval error when a register name is not allowed
    90  func NewRegisterNameNotAllowed(pos lexer.Position, regID RegisterID, err error) *ErrAstToEval {
    91  	return NewError(pos, "register name `%s` error: %s", regID, err)
    92  }
    93  
    94  // ErrRuleParse describes a parsing error and its position in the expression
    95  type ErrRuleParse struct {
    96  	pos  lexer.Position
    97  	expr string
    98  }
    99  
   100  func (e *ErrRuleParse) Error() string {
   101  	column := e.pos.Column
   102  	if column > 0 {
   103  		column--
   104  	}
   105  
   106  	str := fmt.Sprintf("%s\n", e.expr)
   107  	str += strings.Repeat(" ", column)
   108  	str += "^"
   109  	return str
   110  }
   111  
   112  // ErrFieldNotFound error when a field is not present in the model
   113  type ErrFieldNotFound struct {
   114  	Field string
   115  }
   116  
   117  func (e ErrFieldNotFound) Error() string {
   118  	return fmt.Sprintf("field `%s` not found", e.Field)
   119  }
   120  
   121  // ErrIteratorNotSupported error when a field doesn't support iteration
   122  type ErrIteratorNotSupported struct {
   123  	Field string
   124  }
   125  
   126  func (e ErrIteratorNotSupported) Error() string {
   127  	return fmt.Sprintf("field `%s` doesn't support iteration", e.Field)
   128  }
   129  
   130  // ErrNotSupported returned when something is not supported on a field
   131  type ErrNotSupported struct {
   132  	Field string
   133  }
   134  
   135  func (e ErrNotSupported) Error() string {
   136  	return fmt.Sprintf("not supported by field `%s`", e.Field)
   137  }
   138  
   139  // ErrValueTypeMismatch error when the given value is not having the correct type
   140  type ErrValueTypeMismatch struct {
   141  	Field string
   142  }
   143  
   144  func (e ErrValueTypeMismatch) Error() string {
   145  	return fmt.Sprintf("incorrect value type for `%s`", e.Field)
   146  }
   147  
   148  // ErrRuleNotCompiled error returned by functions that require to have the rule compiled
   149  type ErrRuleNotCompiled struct {
   150  	RuleID string
   151  }
   152  
   153  func (e ErrRuleNotCompiled) Error() string {
   154  	return fmt.Sprintf("rule not compiled `%s`", e.RuleID)
   155  }
   156  
   157  // ErrFieldReadOnly is returned when a filter does not support being set with SetFieldValue
   158  type ErrFieldReadOnly struct {
   159  	Field Field
   160  }
   161  
   162  func (e ErrFieldReadOnly) Error() string {
   163  	return fmt.Sprintf("read-only field `%s`", e.Field)
   164  }