We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

HTTP Error Responses

Now that we can pass logging context upstream, we can improve HTTP error logging. Wouldn't it be useful to include stack traces whenever we log an error status?

We'll start by adding a small helper that wraps http.Error. It still sends the HTTP response, but first stores the error in LogContext (if present) so request logs can include it:

func httpError(ctx context.Context, w http.ResponseWriter, status int, err error) {
	if logCtx, ok := ctx.Value(logContextKey).(*LogContext); ok {
		logCtx.Error = err
	}
	http.Error(w, err.Error(), status)
}

Now request logs for error responses include full error details (including stack traces when present). How handy is that?

Assignment

Capture HTTP response errors in request logs.

  1. Pass lowercase internal messages (for example, unauthorized and internal server error) so logs stay consistent. That's the Go standard.

Restart your server with LINKO_LOG_FILE=linko.access.log set:

LINKO_LOG_FILE=linko.access.log go run .

Run and submit the CLI tests from the root of the Linko repo.