-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
55 lines (42 loc) · 1.1 KB
/
error.go
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
package art
import (
"errors"
"fmt"
)
var (
ErrClosed = NewCustomError(2001, "service has been closed")
ErrNotFound = NewCustomError(2100, "not found")
ErrNotFoundSubject = NewCustomError(2101, "not found subject mux")
)
//
func ErrorExtractCode(err error) int {
var Err *CustomError
if errors.As(err, &Err) {
return Err.myCode
}
unknown := 0
return unknown
}
func ErrorJoin3rdPartyWithMsg(myErr error, Err3rd error, msg string, args ...any) error {
return fmt.Errorf("%v: %w: %w", fmt.Sprintf(msg, args...), Err3rd, myErr)
}
func ErrorJoin3rdParty(myErr error, Err3rd error) error {
return fmt.Errorf("%w: %w", Err3rd, myErr)
}
func ErrorWrapWithMessage(myErr error, msg string, args ...any) error {
return fmt.Errorf("%v: %w", fmt.Sprintf(msg, args...), myErr)
}
func NewCustomError(myCode int, title string) *CustomError {
return &CustomError{title: title, myCode: myCode}
}
type CustomError struct {
title string
myCode int
}
func (c *CustomError) Error() string {
return c.title
}
func (c *CustomError) MyCode() int {
return c.myCode
}
func (c *CustomError) CustomError() {}