From 0c4e8aeb2b32d576f2408c888792721fbc5bd1f2 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 1 Jul 2020 00:02:02 +0300 Subject: [PATCH] all: use `errors.As` for inspecting errors that implement httpserver.ErrorWithStatusCode --- app/vmselect/main.go | 4 +++- lib/httpserver/httpserver.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/vmselect/main.go b/app/vmselect/main.go index 097fd59c7..837fec506 100644 --- a/app/vmselect/main.go +++ b/app/vmselect/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "flag" "fmt" "net/http" @@ -318,7 +319,8 @@ func sendPrometheusError(w http.ResponseWriter, r *http.Request, err error) { w.Header().Set("Content-Type", "application/json") statusCode := http.StatusUnprocessableEntity - if esc, ok := err.(*httpserver.ErrorWithStatusCode); ok { + var esc *httpserver.ErrorWithStatusCode + if errors.As(err, &esc) { statusCode = esc.StatusCode } w.WriteHeader(statusCode) diff --git a/lib/httpserver/httpserver.go b/lib/httpserver/httpserver.go index c4930c809..739b31d91 100644 --- a/lib/httpserver/httpserver.go +++ b/lib/httpserver/httpserver.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "crypto/tls" + "errors" "flag" "fmt" "io" @@ -436,8 +437,9 @@ func Errorf(w http.ResponseWriter, format string, args ...interface{}) { // Extract statusCode from args statusCode := http.StatusBadRequest + var esc *ErrorWithStatusCode for _, arg := range args { - if esc, ok := arg.(*ErrorWithStatusCode); ok { + if err, ok := arg.(error); ok && errors.As(err, &esc) { statusCode = esc.StatusCode break }