/
exit_handler.go
71 lines (58 loc) · 1.42 KB
/
exit_handler.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
package foundation
import (
"errors"
"fmt"
"runtime/debug"
"github.com/asaskevich/EventBus"
"github.com/lara-go/larago/logger"
"github.com/lara-go/larago/logger/format"
"github.com/lara-go/larago/support/utils"
"github.com/urfave/cli"
)
// ExitHandler handles all panics.
type ExitHandler struct {
Events *EventBus.EventBus
}
// Exit terminates application.
func (h *ExitHandler) Exit(message string, exitCode int) {
cli.HandleExitCoder(
cli.NewExitError(message, exitCode),
)
}
// Defer to handle panics.
func (h *ExitHandler) Defer() {
var err error
var ok bool
// Try to recover.
if r := recover(); r != nil {
if err, ok = r.(error); !ok {
err = errors.New(r.(string))
}
h.handleError(err)
}
}
// HandleError writes error to the output and terminates.
func (h *ExitHandler) handleError(err error) {
message := err.Error()
exitCode := 2
// Check if error is already an instance of cli.ExitCoder
// and update exit code
if exitErr, ok := err.(cli.ExitCoder); ok {
exitCode = exitErr.ExitCode()
}
// Color output.
if logger.IsTTY() {
message = format.Red(message).Format()
}
// Find trower line.
file, line := utils.Thrower("larago/errors")
// Publish event.
if h.Events != nil {
h.Events.Publish("panic", message, file, line, debug.Stack(), exitCode)
}
// Handle exit with formatted error message.
h.Exit(
fmt.Sprintf("%s\n--> %s:%d\n%s", message, file, line, debug.Stack()),
exitCode,
)
}