File: errs.go

package info (click to toggle)
zabbix 1%3A7.0.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 272,688 kB
  • sloc: sql: 946,050; ansic: 389,440; php: 292,698; javascript: 83,388; sh: 5,680; makefile: 3,285; java: 1,420; cpp: 694; perl: 64; xml: 56
file content (201 lines) | stat: -rw-r--r-- 5,845 bytes parent folder | download | duplicates (2)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
** documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
** rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in all copies or substantial portions
** of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
** WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
** COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
**/

package errs

import (
	"fmt"
	"strings"
	"unicode"
)

var (
	_ error    = (*stringError)(nil) //nolint:errcheck
	_ rawError = (*stringError)(nil) //nolint:errcheck

	_ error    = (*messageWrappedError)(nil) //nolint:errcheck
	_ rawError = (*messageWrappedError)(nil) //nolint:errcheck

	_ error    = (*constantWrappedError)(nil) //nolint:errcheck
	_ rawError = (*constantWrappedError)(nil) //nolint:errcheck
)

// rawError defines an internal interface for retrieving raw error message
// strings.
type rawError interface {
	rawError() string
}

// messageWrappedError defines a basic error with a message.
type stringError struct {
	msg string
}

// messageWrappedError defines a error wrapped with a message.
type messageWrappedError struct {
	err error
	msg string
}

// constantWrappedError defines a error wrapped with an error constant.
type constantWrappedError struct {
	err      error
	constant error
}

// New creates a new ZabbixError.
func New(msg string) error {
	return &stringError{
		msg: msg,
	}
}

// Errorf creates a new Zabbix compliant error string (see zabbixErrorString
// for more info) with a given format.
func Errorf(format string, args ...any) error {
	return New(fmt.Sprintf(format, args...))
}

// Wrap creates a new Zabbix error (see zabbixErrorString funcion) that wraps
// the given error with a given message.
func Wrap(err error, msg string) error {
	return &messageWrappedError{
		err: err,
		msg: msg,
	}
}

// Wrapf creates a new Zabbix error (see zabbixErrorString funcion) that wraps
// the given error with a given format message.
func Wrapf(err error, format string, args ...any) error {
	return Wrap(err, fmt.Sprintf(format, args...))
}

// WrapConst wraps an error with an error constant. errors.Is and errors.As
// matches both the error and constant. Requires go version to be 1.20 or above
// as it's basically an errors.Join that was introduced in 1.20. For more info
// view constantWrappedError.Unwrap().
func WrapConst(err, constant error) error {
	return &constantWrappedError{
		err:      err,
		constant: constant,
	}
}

// Error implements the error interface for stringError, returning
// Zabbix compliant error string (see zabbixErrorString for more info).
func (e *stringError) Error() string {
	return zabbixErrorString(e.rawError())
}

// Error implements the error interface for messageWrappedError, returning
// Zabbix compliant error string (see zabbixErrorString for more info).
func (e *messageWrappedError) Error() string {
	return zabbixErrorString(e.rawError())
}

// Error implements the error interface for constantWrappedError, returning
// Zabbix compliant error string (see zabbixErrorString for more info).
func (e *constantWrappedError) Error() string {
	return zabbixErrorString(e.rawError())
}

// Unwrap returns the wrapped error allowing errors.Is and errors.As to work.
func (e *messageWrappedError) Unwrap() error {
	return e.err
}

// Unwrap returns an slice of errors that contains both the wrapped error and
// the error constant. Unwrap methods on error implementations allow errors.As
// and errors.Is to work for complex error types. The ability for Unwrap method
// to return slice was introduced in go 1.20 with errors.Join function.
func (e *constantWrappedError) Unwrap() []error {
	return []error{e.err, e.constant}
}

// rawError implements the internal rawError interface for unprocessed error
// string retrieval.
func (e *stringError) rawError() string {
	return e.msg
}

// rawError implements the internal rawError interface for unprocessed error
// string retrieval.
func (e *messageWrappedError) rawError() string {
	errStr := "nil error"

	if e.err != nil {
		errStr = e.err.Error()

		rErr, ok := e.err.(rawError)
		if ok {
			errStr = rErr.rawError()
		}
	}

	return fmt.Sprintf("%s: %s", e.msg, errStr)
}

// rawError implements the internal rawError interface for unprocessed error
// string retrieval.
func (e *constantWrappedError) rawError() string {
	errStr := "nil error"
	constantStr := "nil error"

	if e.err != nil {
		errStr = e.err.Error()

		rErr, ok := e.err.(rawError)
		if ok {
			errStr = rErr.rawError()
		}
	}

	if e.constant != nil {
		constantStr = e.constant.Error()

		rErr, ok := e.constant.(rawError)
		if ok {
			constantStr = rErr.rawError()
		}
	}

	return fmt.Sprintf("%s: %s", constantStr, errStr)
}

// zabbixErrorString creates a new string in accordance with Zabbix
// requirements:
// - the first letter must be capitalized;
// - an error text should be trailed by a dot.
func zabbixErrorString(s string) string {
	s = strings.TrimSpace(s)

	if s == "" {
		return ""
	}

	r := []rune(s)
	r[0] = unicode.ToUpper(r[0])
	s = string(r)

	if strings.HasSuffix(s, ".") {
		return s
	}

	return s + "."
}