Skip to content

Commit 63a5957

Browse files
authored
Merge pull request #2361 from gobuffalo/enable-stack-trace-for-internal-errors
enabled stack trace when the original error support it (in dev and event)
2 parents e3d3a01 + 2a90833 commit 63a5957

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

error.dev.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,12 @@
496496

497497
table.table tbody tr td {
498498
border-top: 0;
499-
padding: 10px
499+
border-bottom: 1px dotted #ddd;
500+
padding: 2px;
500501
}
501502

502503
pre {
503-
white-space: pre-line;
504+
white-space: pre-wrap;
504505
margin-bottom: 10px;
505506
max-height: 275px;
506507
overflow: scroll

errors.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ func (a *App) defaultErrorMiddleware(next Handler) Handler {
114114
if err == nil {
115115
return nil
116116
}
117+
118+
// 500 Internal Server Error by default
117119
status := http.StatusInternalServerError
120+
118121
// unpack root err and check for HTTPError
119122
if errors.Is(err, sql.ErrNoRows) {
120123
status = http.StatusNotFound
@@ -123,9 +126,18 @@ func (a *App) defaultErrorMiddleware(next Handler) Handler {
123126
if errors.As(err, &h) {
124127
status = h.Status
125128
}
129+
126130
payload := events.Payload{
127131
"context": c,
128132
"app": a,
133+
"status": status,
134+
"error": err,
135+
}
136+
if status >= http.StatusInternalServerError {
137+
// we need the details (or stack trace) only for 5xx errors.
138+
// pkg/errors supports '%+v' for stack trace.
139+
// the other type of errors that support '%+v' is also supported.
140+
payload["stacktrace"] = fmt.Sprintf("%+v", err)
129141
}
130142
events.EmitError(events.ErrGeneral, err, payload)
131143

@@ -190,7 +202,7 @@ func defaultErrorHandler(status int, origErr error, c Context) error {
190202
}
191203
}
192204

193-
trace := origErr.Error()
205+
trace := fmt.Sprintf("%+v", origErr)
194206
if cause := errors.Unwrap(origErr); cause != nil {
195207
origErr = cause
196208
}
@@ -269,5 +281,5 @@ func (i inspectHeaders) String() string {
269281
bb = append(bb, fmt.Sprintf("%s: %s", k, v))
270282
}
271283
sort.Strings(bb)
272-
return strings.Join(bb, "\n\n")
284+
return strings.Join(bb, "\n")
273285
}

0 commit comments

Comments
 (0)