File: err.go

package info (click to toggle)
golang-github-seancfoley-ipaddress-go 1.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 3,700 kB
  • sloc: makefile: 3
file content (279 lines) | stat: -rw-r--r-- 6,549 bytes parent folder | download
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
// Copyright 2020-2022 Sean C Foley
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package ipaddr

import (
	"errors"
	"fmt"
	"strconv"
	"strings"

	"github.com/seancfoley/ipaddress-go/ipaddr/addrerr"
)

type addressError struct {
	// key to look up the error message
	key string

	// an optional string with the address
	str string
}

func (a *addressError) Error() string {
	return getStr(a.str) + lookupStr("ipaddress.address.error") + " " + lookupStr(a.key)
}

func getStr(str string) (res string) {
	if len(str) > 0 {
		res = str + " "
	}
	return
}

// GetKey can be used to internationalize the error strings in the IPAddress library.
// The list of keys and their English translations are listed in IPAddressResources.properties.
// Use your own preferred method to map the key to your translations.
// One such option is golang.org/x/text which provides language tags (https://pkg.go.dev/golang.org/x/text/language?utm_source=godoc#Tag),
// which can then be mapped to catalogs, each catalog a list of translations for the set of keys provided here.
// In the code you supply a language key to use the right catalog.
// You can use the gotext tool to integrate those translations with your application.
func (a *addressError) GetKey() string {
	return a.key
}

type mergedError struct {
	addrerr.AddressError
	merged []addrerr.AddressError
}

func (a *mergedError) GetMerged() []addrerr.AddressError {
	return a.merged
}

type addressStringError struct {
	addressError
}

type addressStringNestedError struct {
	addressStringError
	nested addrerr.AddressStringError
}

func (a *addressStringNestedError) Error() string {
	return a.addressError.Error() + ": " + a.nested.Error()
}

type addressStringIndexError struct {
	addressStringError

	// byte index location in string of the error
	index int
}

func (a *addressStringIndexError) Error() string {
	return lookupStr("ipaddress.address.error") + " " + lookupStr(a.key) + " " + strconv.Itoa(a.index)
}

type hostNameError struct {
	addressError
}

// GetAddrError returns the nested address error which is nil for a host name error
func (a *hostNameError) GetAddrError() addrerr.AddressError {
	return nil
}

func (a *hostNameError) Error() string {
	return getStr(a.str) + lookupStr("ipaddress.host.error") + " " + lookupStr(a.key)
}

type hostNameNestedError struct {
	hostNameError
	nested error
}

type hostAddressNestedError struct {
	hostNameIndexError
	nested addrerr.AddressError
}

// GetAddrError returns the nested address error
func (a *hostAddressNestedError) GetAddrError() addrerr.AddressError {
	return a.nested
}

func (a *hostAddressNestedError) Error() string {
	if a.hostNameIndexError.key != "" {
		return getStr(a.str) + lookupStr("ipaddress.host.error") + " " + a.hostNameIndexError.Error() + " " + a.nested.Error()
	}
	return getStr(a.str) + lookupStr("ipaddress.host.error") + " " + a.nested.Error()
}

type hostNameIndexError struct {
	hostNameError

	// byte index location in string of the error
	index int
}

func (a *hostNameIndexError) Error() string {
	return getStr(a.str) + lookupStr("ipaddress.host.error") + " " + lookupStr(a.key) + " " + strconv.Itoa(a.index)
}

type incompatibleAddressError struct {
	addressError
}

type sizeMismatchError struct {
	incompatibleAddressError
}

type addressValueError struct {
	addressError

	// the value
	val int
}

///////////////////////////////////////////////

type wrappedErr struct {
	// root cause
	cause error

	// wrapper
	err error

	str string
}

func (wrappedErr *wrappedErr) Error() string {
	str := wrappedErr.str
	if len(str) > 0 {
		return str
	}
	str = wrappedErr.err.Error() + ": " + wrappedErr.cause.Error()
	wrappedErr.str = str
	return str
}

func newError(str string) error {
	return errors.New(str)
}

// errorF returns a formatted error
func errorF(format string, a ...interface{}) error {
	return errors.New(fmt.Sprintf(format, a...))
}

// wrapErrf wraps the given error, but only if it is not nil.
func wrapErrf(err error, format string, a ...interface{}) error {
	return wrapper(true, err, format, a...)
}

// wrapToErrf is like wrapErrf but always returns an error
func wrapToErrf(err error, format string, a ...interface{}) error {
	return wrapper(false, err, format, a...)
}

func wrapper(nilIfFirstNil bool, err error, format string, a ...interface{}) error {
	if err == nil {
		if nilIfFirstNil {
			return nil
		}
		return errorF(format, a...)
	}
	return &wrappedErr{
		cause: err,
		err:   errorF(format, a...),
	}
}

type mergedErr struct {
	mergedErrs []error
	str        string
}

func (merged *mergedErr) Error() (str string) {
	str = merged.str
	if len(str) > 0 {
		return
	}
	mergedErrs := merged.mergedErrs
	errLen := len(mergedErrs)
	strs := make([]string, errLen)
	totalLen := 0
	for i, err := range mergedErrs {
		str := err.Error()
		strs[i] = str
		totalLen += len(str)
	}
	format := strings.Builder{}
	format.Grow(totalLen + errLen*2)
	format.WriteString(strs[0])
	for _, str := range strs[1:] {
		format.WriteString(", ")
		format.WriteString(str)
	}
	str = format.String()
	merged.str = str
	return
}

// mergeErrs merges an existing error with a new one
func mergeErrs(err error, format string, a ...interface{}) error {
	newErr := errorF(format, a...)
	if err == nil {
		return newErr
	}
	var merged []error
	if merge, isMergedErr := err.(*mergedErr); isMergedErr {
		merged = append(append([]error(nil), merge.mergedErrs...), newErr)
	} else {
		merged = []error{err, newErr}
	}
	return &mergedErr{mergedErrs: merged}
}

// mergeErrors merges multiple errors
func mergeAllErrs(errs ...error) error {
	var all []error
	allLen := len(errs)
	if allLen <= 1 {
		if allLen == 0 {
			return nil
		}
		return errs[0]
	}
	for _, err := range errs {
		if err != nil {
			if merge, isMergedErr := err.(*mergedErr); isMergedErr {
				all = append(all, merge.mergedErrs...)
			} else {
				all = append(all, err)
			}
		}
	}
	allLen = len(all)
	if allLen <= 1 {
		if allLen == 0 {
			return nil
		}
		return all[0]
	}
	return &mergedErr{mergedErrs: all}
}