Skip to content

Commit

Permalink
middleware.Recoverer, stack formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed Mar 24, 2020
1 parent 6874595 commit d54e3e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
3 changes: 3 additions & 0 deletions _examples/hello-world/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ func main() {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
})

http.ListenAndServe(":3333", r)
}
35 changes: 26 additions & 9 deletions middleware/recoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s prettyStack) parse(debugStack []byte, rvr interface{}) ([]byte, error) {
}

if strings.HasPrefix(stack[0], "goroutine") {
cW(buf, useColor, bBlue, "%s\n\n", stack[0])
cW(buf, useColor, bCyan, "%s\n\n", stack[0])
}

// reverse
Expand Down Expand Up @@ -140,9 +140,18 @@ func (s prettyStack) decorateFuncCallLine(line string, useColor bool, num int) (
pkg += method[0:idx]
method = method[idx:]
}
pkgColor := nYellow
methodColor := bGreen

cW(buf, useColor, nYellow, " %s", pkg)
cW(buf, useColor, bGreen, "%s\n", method)
if num == 0 {
cW(buf, useColor, bRed, " -> ")
pkgColor = bMagenta
methodColor = bRed
} else {
cW(buf, useColor, bWhite, " ")
}
cW(buf, useColor, pkgColor, "%s", pkg)
cW(buf, useColor, methodColor, "%s\n", method)
// cW(buf, useColor, nBlack, "%s", addr)
return buf.String(), nil
}
Expand All @@ -165,15 +174,23 @@ func (s prettyStack) decorateSourceLine(line string, useColor bool, num int) (st
if idx > 0 {
lineno = lineno[0:idx]
}
fileColor := bCyan
lineColor := bGreen

cW(buf, useColor, bWhite, " %s", dir)
cW(buf, useColor, bCyan, "%s", file)
cW(buf, useColor, bGreen, "%s", lineno)

if num == 1 {
cW(buf, useColor, bRed, " <--")
cW(buf, useColor, bRed, " -> ")
fileColor = bRed
lineColor = bMagenta
} else {
cW(buf, useColor, bWhite, " ")
}
cW(buf, useColor, bWhite, "%s", dir)
cW(buf, useColor, fileColor, "%s", file)
cW(buf, useColor, lineColor, "%s", lineno)
if num == 1 {
cW(buf, useColor, bRed, "\n")
}
cW(buf, useColor, bWhite, "\n\n")
cW(buf, useColor, bWhite, "\n")

return buf.String(), nil
}

0 comments on commit d54e3e9

Please sign in to comment.