Access response writer #6

Closed
opened 2025-08-26 10:32:17 +00:00 by mybigman · 2 comments

How to access response writer or do I need to set manually at lower level?

// normally do something like this
func Recover(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if r := recover(); r != nil {
				buf := make([]byte, 2048)
				n := runtime.Stack(buf, false)

				fmt.Fprintf(os.Stderr, "panic recovered: %v\n\n%s", r, buf[:n])

				http.Error(w, "Internal Server Error", http.StatusInternalServerError)
			}
		}()

		next.ServeHTTP(w, r)
	})
}

Is this correct approach?

func Recover(ctx web.Context) error {
	defer func() {
		if r := recover(); r != nil {
			buf := make([]byte, 2048)
			n := runtime.Stack(buf, false)

			fmt.Fprintf(os.Stderr, "panic recovered: %v\n\n%s", r, buf[:n])

			ctx.Response().SetHeader("Content-Type", "text/plain; charset=utf-8")
			ctx.Response().SetHeader("X-Content-Type-Options", "nosniff")

			ctx.Response().SetStatus(http.StatusInternalServerError)

			_, _ = ctx.Response().Write([]byte("Internal Server Error\n"))
		}
	}()

	return ctx.Next(ctx)
}
How to access response writer or do I need to set manually at lower level? ```go // normally do something like this func Recover(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer func() { if r := recover(); r != nil { buf := make([]byte, 2048) n := runtime.Stack(buf, false) fmt.Fprintf(os.Stderr, "panic recovered: %v\n\n%s", r, buf[:n]) http.Error(w, "Internal Server Error", http.StatusInternalServerError) } }() next.ServeHTTP(w, r) }) } ``` Is this correct approach? ```go func Recover(ctx web.Context) error { defer func() { if r := recover(); r != nil { buf := make([]byte, 2048) n := runtime.Stack(buf, false) fmt.Fprintf(os.Stderr, "panic recovered: %v\n\n%s", r, buf[:n]) ctx.Response().SetHeader("Content-Type", "text/plain; charset=utf-8") ctx.Response().SetHeader("X-Content-Type-Options", "nosniff") ctx.Response().SetStatus(http.StatusInternalServerError) _, _ = ctx.Response().Write([]byte("Internal Server Error\n")) } }() return ctx.Next(ctx) } ```
ed self-assigned this 2025-08-26 10:53:16 +00:00
Owner

I suppose you could do that but I think it's better to clear the body first with SetBody(nil):

ctx.Response().SetBody(nil)
ctx.Response().WriteString("Internal Server Error\n")

Or shorter:

ctx.Response().SetBody([]byte("Internal Server Error\n"))
I suppose you could do that but I think it's better to clear the body first with `SetBody(nil)`: ```go ctx.Response().SetBody(nil) ctx.Response().WriteString("Internal Server Error\n") ``` Or shorter: ```go ctx.Response().SetBody([]byte("Internal Server Error\n")) ```
Author

thanks mate, wasn't sure if I missing something as went through the code and test cases.

thanks mate, wasn't sure if I missing something as went through the code and test cases.
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: go/web#6
No description provided.