Skip to content

Commit

Permalink
Replace where we return with 500 to panic an error instead (#2216)
Browse files Browse the repository at this point in the history
* replace where we return with 500 to panic an error instead

* fixed test failing
  • Loading branch information
MahtabBukhari authored Dec 18, 2024
1 parent a0ea3bb commit 5385019
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 9 additions & 1 deletion auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
22 changes: 11 additions & 11 deletions handlers/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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)
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions handlers/meme.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ 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
}

defer dst.Close()

_, 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
}

Expand Down
12 changes: 6 additions & 6 deletions handlers/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ 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
}

request.RequestID = processedRequestID
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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -119,4 +119,4 @@ func (wh *workflowHandler) HandleWorkflowResponse(w http.ResponseWriter, r *http
"status": "success",
"request_id": response.RequestID,
})
}
}
2 changes: 1 addition & 1 deletion routes/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}()

Expand Down

0 comments on commit 5385019

Please sign in to comment.