mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-02-09 15:27:11 +00:00
all: use errors.As
for inspecting errors that implement httpserver.ErrorWithStatusCode
This commit is contained in:
parent
d962568e93
commit
0c4e8aeb2b
2 changed files with 6 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -318,7 +319,8 @@ func sendPrometheusError(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
statusCode := http.StatusUnprocessableEntity
|
statusCode := http.StatusUnprocessableEntity
|
||||||
if esc, ok := err.(*httpserver.ErrorWithStatusCode); ok {
|
var esc *httpserver.ErrorWithStatusCode
|
||||||
|
if errors.As(err, &esc) {
|
||||||
statusCode = esc.StatusCode
|
statusCode = esc.StatusCode
|
||||||
}
|
}
|
||||||
w.WriteHeader(statusCode)
|
w.WriteHeader(statusCode)
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -436,8 +437,9 @@ func Errorf(w http.ResponseWriter, format string, args ...interface{}) {
|
||||||
|
|
||||||
// Extract statusCode from args
|
// Extract statusCode from args
|
||||||
statusCode := http.StatusBadRequest
|
statusCode := http.StatusBadRequest
|
||||||
|
var esc *ErrorWithStatusCode
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
if esc, ok := arg.(*ErrorWithStatusCode); ok {
|
if err, ok := arg.(error); ok && errors.As(err, &esc) {
|
||||||
statusCode = esc.StatusCode
|
statusCode = esc.StatusCode
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue