This repository has been archived by the owner on Apr 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
96 lines (78 loc) · 2.32 KB
/
errors.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
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
// Author: Liam Stanley <[email protected]>
// Docs: https://marill.liam.sh/
// Repo: https://github.com/lrstanley/marill
package main
import "fmt"
// Err represents the custom error methods
type Err interface {
error
GetCode() int
}
// NewErr is a custom error struct representing the error with additional
// information
type NewErr struct {
Code int
value string
deepErr error
}
// GetCode returns the code of the error, useful to reference errMsg
func (e NewErr) GetCode() int {
return e.Code
}
// Error replaces the default Error method
func (e NewErr) Error() string {
switch {
case e.Code == ErrUpgradedError && e.value == "" && e.deepErr != nil:
return e.deepErr.Error()
case e.deepErr == nil && e.value == "":
return errMsg[e.Code]
case e.deepErr == nil && e.value != "":
return fmt.Sprintf(errMsg[e.Code], e.value)
case e.value == "" && e.deepErr != nil:
return fmt.Sprintf(errMsg[e.Code], e.deepErr)
default:
return fmt.Sprintf(errMsg[e.Code], e.value, e.deepErr)
}
}
// UpgradeErr takes a standard error interface and upgrades it to our
// custom error types
func UpgradeErr(e error) *NewErr {
if e == nil {
return nil
}
return &NewErr{Code: ErrUpgradedError, deepErr: e}
}
// map each error name to a unique id
const (
ErrUpgradedError = 1 << iota
// cli parse errors
ErrInstantiateApp
ErrBadDomains
ErrDomains
// process fetching
ErrProcList
// domain fetching
ErrGetDomains
ErrNoDomainsFound
// update checks
ErrUpdateUnknownResp
ErrUpdate
ErrUpdateGeneric
)
// errMsg contains a map of error name id keys and error/deep error pairs
var errMsg = map[int]string{
ErrUpgradedError: "not a real error",
// cli parse errors
ErrInstantiateApp: "unable to instantiate app: %s",
ErrBadDomains: "invalid domain manually provided: %s",
ErrDomains: "unable to parse domain list: %s",
// process fetching
ErrProcList: "unable to get process list: %s",
// domain fetching
ErrGetDomains: "unable to auto-fetch domain list: %s",
ErrNoDomainsFound: "domain search found no results (domain filters?)",
// update checks
ErrUpdateUnknownResp: "update check: received unknown response from Github: %s",
ErrUpdate: "update check: error: %s: %s",
ErrUpdateGeneric: "{red}an unknown error occurred while attempting to check for updates. see debugging for more info.{c}",
}