From 3389e57c48ef58543b30d8756237c29da3ba00d1 Mon Sep 17 00:00:00 2001 From: Panagiotis Siatras Date: Fri, 18 Mar 2022 17:34:10 +0200 Subject: [PATCH] api/render: implemented Error --- api/errors.go | 66 -------------------------------------------- api/render/render.go | 57 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 66 deletions(-) delete mode 100644 api/errors.go diff --git a/api/errors.go b/api/errors.go deleted file mode 100644 index 49efd486..00000000 --- a/api/errors.go +++ /dev/null @@ -1,66 +0,0 @@ -package api - -import ( - "encoding/json" - "fmt" - "net/http" - "os" - - "github.com/pkg/errors" - - "github.com/smallstep/certificates/acme" - "github.com/smallstep/certificates/api/log" - "github.com/smallstep/certificates/authority/admin" - "github.com/smallstep/certificates/errs" - "github.com/smallstep/certificates/logging" - "github.com/smallstep/certificates/scep" -) - -// WriteError writes to w a JSON representation of the given error. -func WriteError(w http.ResponseWriter, err error) { - switch k := err.(type) { - case *acme.Error: - acme.WriteError(w, k) - return - case *admin.Error: - admin.WriteError(w, k) - return - case *scep.Error: - w.Header().Set("Content-Type", "text/plain") - default: - w.Header().Set("Content-Type", "application/json") - } - - cause := errors.Cause(err) - if sc, ok := err.(errs.StatusCoder); ok { - w.WriteHeader(sc.StatusCode()) - } else { - if sc, ok := cause.(errs.StatusCoder); ok { - w.WriteHeader(sc.StatusCode()) - } else { - w.WriteHeader(http.StatusInternalServerError) - } - } - - // Write errors in the response writer - if rl, ok := w.(logging.ResponseLogger); ok { - rl.WithFields(map[string]interface{}{ - "error": err, - }) - if os.Getenv("STEPDEBUG") == "1" { - if e, ok := err.(errs.StackTracer); ok { - rl.WithFields(map[string]interface{}{ - "stack-trace": fmt.Sprintf("%+v", e), - }) - } else if e, ok := cause.(errs.StackTracer); ok { - rl.WithFields(map[string]interface{}{ - "stack-trace": fmt.Sprintf("%+v", e), - }) - } - } - } - - if err := json.NewEncoder(w).Encode(err); err != nil { - log.Error(w, err) - } -} diff --git a/api/render/render.go b/api/render/render.go index e4fbaffa..56a68f30 100644 --- a/api/render/render.go +++ b/api/render/render.go @@ -3,12 +3,20 @@ package render import ( "encoding/json" + "fmt" "net/http" + "os" + "github.com/pkg/errors" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/proto" + "github.com/smallstep/certificates/acme" "github.com/smallstep/certificates/api/log" + "github.com/smallstep/certificates/authority/admin" + "github.com/smallstep/certificates/errs" + "github.com/smallstep/certificates/logging" + "github.com/smallstep/certificates/scep" ) // JSON writes the passed value into the http.ResponseWriter. @@ -56,3 +64,52 @@ func ProtoJSONStatus(w http.ResponseWriter, m proto.Message, status int) { // log.EnabledResponse(w, v) } + +// Error encodes the JSON representation of err to w. +func Error(w http.ResponseWriter, err error) { + switch k := err.(type) { + case *acme.Error: + acme.WriteError(w, k) + return + case *admin.Error: + admin.WriteError(w, k) + return + case *scep.Error: + w.Header().Set("Content-Type", "text/plain") + default: + w.Header().Set("Content-Type", "application/json") + } + + cause := errors.Cause(err) + if sc, ok := err.(errs.StatusCoder); ok { + w.WriteHeader(sc.StatusCode()) + } else { + if sc, ok := cause.(errs.StatusCoder); ok { + w.WriteHeader(sc.StatusCode()) + } else { + w.WriteHeader(http.StatusInternalServerError) + } + } + + // Write errors in the response writer + if rl, ok := w.(logging.ResponseLogger); ok { + rl.WithFields(map[string]interface{}{ + "error": err, + }) + if os.Getenv("STEPDEBUG") == "1" { + if e, ok := err.(errs.StackTracer); ok { + rl.WithFields(map[string]interface{}{ + "stack-trace": fmt.Sprintf("%+v", e), + }) + } else if e, ok := cause.(errs.StackTracer); ok { + rl.WithFields(map[string]interface{}{ + "stack-trace": fmt.Sprintf("%+v", e), + }) + } + } + } + + if err := json.NewEncoder(w).Encode(err); err != nil { + log.Error(w, err) + } +}