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

enabled stack trace when the original error support it (in dev and event) #2361

Merged
merged 2 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}