Server: Write internal server error on missing write (#57813)
This commit is contained in:
+10
-1
@@ -22,6 +22,9 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
"github.com/grafana/grafana/pkg/util/errutil/errhttp"
|
||||
)
|
||||
|
||||
// Context represents the runtime context of current request of Macaron instance.
|
||||
@@ -34,6 +37,8 @@ type Context struct {
|
||||
template *template.Template
|
||||
}
|
||||
|
||||
var errMissingWrite = errutil.NewBase(errutil.StatusInternal, "web.missingWrite")
|
||||
|
||||
func (ctx *Context) run() {
|
||||
h := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
||||
for i := len(ctx.mws) - 1; i >= 0; i-- {
|
||||
@@ -47,7 +52,11 @@ func (ctx *Context) run() {
|
||||
// This indicates nearly always that a middleware is misbehaving and not calling its next.ServeHTTP().
|
||||
// In rare cases where a blank http.StatusOK without any body is wished, explicitly state that using w.WriteStatus(http.StatusOK)
|
||||
if !rw.Written() {
|
||||
panic("chain did not write HTTP response")
|
||||
errhttp.Write(
|
||||
ctx.Req.Context(),
|
||||
errMissingWrite.Errorf("chain did not write HTTP response: %s", ctx.Req.URL.Path),
|
||||
rw,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,11 @@ package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
)
|
||||
|
||||
@@ -94,3 +97,17 @@ func TestContext_RemoteAddr(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_noHandler(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
method := http.MethodGet
|
||||
c := &Context{
|
||||
Req: httptest.NewRequest(method, "/", nil),
|
||||
Resp: NewResponseWriter(method, recorder),
|
||||
}
|
||||
|
||||
c.run()
|
||||
|
||||
assert.Equal(t, http.StatusInternalServerError, recorder.Code)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user