/
error_handling.go
88 lines (73 loc) · 1.81 KB
/
error_handling.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
/*
Package helper consists of helping functions.
Copyright 2021 Michael Bungenstock
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
package helper
import (
"fmt"
"io"
"os"
"strings"
"github.com/fuxs/aepctl/api"
"github.com/spf13/cobra"
)
// CheckErr prints a user friendly error message to stderr
func CheckErr(err error) {
formatError(err, fatal)
}
func CheckErrInfo(err error) {
formatError(err, info)
}
func CheckErrParams(params api.ParametersE) *api.Request {
p, err := params.Request()
if err != nil {
formatError(err, fatal)
}
return p
}
// CheckErrEOF prints a user friendly error message to stderr but ignores EOF
func CheckErrEOF(err error) {
if err == io.EOF {
return
}
formatError(err, fatal)
}
func info(msg string, code int) {
if len(msg) > 0 {
if !strings.HasSuffix(msg, "\n") {
msg = msg + "\n"
}
fmt.Fprint(os.Stderr, msg)
}
}
func fatal(msg string, code int) {
info(msg, code)
os.Exit(code)
}
func formatError(err error, handler func(string, int)) {
if err == nil {
return
}
handler(err.Error(), 1)
}
// CheckErrs prints a user friendly error message to stderr
func CheckErrs(err ...error) {
for _, e := range err {
CheckErr(e)
}
}
func PrintError(msg string, cmd *cobra.Command) {
fmt.Fprintln(os.Stderr, msg)
if err := cmd.Help(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
os.Exit(1)
}