From bf18f36e4576640e2e995936dd4f02261fd1d7ae Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Fri, 13 Oct 2023 14:48:53 +0100 Subject: [PATCH] If proxying of the query fails - return 500. --- proxy.go | 6 ++++++ server.go | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/proxy.go b/proxy.go index 25f5949..30aaff8 100644 --- a/proxy.go +++ b/proxy.go @@ -2,6 +2,7 @@ package main import ( "crypto/tls" + "fmt" "time" fiber "github.com/gofiber/fiber/v2" @@ -47,6 +48,11 @@ func proxyTheRequest(c *fiber.Ctx) error { } cfg.Logger.Debug("Received proxied response", map[string]interface{}{"path": c.Path(), "response_body": string(c.Response().Body()), "response_code": c.Response().StatusCode()}) + if c.Response().StatusCode() != 200 { + cfg.Monitoring.Increment(libpack_monitoring.MetricsFailed, nil) + return fmt.Errorf("Received non-200 response from the GraphQL server: %d", c.Response().StatusCode()) + } + c.Response().Header.Del(fiber.HeaderServer) return nil } diff --git a/server.go b/server.go index 9a35811..be32e24 100644 --- a/server.go +++ b/server.go @@ -127,7 +127,13 @@ func processGraphQLRequest(c *fiber.Ctx) error { // Additional helper function to avoid code repetition func proxyAndCacheTheRequest(c *fiber.Ctx, queryCacheHash string, cache_time int) { - proxyTheRequest(c) + err := proxyTheRequest(c) + if err != nil { + cfg.Logger.Error("Can't proxy the request", map[string]interface{}{"error": err.Error()}) + cfg.Monitoring.Increment(libpack_monitoring.MetricsFailed, nil) + c.Status(500).SendString("Can't proxy the request - try again later") + return + } cfg.Cache.CacheClient.Set(queryCacheHash, c.Response().Body(), time.Duration(cache_time)*time.Second) c.Send(c.Response().Body()) }