Skip to content

Commit 1955876

Browse files
fix: return a HTTP error status code when the answer is invalid (#124)
Signed-off-by: Harikrishnan Balagopal <[email protected]>
1 parent 5a9aefc commit 1955876

File tree

5 files changed

+27
-24
lines changed

5 files changed

+27
-24
lines changed

internal/filesystem/filesystem.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"encoding/json"
2424
"fmt"
2525
"io"
26-
"io/ioutil"
2726
"net"
2827
"net/http"
2928
"os"
@@ -1451,7 +1450,7 @@ func (fs *FileSystem) updatePlan(t *bolt.Tx, workspaceId, projectId string, plan
14511450
if project.Status[types.ProjectStatusPlanning] {
14521451
return types.ErrorOngoing{Id: projectId}
14531452
}
1454-
planBytes, err := ioutil.ReadAll(plan)
1453+
planBytes, err := io.ReadAll(plan)
14551454
if err != nil {
14561455
return fmt.Errorf("failed to read the plan. Error: %q", err)
14571456
}
@@ -1467,7 +1466,7 @@ func (fs *FileSystem) updatePlan(t *bolt.Tx, workspaceId, projectId string, plan
14671466
}
14681467
// effects
14691468
planFilePath := filepath.Join(common.Config.DataDir, PROJECTS_DIR, projectId, INPUTS_DIR, EXPANDED_DIR, M2K_PLAN_FILENAME)
1470-
if err := ioutil.WriteFile(planFilePath, planBytes, DEFAULT_FILE_PERMISSIONS); err != nil {
1469+
if err := os.WriteFile(planFilePath, planBytes, DEFAULT_FILE_PERMISSIONS); err != nil {
14711470
return fmt.Errorf("failed to write to the plan file at path %s . Error: %q", planFilePath, err)
14721471
}
14731472
return nil
@@ -1664,7 +1663,7 @@ func (fs *FileSystem) startTransformation(t *bolt.Tx, workspaceId, projectId str
16641663
var planBytes []byte
16651664
if plan == nil {
16661665
srcPlanPath := filepath.Join(projInputsDir, EXPANDED_DIR, M2K_PLAN_FILENAME)
1667-
planBytes, err = ioutil.ReadFile(srcPlanPath)
1666+
planBytes, err = os.ReadFile(srcPlanPath)
16681667
if err != nil {
16691668
err := fmt.Errorf("failed to read the plan file at path %s . Error: %q", srcPlanPath, err)
16701669
logrus.Error(err)
@@ -1674,7 +1673,7 @@ func (fs *FileSystem) startTransformation(t *bolt.Tx, workspaceId, projectId str
16741673
return err
16751674
}
16761675
} else {
1677-
planBytes, err = ioutil.ReadAll(plan)
1676+
planBytes, err = io.ReadAll(plan)
16781677
if err != nil {
16791678
return fmt.Errorf("failed to read the plan. Error: %q", err)
16801679
}
@@ -1713,7 +1712,7 @@ func (fs *FileSystem) startTransformation(t *bolt.Tx, workspaceId, projectId str
17131712
return fmt.Errorf("failed to make the project output directory at path %s . Error: %q", currentRunDir, err)
17141713
}
17151714
planPath := filepath.Join(currentRunDir, M2K_PLAN_FILENAME)
1716-
if err := ioutil.WriteFile(planPath, []byte(planStr), DEFAULT_FILE_PERMISSIONS); err != nil {
1715+
if err := os.WriteFile(planPath, []byte(planStr), DEFAULT_FILE_PERMISSIONS); err != nil {
17171716
return fmt.Errorf("failed to write the plan to a file at path %s . Error: %q", planPath, err)
17181717
}
17191718
// default is empty string, if the input source is given, the value is updated
@@ -2007,7 +2006,7 @@ func (fs *FileSystem) getQuestion(t *bolt.Tx, workspaceId, projectId, projOutput
20072006
return "", fmt.Errorf("got an error response status code. Status: %s", resp.Status)
20082007
}
20092008
defer resp.Body.Close()
2010-
respBodyBytes, err := ioutil.ReadAll(resp.Body)
2009+
respBodyBytes, err := io.ReadAll(resp.Body)
20112010
if err != nil {
20122011
return "", fmt.Errorf("failed to read the response body. Error: %q", err)
20132012
}
@@ -2068,12 +2067,16 @@ func (fs *FileSystem) postSolution(t *bolt.Tx, workspaceId, projectId, projOutpu
20682067
}
20692068
if resp.StatusCode < 200 || resp.StatusCode > 299 {
20702069
if resp.StatusCode == 406 {
2071-
return types.ErrorValidation{Reason: "not a valid answer to the question"}
2070+
respBodyBytes, err := io.ReadAll(resp.Body)
2071+
if err != nil {
2072+
return types.ErrorValidation{Reason: "not a valid answer to the question"}
2073+
}
2074+
return types.ErrorValidation{Reason: string(respBodyBytes)}
20722075
}
20732076
return fmt.Errorf("got an error response status code. Status: %s", resp.Status)
20742077
}
20752078
defer resp.Body.Close()
2076-
respBodyBytes, err := ioutil.ReadAll(resp.Body)
2079+
respBodyBytes, err := io.ReadAll(resp.Body)
20772080
if err != nil {
20782081
return fmt.Errorf("failed to read the response body. Error: %q", err)
20792082
}
@@ -2526,28 +2529,28 @@ func copyOverPlanConfigAndQACache(srcDir, destDir string) error {
25262529
if err != nil {
25272530
return fmt.Errorf("failed to read the plan file at path %s . Error: %q", planSrcPath, err)
25282531
}
2529-
if err := ioutil.WriteFile(planDestPath, planBytes, DEFAULT_FILE_PERMISSIONS); err != nil {
2532+
if err := os.WriteFile(planDestPath, planBytes, DEFAULT_FILE_PERMISSIONS); err != nil {
25302533
return fmt.Errorf("failed to write the plan file to the path %s . Error: %q", planDestPath, err)
25312534
}
25322535
configBytes, err := os.ReadFile(configSrcPath)
25332536
if err != nil {
25342537
return fmt.Errorf("failed to read the config file at path %s . Error: %q", configSrcPath, err)
25352538
}
2536-
if err := ioutil.WriteFile(configDestPath, configBytes, DEFAULT_FILE_PERMISSIONS); err != nil {
2539+
if err := os.WriteFile(configDestPath, configBytes, DEFAULT_FILE_PERMISSIONS); err != nil {
25372540
return fmt.Errorf("failed to write the config file to the path %s . Error: %q", configDestPath, err)
25382541
}
25392542
graphBytes, err := os.ReadFile(graphSrcPath)
25402543
if err != nil {
25412544
return fmt.Errorf("failed to read the m2k graph file at path %s . Error: %q", graphSrcPath, err)
25422545
}
2543-
if err := ioutil.WriteFile(graphDestPath, graphBytes, DEFAULT_FILE_PERMISSIONS); err != nil {
2546+
if err := os.WriteFile(graphDestPath, graphBytes, DEFAULT_FILE_PERMISSIONS); err != nil {
25442547
return fmt.Errorf("failed to write the m2k graph file to the path %s . Error: %q", graphDestPath, err)
25452548
}
25462549
qaCacheBytes, err := os.ReadFile(qaCacheSrcPath)
25472550
if err != nil {
25482551
return fmt.Errorf("failed to read the qa cache file at path %s . Error: %q", qaCacheSrcPath, err)
25492552
}
2550-
if err := ioutil.WriteFile(qaCacheDestPath, qaCacheBytes, DEFAULT_FILE_PERMISSIONS); err != nil {
2553+
if err := os.WriteFile(qaCacheDestPath, qaCacheBytes, DEFAULT_FILE_PERMISSIONS); err != nil {
25512554
return fmt.Errorf("failed to write the qa cache file to the path %s . Error: %q", qaCacheDestPath, err)
25522555
}
25532556
return nil
@@ -2565,7 +2568,7 @@ func generateVerboseLogs(message string) {
25652568
syncLoggingLevel(loggingLevel, message)
25662569
}
25672570

2568-
//syncLoggingLevel matches log levels of Move2Kube-api and Move2Kube
2571+
// syncLoggingLevel matches log levels of Move2Kube-api and Move2Kube
25692572
func syncLoggingLevel(loggingLevel, message string) {
25702573
switch {
25712574
case loggingLevel == "trace":
@@ -2594,7 +2597,7 @@ func putM2KIgnore(path string) error {
25942597
return fmt.Errorf("failed to create a directory at the path %s . Error: %q", path, err)
25952598
}
25962599
m2kIgnorePath := filepath.Join(path, ".m2kignore")
2597-
if err := ioutil.WriteFile(m2kIgnorePath, []byte("."), DEFAULT_FILE_PERMISSIONS); err != nil {
2600+
if err := os.WriteFile(m2kIgnorePath, []byte("."), DEFAULT_FILE_PERMISSIONS); err != nil {
25982601
return fmt.Errorf("failed to write a .m2kingore file to the path %s . Error: %q", m2kIgnorePath, err)
25992602
}
26002603
return nil

internal/move2kubeapi/handlers/plan.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package handlers
1919
import (
2020
"bytes"
2121
"encoding/json"
22-
"io/ioutil"
22+
"io"
2323
"net/http"
2424

2525
"github.com/gorilla/mux"
@@ -98,7 +98,7 @@ func HandleReadPlan(w http.ResponseWriter, r *http.Request) {
9898
w.WriteHeader(http.StatusInternalServerError)
9999
return
100100
}
101-
planBytes, err := ioutil.ReadAll(plan)
101+
planBytes, err := io.ReadAll(plan)
102102
if err != nil {
103103
logrus.Errorf("failed to read the plan file. Error: %q", err)
104104
w.WriteHeader(http.StatusInternalServerError)

internal/move2kubeapi/handlers/qa.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func HandlePostSolution(w http.ResponseWriter, r *http.Request) {
8888
w.WriteHeader(http.StatusNotFound)
8989
return
9090
}
91-
if _, ok := err.(types.ErrorValidation); ok {
92-
sendErrorJSON(w, "the solution is invalid", http.StatusBadRequest)
91+
if e, ok := err.(types.ErrorValidation); ok {
92+
sendErrorJSON(w, e.Reason, http.StatusBadRequest)
9393
return
9494
}
9595
w.WriteHeader(http.StatusInternalServerError)

internal/move2kubeapi/handlers/role-bindings.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23-
"io/ioutil"
23+
"io"
2424
"net/http"
2525

2626
"github.com/Nerzal/gocloak/v10"
@@ -95,7 +95,7 @@ func HandlePatchRoleBindings(w http.ResponseWriter, r *http.Request) {
9595
return
9696
}
9797
defer r.Body.Close()
98-
bodyBytes, err := ioutil.ReadAll(r.Body)
98+
bodyBytes, err := io.ReadAll(r.Body)
9999
if err != nil {
100100
logrus.Debugf("failed to read the request body. Error: %q", err)
101101
sendErrorJSON(w, "the request body is missing or incomplete", http.StatusBadRequest)

internal/move2kubeapi/handlers/roles.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package handlers
1919
import (
2020
"context"
2121
"encoding/json"
22-
"io/ioutil"
22+
"io"
2323
"net/http"
2424
"path"
2525

@@ -81,7 +81,7 @@ func HandleCreateRole(w http.ResponseWriter, r *http.Request) {
8181
return
8282
}
8383
defer r.Body.Close()
84-
bodyBytes, err := ioutil.ReadAll(r.Body)
84+
bodyBytes, err := io.ReadAll(r.Body)
8585
if err != nil {
8686
logrus.Debugf("failed to read the request body. Error: %q", err)
8787
sendErrorJSON(w, "the request body is missing or incomplete", http.StatusBadRequest)
@@ -176,7 +176,7 @@ func HandleUpdateRole(w http.ResponseWriter, r *http.Request) {
176176
return
177177
}
178178
defer r.Body.Close()
179-
bodyBytes, err := ioutil.ReadAll(r.Body)
179+
bodyBytes, err := io.ReadAll(r.Body)
180180
if err != nil {
181181
logrus.Debugf("failed to read the request body. Error: %q", err)
182182
sendErrorJSON(w, "the request body is missing or incomplete", http.StatusBadRequest)

0 commit comments

Comments
 (0)