Skip to content

Commit

Permalink
Return 404 Not Found responses as JSON
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
  • Loading branch information
chaudum committed Dec 2, 2021
1 parent f700a24 commit 4820322
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
2 changes: 0 additions & 2 deletions docs/sources/upgrading/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ The response body has the following schema:
}
```

"404 Not Found" errors still return responses with content type `text/plain`.

### Promtail

#### `gcplog` labels have changed
Expand Down
1 change: 1 addition & 0 deletions pkg/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ func (t *Loki) Run(opts RunOpts) error {

t.serviceMap = serviceMap
t.Server.HTTP.Path("/services").Methods("GET").Handler(http.HandlerFunc(t.servicesHandler))
t.Server.HTTP.NotFoundHandler = http.HandlerFunc(serverutil.NotFoundHandler)

// get all services, create service manager and tell it to start
var servs []services.Service
Expand Down
6 changes: 5 additions & 1 deletion pkg/util/server/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ type ErrorResponseBody struct {
Message string `json:"message"`
}

func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
JSONError(w, 404, "page not found")
}

func JSONError(w http.ResponseWriter, code int, message string, args ...interface{}) {
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
json.NewEncoder(w).Encode(ErrorResponseBody{
Code: code,
Status: "error",
Expand Down

0 comments on commit 4820322

Please sign in to comment.