summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/spicedb/pkg/spiceerrors/common.go
blob: 625faef27016e36a1268d05e6a2d69bee79f7968 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package spiceerrors

import (
	"errors"
	"maps"
)

// SourcePosition is a position in the input source.
type SourcePosition struct {
	// LineNumber is the 1-indexed line number in the input source.
	LineNumber int

	// ColumnPosition is the 1-indexed column position in the input source.
	ColumnPosition int
}

// WithSourceError is an error that includes the source text and position
// information.
type WithSourceError struct {
	error

	// SourceCodeString is the input source code string for the error.
	SourceCodeString string

	// LineNumber is the (1-indexed) line number of the error, or 0 if unknown.
	LineNumber uint64

	// ColumnPosition is the (1-indexed) column position of the error, or 0 if
	// unknown.
	ColumnPosition uint64
}

// HasMetadata indicates that the error has metadata defined.
type HasMetadata interface {
	// DetailsMetadata returns the metadata for details for this error.
	DetailsMetadata() map[string]string
}

// Unwrap returns the inner, wrapped error.
func (err *WithSourceError) Unwrap() error {
	return err.error
}

// NewWithSourceError creates and returns a new WithSourceError.
func NewWithSourceError(err error, sourceCodeString string, oneIndexedLineNumber uint64, oneIndexedColumnPosition uint64) *WithSourceError {
	return &WithSourceError{err, sourceCodeString, oneIndexedLineNumber, oneIndexedColumnPosition}
}

// AsWithSourceError returns the error as an WithSourceError, if applicable.
func AsWithSourceError(err error) (*WithSourceError, bool) {
	var serr *WithSourceError
	if errors.As(err, &serr) {
		return serr, true
	}
	return nil, false
}

// WithAdditionalDetailsError is an error that includes additional details.
type WithAdditionalDetailsError struct {
	error

	// AdditionalDetails is a map of additional details for the error.
	AdditionalDetails map[string]string
}

func NewWithAdditionalDetailsError(err error) *WithAdditionalDetailsError {
	return &WithAdditionalDetailsError{err, nil}
}

// Unwrap returns the inner, wrapped error.
func (err *WithAdditionalDetailsError) Unwrap() error {
	return err.error
}

func (err *WithAdditionalDetailsError) WithAdditionalDetails(key string, value string) {
	if err.AdditionalDetails == nil {
		err.AdditionalDetails = make(map[string]string)
	}
	err.AdditionalDetails[key] = value
}

func (err *WithAdditionalDetailsError) AddToDetails(details map[string]string) map[string]string {
	if err.AdditionalDetails != nil {
		maps.Copy(details, err.AdditionalDetails)
	}

	return details
}