all: use errors.As for inspecting errors that implement httpserver.ErrorWithStatusCode

This commit is contained in:
Aliaksandr Valialkin 2020-07-01 00:02:02 +03:00
parent d5dddb0953
commit 7c2c8b2981
2 changed files with 6 additions and 2 deletions

View file

@ -1,6 +1,7 @@
package vmselect
import (
"errors"
"flag"
"fmt"
"net/http"
@ -240,7 +241,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)

View file

@ -4,6 +4,7 @@ import (
"bufio"
"context"
"crypto/tls"
"errors"
"flag"
"fmt"
"io"
@ -487,8 +488,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
}