-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
48 lines (39 loc) · 1.11 KB
/
Copy patherrors.go
File metadata and controls
48 lines (39 loc) · 1.11 KB
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
/*
* Copyright (c) 2024-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
// Package errors provides error construction, wrapping, and inspection helpers.
package errors
import "strings"
const causeSeparator = ": "
type errorEntity struct {
cause error
message string
trace string
}
// New returns an error with the provided message.
func New(message string) error {
return &errorEntity{message: message}
}
func (v *errorEntity) Error() string {
if v.cause == nil {
return v.message + v.trace
}
cause := v.cause.Error()
var message strings.Builder
message.Grow(len(v.message) + len(cause) + len(v.trace) + len(causeSeparator))
_, _ = message.WriteString(v.message)
if v.message != "" {
_, _ = message.WriteString(causeSeparator)
}
_, _ = message.WriteString(cause)
_, _ = message.WriteString(v.trace)
return message.String()
}
// Cause returns the underlying cause.
func (v *errorEntity) Cause() error {
return v.cause
}
func (v *errorEntity) Unwrap() error {
return v.cause
}