From 1a7d8d6431a54671ea4fefce6d4e5ff3d3ffdfa1 Mon Sep 17 00:00:00 2001 From: tidwall Date: Sat, 7 Nov 2020 05:22:11 -0700 Subject: [PATCH] Added ENV var for 500 http errors --- internal/server/server.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/server/server.go b/internal/server/server.go index 1ac710e6..8add364d 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -25,6 +25,7 @@ import ( "github.com/tidwall/buntdb" "github.com/tidwall/geojson" "github.com/tidwall/geojson/geometry" + "github.com/tidwall/gjson" "github.com/tidwall/rbang" "github.com/tidwall/redcon" "github.com/tidwall/resp" @@ -81,6 +82,7 @@ type Server struct { // env opts geomParseOpts geojson.ParseOptions geomIndexOpts geometry.IndexOptions + http500Errors bool // atomics followc aint // counter increases when follow property changes @@ -179,6 +181,10 @@ func Serve(host string, port int, dir string, http bool) error { return err } + // Send "500 Internal Server" error instead of "200 OK" for json responses + // with `"ok":false`. T38HTTP500ERRORS=1 + server.http500Errors, _ = strconv.ParseBool(os.Getenv("T38HTTP500ERRORS")) + // Allow for geometry indexing options through environment variables: // T38IDXGEOMKIND -- None, RTree, QuadTree // T38IDXGEOM -- Min number of points in a geometry for indexing. @@ -708,11 +714,15 @@ func (server *Server) handleInputCommand(client *Client, msg *Message) error { case WebSocket: return WriteWebSocketMessage(client, []byte(res)) case HTTP: - _, err := fmt.Fprintf(client, "HTTP/1.1 200 OK\r\n"+ + status := "200 OK" + if server.http500Errors && !gjson.Get(res, "ok").Bool() { + status = "500 Internal Server Error" + } + _, err := fmt.Fprintf(client, "HTTP/1.1 %s\r\n"+ "Connection: close\r\n"+ "Content-Length: %d\r\n"+ "Content-Type: application/json; charset=utf-8\r\n"+ - "\r\n", len(res)+2) + "\r\n", status, len(res)+2) if err != nil { return err }