summaryrefslogtreecommitdiff
path: root/data/basics/serr.go
blob: 229d800d7c5bf656b06d146f6760e66d79e53268 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package basics

import (
	"errors"
	"strings"

	"golang.org/x/exp/slog"
)

// SError is a structured error object. It contains a message and an arbitrary
// set of attributes. If the message contains "%A", it will be replaced by the
// attributes (in no guaranteed order), when SError() is called.
//
//msgp:ignore SError
type SError struct {
	Msg     string
	Attrs   map[string]any
	Wrapped error
}

// New creates a new structured error object using the supplied message and
// attributes. If the message contains "%A", it will be replaced by the
// attributes when Error() is called.
func New(msg string, pairs ...any) *SError {
	attrs := make(map[string]any, len(pairs)/2)
	for i := 0; i < len(pairs); i += 2 {
		attrs[pairs[i].(string)] = pairs[i+1]
	}
	return &SError{Msg: msg, Attrs: attrs}
}

// Error returns either the exact supplied message, or the serialized attributes if
// the supplied message was blank, or substituted for %A.
func (e *SError) Error() string {
	if e.Msg == "" {
		return e.AttributesAsString()
	}
	// imperfect because we replace \%A as well
	if strings.Contains(e.Msg, "%A") {
		return strings.Replace(e.Msg, "%A", e.AttributesAsString(), -1)
	}
	return e.Msg
}

// AttributesAsString returns the attributes the same way that slog serializes
// attributes to text in a log message, in no guaranteed order.
func (e *SError) AttributesAsString() string {
	var buf strings.Builder
	args := make([]any, 0, 2*len(e.Attrs))
	for key, val := range e.Attrs {
		args = append(args, key)
		args = append(args, val)
	}
	l := slog.New(slog.NewTextHandler(&buf, nil))
	l.Info("", args...)
	return strings.TrimSuffix(strings.SplitN(buf.String(), " ", 4)[3], "\n")
}

// Annotate adds additional attributes to an existing error, even if the error
// is deep in the error chain. If the supplied error is nil, nil is returned so
// that callers can annotate errors without checking if they are non-nil.  If
// the error is not a structured error, it is wrapped in one using its existing
// message and the new attributes. Just like append() for slices, callers should
// re-assign, like this `err = serr.Annotate(err, "x", 100)`
func Annotate(err error, pairs ...any) error {
	if err == nil {
		return nil
	}
	var serr *SError
	if ok := errors.As(err, &serr); ok {
		for i := 0; i < len(pairs); i += 2 {
			serr.Attrs[pairs[i].(string)] = pairs[i+1]
		}
		return err
	}
	// Since we don't have a structured error, we wrap the existing error in one.
	serr = New(err.Error(), pairs...)
	serr.Wrapped = err
	return serr
}

// Wrap is used to "demote" an existing error to a field in a new structured
// error. The wrapped error message is added as $field-msg, and if the error is
// structured, the attributes are added under $field-attrs.
func Wrap(err error, msg string, field string, pairs ...any) error {
	serr := New(msg, field+"-msg", err.Error())
	for i := 0; i < len(pairs); i += 2 {
		serr.Attrs[pairs[i].(string)] = pairs[i+1]
	}
	serr.Wrapped = err

	var inner *SError
	if ok := errors.As(err, &inner); ok {
		attributes := make(map[string]any, len(inner.Attrs))
		for key, val := range inner.Attrs {
			attributes[key] = val
		}
		serr.Attrs[field+"-attrs"] = attributes
	}

	return serr
}

// Unwrap returns the inner error, if it exists.
func (e *SError) Unwrap() error {
	return e.Wrapped
}

// Attributes returns the attributes of a structured error, or nil/empty.
func Attributes(err error) map[string]any {
	var se *SError
	if errors.As(err, &se) {
		return se.Attrs
	}
	return nil
}