-
Notifications
You must be signed in to change notification settings - Fork 0
/
die.go
42 lines (37 loc) · 977 Bytes
/
die.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
//spellchecker:words exit
package exit
//spellchecker:words github pkglib docfmt stream
import (
"github.com/tkw1536/pkglib/docfmt"
"github.com/tkw1536/pkglib/stream"
)
var errDieUnknown = Error{
ExitCode: ExitGeneric,
Message: "unknown error",
}
// Die prints a non-nil err to io.Stderr and returns an error of type Error or nil.
//
// When error is non-nil, this function first turns err into type Error.
// Then if err.Message is not the empty string, it prints it to io.Stderr with wrapping.
//
// If err is nil, it does nothing and returns nil.
func Die(str stream.IOStream, err error) error {
var e Error
switch ee := err.(type) {
case nil:
return nil
case Error:
e = ee
default:
e = errDieUnknown.Wrap(ee)
}
// print the error message to standard error in a wrapped way
if message := e.Error(); message != "" {
if stream.IsNullWriter(str.Stderr) {
docfmt.Format(message)
return e
}
str.EPrintln(docfmt.Format(message))
}
return e
}