github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/caveats/errors.go (about)

     1  package caveats
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/authzed/cel-go/cel"
     7  	"github.com/rs/zerolog"
     8  )
     9  
    10  // EvaluationErr is an error in evaluation of a caveat expression.
    11  type EvaluationErr struct {
    12  	error
    13  }
    14  
    15  // MarshalZerologObject implements zerolog.LogObjectMarshaler
    16  func (err EvaluationErr) MarshalZerologObject(e *zerolog.Event) {
    17  	e.Err(err.error)
    18  }
    19  
    20  // DetailsMetadata returns the metadata for details for this error.
    21  func (err EvaluationErr) DetailsMetadata() map[string]string {
    22  	return map[string]string{}
    23  }
    24  
    25  // ParameterConversionErr is an error in type conversion of a supplied parameter.
    26  type ParameterConversionErr struct {
    27  	error
    28  	parameterName string
    29  }
    30  
    31  // MarshalZerologObject implements zerolog.LogObjectMarshaler
    32  func (err ParameterConversionErr) MarshalZerologObject(e *zerolog.Event) {
    33  	e.Err(err.error).Str("parameterName", err.parameterName)
    34  }
    35  
    36  // DetailsMetadata returns the metadata for details for this error.
    37  func (err ParameterConversionErr) DetailsMetadata() map[string]string {
    38  	return map[string]string{
    39  		"parameter_name": err.parameterName,
    40  	}
    41  }
    42  
    43  // ParameterName is the name of the parameter.
    44  func (err ParameterConversionErr) ParameterName() string {
    45  	return err.parameterName
    46  }
    47  
    48  // CompilationErrors is a wrapping error for containing compilation errors for a Caveat.
    49  type CompilationErrors struct {
    50  	error
    51  
    52  	issues *cel.Issues
    53  }
    54  
    55  // LineNumber is the 0-indexed line number for compilation error.
    56  func (err CompilationErrors) LineNumber() int {
    57  	return err.issues.Errors()[0].Location.Line() - 1
    58  }
    59  
    60  // ColumnPositionis the 0-indexed column position for compilation error.
    61  func (err CompilationErrors) ColumnPosition() int {
    62  	return err.issues.Errors()[0].Location.Column() - 1
    63  }
    64  
    65  // MarshalZerologObject implements zerolog.LogObjectMarshaler
    66  func (err CompilationErrors) MarshalZerologObject(e *zerolog.Event) {
    67  	e.Err(err.error).Int("lineNumber", err.LineNumber()).Int("columnPosition", err.ColumnPosition())
    68  }
    69  
    70  // DetailsMetadata returns the metadata for details for this error.
    71  func (err CompilationErrors) DetailsMetadata() map[string]string {
    72  	return map[string]string{
    73  		"line_number":     strconv.Itoa(err.LineNumber()),
    74  		"column_position": strconv.Itoa(err.ColumnPosition()),
    75  	}
    76  }