Skip to content

Commit f0da1de

Browse files
authored
Improve package API log handling (#35100)
Simplify code and fix log processing logic
1 parent 37958e4 commit f0da1de

File tree

23 files changed

+101
-132
lines changed

23 files changed

+101
-132
lines changed

routers/api/packages/alpine/alpine.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ import (
2525
)
2626

2727
func apiError(ctx *context.Context, status int, obj any) {
28-
helper.LogAndProcessError(ctx, status, obj, func(message string) {
29-
ctx.PlainText(status, message)
30-
})
28+
message := helper.ProcessErrorForUser(ctx, status, obj)
29+
ctx.PlainText(status, message)
3130
}
3231

3332
func GetRepositoryKey(ctx *context.Context) {

routers/api/packages/arch/arch.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import (
2424
)
2525

2626
func apiError(ctx *context.Context, status int, obj any) {
27-
helper.LogAndProcessError(ctx, status, obj, func(message string) {
28-
ctx.PlainText(status, message)
29-
})
27+
message := helper.ProcessErrorForUser(ctx, status, obj)
28+
ctx.PlainText(status, message)
3029
}
3130

3231
func GetRepositoryKey(ctx *context.Context) {

routers/api/packages/cargo/cargo.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ type StatusMessage struct {
3737
}
3838

3939
func apiError(ctx *context.Context, status int, obj any) {
40-
helper.LogAndProcessError(ctx, status, obj, func(message string) {
41-
ctx.JSON(status, StatusResponse{
42-
OK: false,
43-
Errors: []StatusMessage{
44-
{
45-
Message: message,
46-
},
40+
message := helper.ProcessErrorForUser(ctx, status, obj)
41+
ctx.JSON(status, StatusResponse{
42+
OK: false,
43+
Errors: []StatusMessage{
44+
{
45+
Message: message,
4746
},
48-
})
47+
},
4948
})
5049
}
5150

routers/api/packages/chef/chef.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ func apiError(ctx *context.Context, status int, obj any) {
3030
ErrorMessages []string `json:"error_messages"`
3131
}
3232

33-
helper.LogAndProcessError(ctx, status, obj, func(message string) {
34-
ctx.JSON(status, Error{
35-
ErrorMessages: []string{message},
36-
})
33+
message := helper.ProcessErrorForUser(ctx, status, obj)
34+
ctx.JSON(status, Error{
35+
ErrorMessages: []string{message},
3736
})
3837
}
3938

routers/api/packages/composer/composer.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,17 @@ import (
2828
)
2929

3030
func apiError(ctx *context.Context, status int, obj any) {
31-
helper.LogAndProcessError(ctx, status, obj, func(message string) {
32-
type Error struct {
33-
Status int `json:"status"`
34-
Message string `json:"message"`
35-
}
36-
ctx.JSON(status, struct {
37-
Errors []Error `json:"errors"`
38-
}{
39-
Errors: []Error{
40-
{Status: status, Message: message},
41-
},
42-
})
31+
message := helper.ProcessErrorForUser(ctx, status, obj)
32+
type Error struct {
33+
Status int `json:"status"`
34+
Message string `json:"message"`
35+
}
36+
ctx.JSON(status, struct {
37+
Errors []Error `json:"errors"`
38+
}{
39+
Errors: []Error{
40+
{Status: status, Message: message},
41+
},
4342
})
4443
}
4544

routers/api/packages/conan/conan.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ func jsonResponse(ctx *context.Context, status int, obj any) {
6161
}
6262

6363
func apiError(ctx *context.Context, status int, obj any) {
64-
helper.LogAndProcessError(ctx, status, obj, func(message string) {
65-
jsonResponse(ctx, status, map[string]string{
66-
"message": message,
67-
})
64+
message := helper.ProcessErrorForUser(ctx, status, obj)
65+
jsonResponse(ctx, status, map[string]string{
66+
"message": message,
6867
})
6968
}
7069

routers/api/packages/conda/conda.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ import (
2525
)
2626

2727
func apiError(ctx *context.Context, status int, obj any) {
28-
helper.LogAndProcessError(ctx, status, obj, func(message string) {
29-
ctx.JSON(status, struct {
30-
Reason string `json:"reason"`
31-
Message string `json:"message"`
32-
}{
33-
Reason: http.StatusText(status),
34-
Message: message,
35-
})
28+
message := helper.ProcessErrorForUser(ctx, status, obj)
29+
ctx.JSON(status, struct {
30+
Reason string `json:"reason"`
31+
Message string `json:"message"`
32+
}{
33+
Reason: http.StatusText(status),
34+
Message: message,
3635
})
3736
}
3837

routers/api/packages/container/container.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ func jsonResponse(ctx *context.Context, status int, obj any) {
9393
}
9494

9595
func apiError(ctx *context.Context, status int, err error) {
96-
helper.LogAndProcessError(ctx, status, err, func(message string) {
97-
setResponseHeaders(ctx.Resp, &containerHeaders{
98-
Status: status,
99-
})
96+
_ = helper.ProcessErrorForUser(ctx, status, err)
97+
setResponseHeaders(ctx.Resp, &containerHeaders{
98+
Status: status,
10099
})
101100
}
102101

routers/api/packages/cran/cran.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ import (
2222
)
2323

2424
func apiError(ctx *context.Context, status int, obj any) {
25-
helper.LogAndProcessError(ctx, status, obj, func(message string) {
26-
ctx.PlainText(status, message)
27-
})
25+
message := helper.ProcessErrorForUser(ctx, status, obj)
26+
ctx.PlainText(status, message)
2827
}
2928

3029
func EnumerateSourcePackages(ctx *context.Context) {

routers/api/packages/debian/debian.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import (
2424
)
2525

2626
func apiError(ctx *context.Context, status int, obj any) {
27-
helper.LogAndProcessError(ctx, status, obj, func(message string) {
28-
ctx.PlainText(status, message)
29-
})
27+
message := helper.ProcessErrorForUser(ctx, status, obj)
28+
ctx.PlainText(status, message)
3029
}
3130

3231
func GetRepositoryKey(ctx *context.Context) {

0 commit comments

Comments
 (0)