-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
91 lines (75 loc) · 2.07 KB
/
errors.go
File metadata and controls
91 lines (75 loc) · 2.07 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
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
package pubsub
import (
"errors"
"fmt"
)
// Error represents a pubsub library error with categorization.
type Error struct {
// Code is a machine-readable error code
Code string
// Message is a human-readable error message
Message string
// Err is the underlying error (if any)
Err error
}
// Error implements the error interface.
func (e *Error) Error() string {
if e.Err != nil {
return fmt.Sprintf("%s: %s: %v", e.Code, e.Message, e.Err)
}
return fmt.Sprintf("%s: %s", e.Code, e.Message)
}
// Unwrap returns the underlying error.
func (e *Error) Unwrap() error {
return e.Err
}
// Error codes for pubsub operations.
const (
// ErrCodeNoData indicates no data was found.
ErrCodeNoData = "NO_DATA"
// ErrCodeValidation indicates validation failed.
ErrCodeValidation = "VALIDATION_ERROR"
// ErrCodeConfiguration indicates invalid configuration.
ErrCodeConfiguration = "CONFIGURATION_ERROR"
// ErrCodeDatabase indicates database operation failed.
ErrCodeDatabase = "DATABASE_ERROR"
// ErrCodeDelivery indicates message delivery failed.
ErrCodeDelivery = "DELIVERY_ERROR"
)
// Common errors.
var (
// ErrNoData is returned when a query returns no results.
// This is not necessarily an error condition in all cases.
ErrNoData = &Error{
Code: ErrCodeNoData,
Message: "no data found",
}
// ErrInvalidConfiguration is returned when worker configuration is invalid.
ErrInvalidConfiguration = &Error{
Code: ErrCodeConfiguration,
Message: "invalid worker configuration",
}
)
// NewError creates a new Error with the given code and message.
func NewError(code, message string) *Error {
return &Error{
Code: code,
Message: message,
}
}
// NewErrorWithCause creates a new Error wrapping an underlying error.
func NewErrorWithCause(code, message string, cause error) *Error {
return &Error{
Code: code,
Message: message,
Err: cause,
}
}
// IsNoData checks if an error is ErrNoData.
func IsNoData(err error) bool {
var pubsubErr *Error
if errors.As(err, &pubsubErr) {
return pubsubErr.Code == ErrCodeNoData
}
return errors.Is(err, ErrNoData)
}