Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2361 from gobuffalo/enable-stack-trace-for-intern…
Browse files Browse the repository at this point in the history
…al-errors

enabled stack trace when the original error support it (in dev and event)
  • Loading branch information
sio4 authored Jan 24, 2023
2 parents e3d3a01 + 2a90833 commit 63a5957
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 3 additions & 2 deletions error.dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,12 @@

table.table tbody tr td {
border-top: 0;
padding: 10px
border-bottom: 1px dotted #ddd;
padding: 2px;
}

pre {
white-space: pre-line;
white-space: pre-wrap;
margin-bottom: 10px;
max-height: 275px;
overflow: scroll
Expand Down
16 changes: 14 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ func (a *App) defaultErrorMiddleware(next Handler) Handler {
if err == nil {
return nil
}

// 500 Internal Server Error by default
status := http.StatusInternalServerError

// unpack root err and check for HTTPError
if errors.Is(err, sql.ErrNoRows) {
status = http.StatusNotFound
Expand All @@ -123,9 +126,18 @@ func (a *App) defaultErrorMiddleware(next Handler) Handler {
if errors.As(err, &h) {
status = h.Status
}

payload := events.Payload{
"context": c,
"app": a,
"status": status,
"error": err,
}
if status >= http.StatusInternalServerError {
// we need the details (or stack trace) only for 5xx errors.
// pkg/errors supports '%+v' for stack trace.
// the other type of errors that support '%+v' is also supported.
payload["stacktrace"] = fmt.Sprintf("%+v", err)
}
events.EmitError(events.ErrGeneral, err, payload)

Expand Down Expand Up @@ -190,7 +202,7 @@ func defaultErrorHandler(status int, origErr error, c Context) error {
}
}

trace := origErr.Error()
trace := fmt.Sprintf("%+v", origErr)
if cause := errors.Unwrap(origErr); cause != nil {
origErr = cause
}
Expand Down Expand Up @@ -269,5 +281,5 @@ func (i inspectHeaders) String() string {
bb = append(bb, fmt.Sprintf("%s: %s", k, v))
}
sort.Strings(bb)
return strings.Join(bb, "\n\n")
return strings.Join(bb, "\n")
}

0 comments on commit 63a5957

Please sign in to comment.