Skip to content

Commit 0444725

Browse files
committed
FAQ: guidance on panic recovery
1 parent e509950 commit 0444725

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

_src/faq/index.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,25 @@ For more on logging philosophy, see
269269

270270
Collecting, shipping, and aggregating logs is the responsibility of the platform, not individual services.
271271
So, just make sure you're writing logs to stdout/stderr, and let another component handle the rest.
272+
273+
## Panics — How should my service handle panics?
274+
275+
Panics indicate programmer error and signal corrputed program state.
276+
They shouldn't be treated as errors, or ersatz exceptions.
277+
In general, you shouldn't explicitly recover from panics:
278+
you should allow them to crash your program or handler goroutine,
279+
and allow your service to return a broken response to the calling client.
280+
Your observability stack should alert you to these problems as they occur,
281+
and you should fix them as soon as possible.
282+
283+
With that said, if you have the need to handle exceptions, the best strategy
284+
is probably to wrap the concrete transport with a transport-specific middleware
285+
that performs a recover.
286+
For example, with HTTP:
287+
288+
```go
289+
var h http.Handler
290+
h = httptransport.NewServer(...)
291+
h = newRecoveringMiddleware(h, ...)
292+
// use h normally
293+
```

0 commit comments

Comments
 (0)