Skip to content

Commit f35850f

Browse files
authored
Refactor error system (#33610)
1 parent 69de5a6 commit f35850f

File tree

184 files changed

+2090
-2096
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

184 files changed

+2090
-2096
lines changed

routers/api/actions/artifacts.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func ArtifactContexter() func(next http.Handler) http.Handler {
135135
// we should verify the ACTIONS_RUNTIME_TOKEN
136136
authHeader := req.Header.Get("Authorization")
137137
if len(authHeader) == 0 || !strings.HasPrefix(authHeader, "Bearer ") {
138-
ctx.Error(http.StatusUnauthorized, "Bad authorization header")
138+
ctx.HTTPError(http.StatusUnauthorized, "Bad authorization header")
139139
return
140140
}
141141

@@ -147,12 +147,12 @@ func ArtifactContexter() func(next http.Handler) http.Handler {
147147
task, err = actions.GetTaskByID(req.Context(), tID)
148148
if err != nil {
149149
log.Error("Error runner api getting task by ID: %v", err)
150-
ctx.Error(http.StatusInternalServerError, "Error runner api getting task by ID")
150+
ctx.HTTPError(http.StatusInternalServerError, "Error runner api getting task by ID")
151151
return
152152
}
153153
if task.Status != actions.StatusRunning {
154154
log.Error("Error runner api getting task: task is not running")
155-
ctx.Error(http.StatusInternalServerError, "Error runner api getting task: task is not running")
155+
ctx.HTTPError(http.StatusInternalServerError, "Error runner api getting task: task is not running")
156156
return
157157
}
158158
} else {
@@ -162,14 +162,14 @@ func ArtifactContexter() func(next http.Handler) http.Handler {
162162
task, err = actions.GetRunningTaskByToken(req.Context(), authToken)
163163
if err != nil {
164164
log.Error("Error runner api getting task: %v", err)
165-
ctx.Error(http.StatusInternalServerError, "Error runner api getting task")
165+
ctx.HTTPError(http.StatusInternalServerError, "Error runner api getting task")
166166
return
167167
}
168168
}
169169

170170
if err := task.LoadJob(req.Context()); err != nil {
171171
log.Error("Error runner api getting job: %v", err)
172-
ctx.Error(http.StatusInternalServerError, "Error runner api getting job")
172+
ctx.HTTPError(http.StatusInternalServerError, "Error runner api getting job")
173173
return
174174
}
175175

@@ -211,7 +211,7 @@ func (ar artifactRoutes) getUploadArtifactURL(ctx *ArtifactContext) {
211211
var req getUploadArtifactRequest
212212
if err := json.NewDecoder(ctx.Req.Body).Decode(&req); err != nil {
213213
log.Error("Error decode request body: %v", err)
214-
ctx.Error(http.StatusInternalServerError, "Error decode request body")
214+
ctx.HTTPError(http.StatusInternalServerError, "Error decode request body")
215215
return
216216
}
217217

@@ -250,7 +250,7 @@ func (ar artifactRoutes) uploadArtifact(ctx *ArtifactContext) {
250250
expiredDays, err = strconv.ParseInt(queryRetentionDays, 10, 64)
251251
if err != nil {
252252
log.Error("Error parse retention days: %v", err)
253-
ctx.Error(http.StatusBadRequest, "Error parse retention days")
253+
ctx.HTTPError(http.StatusBadRequest, "Error parse retention days")
254254
return
255255
}
256256
}
@@ -261,7 +261,7 @@ func (ar artifactRoutes) uploadArtifact(ctx *ArtifactContext) {
261261
artifact, err := actions.CreateArtifact(ctx, task, artifactName, artifactPath, expiredDays)
262262
if err != nil {
263263
log.Error("Error create or get artifact: %v", err)
264-
ctx.Error(http.StatusInternalServerError, "Error create or get artifact")
264+
ctx.HTTPError(http.StatusInternalServerError, "Error create or get artifact")
265265
return
266266
}
267267

@@ -271,7 +271,7 @@ func (ar artifactRoutes) uploadArtifact(ctx *ArtifactContext) {
271271
chunksTotalSize, err := saveUploadChunk(ar.fs, ctx, artifact, contentLength, runID)
272272
if err != nil {
273273
log.Error("Error save upload chunk: %v", err)
274-
ctx.Error(http.StatusInternalServerError, "Error save upload chunk")
274+
ctx.HTTPError(http.StatusInternalServerError, "Error save upload chunk")
275275
return
276276
}
277277

@@ -285,7 +285,7 @@ func (ar artifactRoutes) uploadArtifact(ctx *ArtifactContext) {
285285
artifact.ContentEncoding = ctx.Req.Header.Get("Content-Encoding")
286286
if err := actions.UpdateArtifactByID(ctx, artifact.ID, artifact); err != nil {
287287
log.Error("Error update artifact: %v", err)
288-
ctx.Error(http.StatusInternalServerError, "Error update artifact")
288+
ctx.HTTPError(http.StatusInternalServerError, "Error update artifact")
289289
return
290290
}
291291
log.Debug("[artifact] update artifact size, artifact_id: %d, size: %d, compressed size: %d",
@@ -307,12 +307,12 @@ func (ar artifactRoutes) comfirmUploadArtifact(ctx *ArtifactContext) {
307307
artifactName := ctx.Req.URL.Query().Get("artifactName")
308308
if artifactName == "" {
309309
log.Error("Error artifact name is empty")
310-
ctx.Error(http.StatusBadRequest, "Error artifact name is empty")
310+
ctx.HTTPError(http.StatusBadRequest, "Error artifact name is empty")
311311
return
312312
}
313313
if err := mergeChunksForRun(ctx, ar.fs, runID, artifactName); err != nil {
314314
log.Error("Error merge chunks: %v", err)
315-
ctx.Error(http.StatusInternalServerError, "Error merge chunks")
315+
ctx.HTTPError(http.StatusInternalServerError, "Error merge chunks")
316316
return
317317
}
318318
ctx.JSON(http.StatusOK, map[string]string{
@@ -340,12 +340,12 @@ func (ar artifactRoutes) listArtifacts(ctx *ArtifactContext) {
340340
artifacts, err := db.Find[actions.ActionArtifact](ctx, actions.FindArtifactsOptions{RunID: runID})
341341
if err != nil {
342342
log.Error("Error getting artifacts: %v", err)
343-
ctx.Error(http.StatusInternalServerError, err.Error())
343+
ctx.HTTPError(http.StatusInternalServerError, err.Error())
344344
return
345345
}
346346
if len(artifacts) == 0 {
347347
log.Debug("[artifact] handleListArtifacts, no artifacts")
348-
ctx.Error(http.StatusNotFound)
348+
ctx.HTTPError(http.StatusNotFound)
349349
return
350350
}
351351

@@ -405,18 +405,18 @@ func (ar artifactRoutes) getDownloadArtifactURL(ctx *ArtifactContext) {
405405
})
406406
if err != nil {
407407
log.Error("Error getting artifacts: %v", err)
408-
ctx.Error(http.StatusInternalServerError, err.Error())
408+
ctx.HTTPError(http.StatusInternalServerError, err.Error())
409409
return
410410
}
411411
if len(artifacts) == 0 {
412412
log.Debug("[artifact] getDownloadArtifactURL, no artifacts")
413-
ctx.Error(http.StatusNotFound)
413+
ctx.HTTPError(http.StatusNotFound)
414414
return
415415
}
416416

417417
if itemPath != artifacts[0].ArtifactName {
418418
log.Error("Error dismatch artifact name, itemPath: %v, artifact: %v", itemPath, artifacts[0].ArtifactName)
419-
ctx.Error(http.StatusBadRequest, "Error dismatch artifact name")
419+
ctx.HTTPError(http.StatusBadRequest, "Error dismatch artifact name")
420420
return
421421
}
422422

@@ -460,24 +460,24 @@ func (ar artifactRoutes) downloadArtifact(ctx *ArtifactContext) {
460460
artifact, exist, err := db.GetByID[actions.ActionArtifact](ctx, artifactID)
461461
if err != nil {
462462
log.Error("Error getting artifact: %v", err)
463-
ctx.Error(http.StatusInternalServerError, err.Error())
463+
ctx.HTTPError(http.StatusInternalServerError, err.Error())
464464
return
465465
}
466466
if !exist {
467467
log.Error("artifact with ID %d does not exist", artifactID)
468-
ctx.Error(http.StatusNotFound, fmt.Sprintf("artifact with ID %d does not exist", artifactID))
468+
ctx.HTTPError(http.StatusNotFound, fmt.Sprintf("artifact with ID %d does not exist", artifactID))
469469
return
470470
}
471471
if artifact.RunID != runID {
472472
log.Error("Error mismatch runID and artifactID, task: %v, artifact: %v", runID, artifactID)
473-
ctx.Error(http.StatusBadRequest)
473+
ctx.HTTPError(http.StatusBadRequest)
474474
return
475475
}
476476

477477
fd, err := ar.fs.Open(artifact.StoragePath)
478478
if err != nil {
479479
log.Error("Error opening file: %v", err)
480-
ctx.Error(http.StatusInternalServerError, err.Error())
480+
ctx.HTTPError(http.StatusInternalServerError, err.Error())
481481
return
482482
}
483483
defer fd.Close()

routers/api/actions/artifacts_utils.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var invalidArtifactNameChars = strings.Join([]string{"\\", "/", "\"", ":", "<",
2626
func validateArtifactName(ctx *ArtifactContext, artifactName string) bool {
2727
if strings.ContainsAny(artifactName, invalidArtifactNameChars) {
2828
log.Error("Error checking artifact name contains invalid character")
29-
ctx.Error(http.StatusBadRequest, "Error checking artifact name contains invalid character")
29+
ctx.HTTPError(http.StatusBadRequest, "Error checking artifact name contains invalid character")
3030
return false
3131
}
3232
return true
@@ -37,7 +37,7 @@ func validateRunID(ctx *ArtifactContext) (*actions.ActionTask, int64, bool) {
3737
runID := ctx.PathParamInt64("run_id")
3838
if task.Job.RunID != runID {
3939
log.Error("Error runID not match")
40-
ctx.Error(http.StatusBadRequest, "run-id does not match")
40+
ctx.HTTPError(http.StatusBadRequest, "run-id does not match")
4141
return nil, 0, false
4242
}
4343
return task, runID, true
@@ -48,7 +48,7 @@ func validateRunIDV4(ctx *ArtifactContext, rawRunID string) (*actions.ActionTask
4848
runID, err := strconv.ParseInt(rawRunID, 10, 64)
4949
if err != nil || task.Job.RunID != runID {
5050
log.Error("Error runID not match")
51-
ctx.Error(http.StatusBadRequest, "run-id does not match")
51+
ctx.HTTPError(http.StatusBadRequest, "run-id does not match")
5252
return nil, 0, false
5353
}
5454
return task, runID, true
@@ -62,7 +62,7 @@ func validateArtifactHash(ctx *ArtifactContext, artifactName string) bool {
6262
return true
6363
}
6464
log.Error("Invalid artifact hash: %s", paramHash)
65-
ctx.Error(http.StatusBadRequest, "Invalid artifact hash")
65+
ctx.HTTPError(http.StatusBadRequest, "Invalid artifact hash")
6666
return false
6767
}
6868

0 commit comments

Comments
 (0)