-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmain.go
76 lines (65 loc) · 1.57 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
)
// Some code is extracted by https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully
// ReadFile Reads a file and return your content
func ReadFile(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
// wrap error with a text representing what happens
return nil, errors.Wrap(err, "open failed")
}
defer f.Close()
buf, err := ioutil.ReadAll(f)
if err != nil {
// wrap error with a text representing what happens
return nil, errors.Wrap(err, "read failed")
}
return buf, nil
}
// ReadConfig returns config, but also wraps the error with message
func ReadConfig() ([]byte, error) {
home := os.Getenv("HOME")
config, err := ReadFile(filepath.Join(home, ".settings.xml"))
return config, errors.Wrap(err, "could not read config")
}
func one() error {
// one call two, two call three and and an error happens
err := two()
if err != nil {
return errors.Wrap(err, "error on two")
}
return nil
}
func two() error {
// something happens is not intuitive
err := three()
if err != nil {
return errors.Wrap(err, "error on three")
}
anotherErr := four()
if anotherErr != nil {
return errors.Wrap(anotherErr, "error on four")
}
return nil
}
func three() error {
return fmt.Errorf("something happens")
}
func four() error {
return fmt.Errorf("something happens")
}
func main() {
// err := one()
_, err := ReadConfig()
if err != nil {
// print complete traceback
stack := errors.WithStack(err)
fmt.Printf("%+v", stack)
}
}