Skip to content

Commit 37846ca

Browse files
committed
slightly wrapped error handling for better readability
Signed-off-by: Surax98 <giacomo.surace@gmail.com>
1 parent 680f64c commit 37846ca

File tree

5 files changed

+40
-88
lines changed

5 files changed

+40
-88
lines changed

pkg/slurm/Create.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ func (h *SidecarHandler) SubmitHandler(w http.ResponseWriter, r *http.Request) {
2020
bodyBytes, err := io.ReadAll(r.Body)
2121
if err != nil {
2222
statusCode = http.StatusInternalServerError
23-
w.WriteHeader(statusCode)
24-
w.Write([]byte("Some errors occurred while creating container. Check Slurm Sidecar's logs"))
25-
log.G(h.Ctx).Error(err)
23+
h.handleError(w, statusCode, err)
2624
return
2725
}
2826

@@ -34,9 +32,7 @@ func (h *SidecarHandler) SubmitHandler(w http.ResponseWriter, r *http.Request) {
3432
err = json.Unmarshal(bodyBytes, &data)
3533
if err != nil {
3634
statusCode = http.StatusInternalServerError
37-
w.WriteHeader(statusCode)
38-
w.Write([]byte("Some errors occurred while creating container. Check Slurm Sidecar's logs"))
39-
log.G(h.Ctx).Error(err)
35+
h.handleError(w, statusCode, err)
4036
return
4137
}
4238

@@ -71,9 +67,7 @@ func (h *SidecarHandler) SubmitHandler(w http.ResponseWriter, r *http.Request) {
7167
log.G(h.Ctx).Debug(mounts)
7268
if err != nil {
7369
statusCode = http.StatusInternalServerError
74-
w.WriteHeader(statusCode)
75-
w.Write([]byte("Error prepairing mounts. Check Slurm Sidecar's logs"))
76-
log.G(h.Ctx).Error(err)
70+
h.handleError(w, statusCode, err)
7771
os.RemoveAll(filesPath)
7872
return
7973
}
@@ -102,28 +96,22 @@ func (h *SidecarHandler) SubmitHandler(w http.ResponseWriter, r *http.Request) {
10296
path, err := produceSLURMScript(h.Ctx, h.Config, string(data.Pod.UID), filesPath, metadata, singularity_command_pod)
10397
if err != nil {
10498
statusCode = http.StatusInternalServerError
105-
w.WriteHeader(statusCode)
106-
w.Write([]byte("Error producing Slurm script. Check Slurm Sidecar's logs"))
107-
log.G(h.Ctx).Error(err)
99+
h.handleError(w, statusCode, err)
108100
os.RemoveAll(filesPath)
109101
return
110102
}
111103
out, err := SLURMBatchSubmit(h.Ctx, h.Config, path)
112104
if err != nil {
113105
statusCode = http.StatusInternalServerError
114-
w.WriteHeader(statusCode)
115-
w.Write([]byte("Error submitting Slurm script. Check Slurm Sidecar's logs"))
116-
log.G(h.Ctx).Error(err)
106+
h.handleError(w, statusCode, err)
117107
os.RemoveAll(filesPath)
118108
return
119109
}
120110
log.G(h.Ctx).Info(out)
121111
jid, err := handleJID(h.Ctx, data.Pod, h.JIDs, out, filesPath)
122112
if err != nil {
123113
statusCode = http.StatusInternalServerError
124-
w.WriteHeader(statusCode)
125-
w.Write([]byte("Error handling JID. Check Slurm Sidecar's logs"))
126-
log.G(h.Ctx).Error(err)
114+
h.handleError(w, statusCode, err)
127115
os.RemoveAll(filesPath)
128116
err = deleteContainer(h.Ctx, h.Config, string(data.Pod.UID), h.JIDs, filesPath)
129117
if err != nil {
@@ -137,9 +125,7 @@ func (h *SidecarHandler) SubmitHandler(w http.ResponseWriter, r *http.Request) {
137125
returnedJIDBytes, err = json.Marshal(returnedJID)
138126
if err != nil {
139127
statusCode = http.StatusInternalServerError
140-
w.WriteHeader(statusCode)
141-
w.Write([]byte("Error marshaling JID. Check Slurm Sidecar's logs"))
142-
log.G(h.Ctx).Error(err)
128+
h.handleError(w, statusCode, err)
143129
return
144130
}
145131

pkg/slurm/Delete.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,15 @@ func (h *SidecarHandler) StopHandler(w http.ResponseWriter, r *http.Request) {
1818
bodyBytes, err := io.ReadAll(r.Body)
1919
if err != nil {
2020
statusCode = http.StatusInternalServerError
21-
w.WriteHeader(statusCode)
22-
w.Write([]byte("Some errors occurred while deleting container. Check Slurm Sidecar's logs"))
23-
log.G(h.Ctx).Error(err)
21+
h.handleError(w, statusCode, err)
2422
return
2523
}
2624

2725
var pod *v1.Pod
2826
err = json.Unmarshal(bodyBytes, &pod)
2927
if err != nil {
3028
statusCode = http.StatusInternalServerError
31-
w.WriteHeader(statusCode)
32-
w.Write([]byte("Some errors occurred while deleting container. Check Slurm Sidecar's logs"))
33-
log.G(h.Ctx).Error(err)
29+
h.handleError(w, statusCode, err)
3430
return
3531
}
3632

@@ -39,18 +35,14 @@ func (h *SidecarHandler) StopHandler(w http.ResponseWriter, r *http.Request) {
3935
err = deleteContainer(h.Ctx, h.Config, string(pod.UID), h.JIDs, filesPath+"/"+pod.Namespace)
4036
if err != nil {
4137
statusCode = http.StatusInternalServerError
42-
w.WriteHeader(statusCode)
43-
w.Write([]byte("Error deleting containers. Check Slurm Sidecar's logs"))
44-
log.G(h.Ctx).Error(err)
38+
h.handleError(w, statusCode, err)
4539
return
4640
}
4741
if os.Getenv("SHARED_FS") != "true" {
4842
err = os.RemoveAll(filesPath)
4943
if err != nil {
5044
statusCode = http.StatusInternalServerError
51-
w.WriteHeader(statusCode)
52-
w.Write([]byte("Error deleting containers. Check Slurm Sidecar's logs"))
53-
log.G(h.Ctx).Error(err)
45+
h.handleError(w, statusCode, err)
5446
return
5547
}
5648
}

pkg/slurm/GetLogs.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package slurm
22

33
import (
44
"encoding/json"
5-
"errors"
65
"io"
76
"net/http"
87
"os"
@@ -25,27 +24,21 @@ func (h *SidecarHandler) GetLogsHandler(w http.ResponseWriter, r *http.Request)
2524
bodyBytes, err := io.ReadAll(r.Body)
2625
if err != nil {
2726
statusCode = http.StatusInternalServerError
28-
w.WriteHeader(statusCode)
29-
w.Write([]byte("Some errors occurred while checking log requests raw message. Check Docker Sidecar's logs"))
30-
log.G(h.Ctx).Error(err)
27+
h.handleError(w, statusCode, err)
3128
return
3229
}
3330

3431
err = json.Unmarshal(bodyBytes, &req)
3532
if err != nil {
3633
statusCode = http.StatusInternalServerError
37-
w.WriteHeader(statusCode)
38-
w.Write([]byte("Some errors occurred while unmarshalling log request. Check Docker Sidecar's logs"))
39-
log.G(h.Ctx).Error(err)
34+
h.handleError(w, statusCode, err)
4035
return
4136
}
4237

4338
path := h.Config.DataRootFolder + req.Namespace + "-" + req.PodUID
4439
var output []byte
4540
if req.Opts.Timestamps {
46-
log.G(h.Ctx).Error(errors.New("Not Implemented"))
47-
statusCode = http.StatusInternalServerError
48-
w.WriteHeader(statusCode)
41+
h.handleError(w, statusCode, err)
4942
return
5043
} else {
5144
log.G(h.Ctx).Info("Reading " + path + "/" + req.ContainerName + ".out")
@@ -59,9 +52,7 @@ func (h *SidecarHandler) GetLogsHandler(w http.ResponseWriter, r *http.Request)
5952
}
6053

6154
if err1 != nil && err2 != nil {
62-
log.G(h.Ctx).Error("Failed to retrieve logs.")
63-
statusCode = http.StatusInternalServerError
64-
w.WriteHeader(statusCode)
55+
h.handleError(w, statusCode, err)
6556
return
6657
}
6758

pkg/slurm/Status.go

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package slurm
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io"
78
"net/http"
@@ -30,18 +31,14 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
3031
bodyBytes, err := io.ReadAll(r.Body)
3132
if err != nil {
3233
statusCode = http.StatusInternalServerError
33-
w.WriteHeader(statusCode)
34-
w.Write([]byte("Some errors occurred while retrieving container status. Check Slurm Sidecar's logs"))
35-
log.G(h.Ctx).Error(err)
34+
h.handleError(w, statusCode, err)
3635
return
3736
}
3837

3938
err = json.Unmarshal(bodyBytes, &req)
4039
if err != nil {
4140
statusCode = http.StatusInternalServerError
42-
w.WriteHeader(statusCode)
43-
w.Write([]byte("Some errors occurred while retrieving container status. Check Slurm Sidecar's logs"))
44-
log.G(h.Ctx).Error(err)
41+
h.handleError(w, statusCode, err)
4542
return
4643
}
4744

@@ -57,9 +54,7 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
5754

5855
if execReturn.Stderr != "" {
5956
statusCode = http.StatusInternalServerError
60-
w.WriteHeader(statusCode)
61-
w.Write([]byte("Error executing Squeue. Check Slurm Sidecar's logs"))
62-
log.G(h.Ctx).Error("Unable to retrieve job status: " + execReturn.Stderr)
57+
h.handleError(w, statusCode, errors.New("unable to retrieve job status: "+execReturn.Stderr))
6358
return
6459
}
6560

@@ -87,27 +82,24 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
8782
file, err := os.Open(path + "/" + ct.Name + ".status")
8883
if err != nil {
8984
statusCode = http.StatusInternalServerError
90-
w.WriteHeader(statusCode)
91-
w.Write([]byte("Error retrieving container status. Check Slurm Sidecar's logs"))
92-
log.G(h.Ctx).Error(fmt.Errorf("unable to retrieve container status: %s", err))
85+
h.handleError(w, statusCode, fmt.Errorf("unable to retrieve container status: %s", err))
86+
log.G(h.Ctx).Error()
9387
return
9488
}
9589
defer file.Close()
9690
statusb, err := io.ReadAll(file)
9791
if err != nil {
9892
statusCode = http.StatusInternalServerError
99-
w.WriteHeader(statusCode)
100-
w.Write([]byte("Error reading container status. Check Slurm Sidecar's logs"))
101-
log.G(h.Ctx).Error(fmt.Errorf("unable to read container status: %s", err))
93+
h.handleError(w, statusCode, fmt.Errorf("unable to read container status: %s", err))
94+
log.G(h.Ctx).Error()
10295
return
10396
}
10497

10598
status, err := strconv.Atoi(strings.Replace(string(statusb), "\n", "", -1))
10699
if err != nil {
107100
statusCode = http.StatusInternalServerError
108-
w.WriteHeader(statusCode)
109-
w.Write([]byte("Error converting container status.. Check Slurm Sidecar's logs"))
110-
log.G(h.Ctx).Error(fmt.Errorf("unable to convert container status: %s", err))
101+
h.handleError(w, statusCode, fmt.Errorf("unable to convert container status: %s", err))
102+
log.G(h.Ctx).Error()
111103
status = 500
112104
}
113105

@@ -141,9 +133,7 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
141133
f, err := os.Create(path + "/FinishedAt.time")
142134
if err != nil {
143135
statusCode = http.StatusInternalServerError
144-
w.WriteHeader(statusCode)
145-
w.Write([]byte("Error writing end timestamp... Check Slurm Sidecar's logs"))
146-
log.G(h.Ctx).Error(err)
136+
h.handleError(w, statusCode, err)
147137
return
148138
}
149139
f.WriteString((*h.JIDs)[uid].EndTime.Format("2006-01-02 15:04:05.999999999 -0700 MST"))
@@ -159,9 +149,7 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
159149
f, err := os.Create(path + "/StartedAt.time")
160150
if err != nil {
161151
statusCode = http.StatusInternalServerError
162-
w.WriteHeader(statusCode)
163-
w.Write([]byte("Error writing start timestamp... Check Slurm Sidecar's logs"))
164-
log.G(h.Ctx).Error(err)
152+
h.handleError(w, statusCode, err)
165153
return
166154
}
167155
f.WriteString((*h.JIDs)[uid].StartTime.Format("2006-01-02 15:04:05.999999999 -0700 MST"))
@@ -177,9 +165,7 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
177165
f, err := os.Create(path + "/FinishedAt.time")
178166
if err != nil {
179167
statusCode = http.StatusInternalServerError
180-
w.WriteHeader(statusCode)
181-
w.Write([]byte("Error writing end timestamp... Check Slurm Sidecar's logs"))
182-
log.G(h.Ctx).Error(err)
168+
h.handleError(w, statusCode, err)
183169
return
184170
}
185171
f.WriteString((*h.JIDs)[uid].EndTime.Format("2006-01-02 15:04:05.999999999 -0700 MST"))
@@ -201,9 +187,7 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
201187
f, err := os.Create(path + "/FinishedAt.time")
202188
if err != nil {
203189
statusCode = http.StatusInternalServerError
204-
w.WriteHeader(statusCode)
205-
w.Write([]byte("Error writing end timestamp... Check Slurm Sidecar's logs"))
206-
log.G(h.Ctx).Error(err)
190+
h.handleError(w, statusCode, err)
207191
return
208192
}
209193
f.WriteString((*h.JIDs)[uid].EndTime.Format("2006-01-02 15:04:05.999999999 -0700 MST"))
@@ -219,9 +203,7 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
219203
f, err := os.Create(path + "/StartedAt.time")
220204
if err != nil {
221205
statusCode = http.StatusInternalServerError
222-
w.WriteHeader(statusCode)
223-
w.Write([]byte("Error writing start timestamp... Check Slurm Sidecar's logs"))
224-
log.G(h.Ctx).Error(err)
206+
h.handleError(w, statusCode, err)
225207
return
226208
}
227209
f.WriteString((*h.JIDs)[uid].StartTime.Format("2006-01-02 15:04:05.999999999 -0700 MST"))
@@ -243,9 +225,7 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
243225
f, err := os.Create(path + "/FinishedAt.time")
244226
if err != nil {
245227
statusCode = http.StatusInternalServerError
246-
w.WriteHeader(statusCode)
247-
w.Write([]byte("Error writing end timestamp... Check Slurm Sidecar's logs"))
248-
log.G(h.Ctx).Error(err)
228+
h.handleError(w, statusCode, err)
249229
return
250230
}
251231
f.WriteString((*h.JIDs)[uid].EndTime.Format("2006-01-02 15:04:05.999999999 -0700 MST"))
@@ -261,9 +241,7 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
261241
f, err := os.Create(path + "/FinishedAt.time")
262242
if err != nil {
263243
statusCode = http.StatusInternalServerError
264-
w.WriteHeader(statusCode)
265-
w.Write([]byte("Error writing end timestamp... Check Slurm Sidecar's logs"))
266-
log.G(h.Ctx).Error(err)
244+
h.handleError(w, statusCode, err)
267245
return
268246
}
269247
f.WriteString((*h.JIDs)[uid].EndTime.Format("2006-01-02 15:04:05.999999999 -0700 MST"))
@@ -299,9 +277,7 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
299277
} else {
300278
bodyBytes, err := json.Marshal(resp)
301279
if err != nil {
302-
w.WriteHeader(statusCode)
303-
w.Write([]byte("Some errors occurred while retrieving container status. Check Slurm Sidecar's logs"))
304-
log.G(h.Ctx).Error(err)
280+
h.handleError(w, statusCode, err)
305281
return
306282
}
307283
w.Write(bodyBytes)

pkg/slurm/func.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"flag"
66
"fmt"
7+
"net/http"
78
"os"
89

910
"k8s.io/client-go/kubernetes"
@@ -93,3 +94,9 @@ func NewSlurmConfig() (SlurmConfig, error) {
9394
}
9495
return SlurmConfigInst, nil
9596
}
97+
98+
func (h *SidecarHandler) handleError(w http.ResponseWriter, statusCode int, err error) {
99+
w.WriteHeader(statusCode)
100+
w.Write([]byte("Some errors occurred while creating container. Check Slurm Sidecar's logs"))
101+
log.G(h.Ctx).Error(err)
102+
}

0 commit comments

Comments
 (0)