mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-10-21 06:37:21 +00:00
If proxying of the query fails - return 500.
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
fiber "github.com/gofiber/fiber/v2"
|
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()})
|
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)
|
c.Response().Header.Del(fiber.HeaderServer)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,7 +127,13 @@ func processGraphQLRequest(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
// Additional helper function to avoid code repetition
|
// Additional helper function to avoid code repetition
|
||||||
func proxyAndCacheTheRequest(c *fiber.Ctx, queryCacheHash string, cache_time int) {
|
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)
|
cfg.Cache.CacheClient.Set(queryCacheHash, c.Response().Body(), time.Duration(cache_time)*time.Second)
|
||||||
c.Send(c.Response().Body())
|
c.Send(c.Response().Body())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user