From e2e50cbf4d3824a71add24773cbe2ab4e366fb51 Mon Sep 17 00:00:00 2001 From: grindlemire Date: Fri, 29 Dec 2023 22:37:04 -0700 Subject: [PATCH 1/2] better error handling --- redoc.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/redoc.go b/redoc.go index f70de33..0b73941 100644 --- a/redoc.go +++ b/redoc.go @@ -4,6 +4,7 @@ import ( "bytes" "embed" "errors" + "fmt" "io/ioutil" "net/http" "strings" @@ -92,15 +93,21 @@ func (r Redoc) Handler() http.HandlerFunc { header := w.Header() if strings.HasSuffix(req.URL.Path, r.SpecPath) { header.Set("Content-Type", "application/json") - _, _ = w.Write(spec) - w.WriteHeader(200) + _, err = w.Write(spec) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(fmt.Sprintf("error writing openapi spec: %s", err))) + } return } if docsPath == "" || docsPath == req.URL.Path { header.Set("Content-Type", "text/html") - _, _ = w.Write(data) - w.WriteHeader(200) + _, err = w.Write(data) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(fmt.Sprintf("error writing redoc: %s", err))) + } } } } From 100010ea3632ae9c66f689a93aafbc95a13bbf21 Mon Sep 17 00:00:00 2001 From: grindlemire Date: Fri, 29 Dec 2023 22:52:07 -0700 Subject: [PATCH 2/2] don't change behavior of write --- redoc.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/redoc.go b/redoc.go index 0b73941..dade337 100644 --- a/redoc.go +++ b/redoc.go @@ -4,7 +4,6 @@ import ( "bytes" "embed" "errors" - "fmt" "io/ioutil" "net/http" "strings" @@ -93,21 +92,15 @@ func (r Redoc) Handler() http.HandlerFunc { header := w.Header() if strings.HasSuffix(req.URL.Path, r.SpecPath) { header.Set("Content-Type", "application/json") - _, err = w.Write(spec) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf("error writing openapi spec: %s", err))) - } + w.WriteHeader(http.StatusOK) + w.Write(spec) return } if docsPath == "" || docsPath == req.URL.Path { header.Set("Content-Type", "text/html") - _, err = w.Write(data) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf("error writing redoc: %s", err))) - } + w.WriteHeader(http.StatusOK) + w.Write(data) } } }