From 4cf01acd87ad2fbcfe3e064e59caefcddf720e5b Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Tue, 17 Dec 2024 22:45:06 +0500 Subject: [PATCH 1/2] replace where we return with 500 to panic an error instead --- auth/auth.go | 2 +- handlers/features.go | 22 +++++++++++----------- handlers/meme.go | 4 ++-- handlers/workflow.go | 12 ++++++------ routes/index.go | 2 +- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index d4ab372c9..e41c959f6 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -157,7 +157,7 @@ func ConnectionCodeContext(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r == nil { - http.Error(w, http.StatusText(500), http.StatusInternalServerError) + panic(http.StatusText(http.StatusInternalServerError)) return } diff --git a/handlers/features.go b/handlers/features.go index 7ed0c40c6..ba44507fa 100644 --- a/handlers/features.go +++ b/handlers/features.go @@ -562,7 +562,7 @@ func (oh *featureHandler) StoriesSend(w http.ResponseWriter, r *http.Request) { apiKey := os.Getenv("SWWFKEY") if apiKey == "" { - http.Error(w, "API key not set in environment", http.StatusInternalServerError) + panic("API key not set in environment") return } @@ -580,13 +580,13 @@ func (oh *featureHandler) StoriesSend(w http.ResponseWriter, r *http.Request) { stakworkPayloadJSON, err := json.Marshal(stakworkPayload) if err != nil { - http.Error(w, "Failed to encode payload", http.StatusInternalServerError) + panic("Failed to encode payload") return } req, err := http.NewRequest("POST", "https://api.stakwork.com/api/v1/projects", bytes.NewBuffer(stakworkPayloadJSON)) if err != nil { - http.Error(w, "Failed to create request to Stakwork API", http.StatusInternalServerError) + panic("Failed to create request to Stakwork API") return } req.Header.Set("Authorization", "Token token="+apiKey) @@ -595,14 +595,14 @@ func (oh *featureHandler) StoriesSend(w http.ResponseWriter, r *http.Request) { client := &http.Client{} resp, err := client.Do(req) if err != nil { - http.Error(w, "Failed to send request to Stakwork API", http.StatusInternalServerError) + panic("Failed to send request to Stakwork API") return } defer resp.Body.Close() respBody, err := io.ReadAll(resp.Body) if err != nil { - http.Error(w, "Failed to read response from Stakwork API", http.StatusInternalServerError) + panic("Failed to read response from Stakwork API") return } @@ -636,7 +636,7 @@ func (oh *featureHandler) BriefSend(w http.ResponseWriter, r *http.Request) { host := os.Getenv("HOST") if host == "" { - http.Error(w, "HOST environment variable not set", http.StatusInternalServerError) + panic("HOST environment variable not set") return } @@ -650,7 +650,7 @@ func (oh *featureHandler) BriefSend(w http.ResponseWriter, r *http.Request) { apiKey := os.Getenv("SWWFKEY") if apiKey == "" { - http.Error(w, "API key not set in environment", http.StatusInternalServerError) + panic("API key not set in environment") return } @@ -668,13 +668,13 @@ func (oh *featureHandler) BriefSend(w http.ResponseWriter, r *http.Request) { stakworkPayloadJSON, err := json.Marshal(stakworkPayload) if err != nil { - http.Error(w, "Failed to encode payload", http.StatusInternalServerError) + panic("Failed to encode payload") return } req, err := http.NewRequest(http.MethodPost, "https://api.stakwork.com/api/v1/projects", bytes.NewBuffer(stakworkPayloadJSON)) if err != nil { - http.Error(w, "Failed to create request to Stakwork API", http.StatusInternalServerError) + panic("Failed to create request to Stakwork API") return } req.Header.Set("Authorization", "Token token="+apiKey) @@ -683,14 +683,14 @@ func (oh *featureHandler) BriefSend(w http.ResponseWriter, r *http.Request) { client := &http.Client{} resp, err := client.Do(req) if err != nil { - http.Error(w, "Failed to send request to Stakwork API", http.StatusInternalServerError) + panic("Failed to send request to Stakwork API") return } defer resp.Body.Close() respBody, err := io.ReadAll(resp.Body) if err != nil { - http.Error(w, "Failed to read response from Stakwork API", http.StatusInternalServerError) + panic("Failed to read response from Stakwork API") return } diff --git a/handlers/meme.go b/handlers/meme.go index f59a45220..2ae6294c6 100644 --- a/handlers/meme.go +++ b/handlers/meme.go @@ -43,7 +43,7 @@ func MemeImageUpload(w http.ResponseWriter, r *http.Request) { dst, err := os.Create(dirName + "/" + header.Filename) if err != nil { - http.Error(w, "Unable to create file", http.StatusInternalServerError) + panic("Unable to create file") return } @@ -51,7 +51,7 @@ func MemeImageUpload(w http.ResponseWriter, r *http.Request) { _, err = io.Copy(dst, file) if err != nil { - http.Error(w, "Unable to copy saved file", http.StatusInternalServerError) + panic("Unable to copy saved file") return } diff --git a/handlers/workflow.go b/handlers/workflow.go index 009732232..418f38774 100644 --- a/handlers/workflow.go +++ b/handlers/workflow.go @@ -41,7 +41,7 @@ func (wh *workflowHandler) HandleWorkflowRequest(w http.ResponseWriter, r *http. processedRequestID, err := utils.ProcessWorkflowRequest(request.RequestID, request.Source) if err != nil { - http.Error(w, "Failed to process workflow request", http.StatusInternalServerError) + panic("Failed to process workflow request") return } @@ -49,7 +49,7 @@ func (wh *workflowHandler) HandleWorkflowRequest(w http.ResponseWriter, r *http. request.Status = db.StatusNew if err := wh.db.CreateWorkflowRequest(&request); err != nil { - http.Error(w, "Failed to create workflow request", http.StatusInternalServerError) + panic("Failed to create workflow request") return } @@ -84,7 +84,7 @@ func (wh *workflowHandler) HandleWorkflowResponse(w http.ResponseWriter, r *http request, err := wh.db.GetWorkflowRequest(response.RequestID) if err != nil { - http.Error(w, "Failed to retrieve original request", http.StatusInternalServerError) + panic("Failed to retrieve original request") return } if request == nil { @@ -94,7 +94,7 @@ func (wh *workflowHandler) HandleWorkflowResponse(w http.ResponseWriter, r *http processingMap, err := wh.db.GetProcessingMapByKey(request.Source, request.Action) if err != nil { - http.Error(w, "Failed to check processing requirements", http.StatusInternalServerError) + panic("Failed to check processing requirements") return } @@ -109,7 +109,7 @@ func (wh *workflowHandler) HandleWorkflowResponse(w http.ResponseWriter, r *http response.ResponseData, ) if err != nil { - http.Error(w, "Failed to update workflow request", http.StatusInternalServerError) + panic("Failed to update workflow request") return } @@ -119,4 +119,4 @@ func (wh *workflowHandler) HandleWorkflowResponse(w http.ResponseWriter, r *http "status": "success", "request_id": response.RequestID, }) -} \ No newline at end of file +} diff --git a/routes/index.go b/routes/index.go index 58806897d..7e12fc007 100644 --- a/routes/index.go +++ b/routes/index.go @@ -163,7 +163,7 @@ func internalServerErrorHandler(next http.Handler) http.Handler { stackTrace, ) - http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + panic(http.StatusText(http.StatusInternalServerError)) } }() From 04136ddb2e3ec85d486263a1c7b24599a641ba72 Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Tue, 17 Dec 2024 23:11:36 +0500 Subject: [PATCH 2/2] fixed test failing --- auth/auth_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/auth/auth_test.go b/auth/auth_test.go index 062ce5f17..c5b7bfc84 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -935,7 +935,15 @@ func TestConnectionCodeContext(t *testing.T) { w.WriteHeader(http.StatusOK) }) - handler := ConnectionCodeContext(next) + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + defer func() { + if err := recover(); err != nil { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + } + }() + ConnectionCodeContext(next).ServeHTTP(w, r) + }) + rr := httptest.NewRecorder() handler.ServeHTTP(rr, nil)