-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cast.go
51 lines (46 loc) · 1.16 KB
/
cast.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
// Copyright 2022 Ainsley Clark. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package errors
import "fmt"
// Code returns the code of the root error, if available.
// Otherwise, returns INTERNAL.
func Code(err error) string {
if err == nil {
return ""
} else if e, ok := err.(*Error); ok && e.Code != "" {
return e.Code
} else if ok && e.Err != nil {
return Code(e.Err)
}
return INTERNAL
}
// Message returns the human-readable message of the error,
// if available. Otherwise, returns a generic error
// message.
func Message(err error) string {
if err == nil {
return ""
} else if e, ok := err.(*Error); ok && e.Message != "" {
return e.Message
} else if ok && e.Err != nil {
return Message(e.Err)
}
return GlobalError
}
// ToError Returns an application error from input. If The type
// is not of type Error, nil will be returned.
func ToError(err any) *Error {
switch v := err.(type) {
case *Error:
return v
case Error:
return &v
case error:
return &Error{Err: fmt.Errorf(v.Error())}
case string:
return &Error{Err: fmt.Errorf(v)}
default:
return nil
}
}