Skip to content

Commit be8ff6c

Browse files
committed
refactor(api): 使用错误常量替代字符串比较以提高可维护性
1 parent a935e26 commit be8ff6c

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

internal/service_manager/api/info_api.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/fox-gonic/fox"
77
"github.com/qiniu/zeroops/internal/service_manager/model"
8+
"github.com/qiniu/zeroops/internal/service_manager/service"
89
"github.com/rs/zerolog/log"
910
)
1011

@@ -197,9 +198,9 @@ func (api *Api) GetServiceByName(c *fox.Context) {
197198
return
198199
}
199200

200-
service, err := api.service.GetServiceByName(ctx, serviceName)
201+
svc, err := api.service.GetServiceByName(ctx, serviceName)
201202
if err != nil {
202-
if err.Error() == "service not found" {
203+
if err == service.ErrServiceNotFound {
203204
c.JSON(http.StatusNotFound, map[string]any{
204205
"error": "not found",
205206
"message": "service not found",
@@ -214,7 +215,7 @@ func (api *Api) GetServiceByName(c *fox.Context) {
214215
return
215216
}
216217

217-
c.JSON(http.StatusOK, service)
218+
c.JSON(http.StatusOK, svc)
218219
}
219220

220221
// UpdateService 更新服务信息(PUT /v1/services/:service)
@@ -230,8 +231,8 @@ func (api *Api) UpdateService(c *fox.Context) {
230231
return
231232
}
232233

233-
var service model.Service
234-
if err := c.ShouldBindJSON(&service); err != nil {
234+
var svc model.Service
235+
if err := c.ShouldBindJSON(&svc); err != nil {
235236
c.JSON(http.StatusBadRequest, map[string]any{
236237
"error": "bad request",
237238
"message": "invalid request body: " + err.Error(),
@@ -240,10 +241,10 @@ func (api *Api) UpdateService(c *fox.Context) {
240241
}
241242

242243
// 确保URL参数和请求体中的服务名一致
243-
service.Name = serviceName
244+
svc.Name = serviceName
244245

245-
if err := api.service.UpdateService(ctx, &service); err != nil {
246-
if err.Error() == "service not found" {
246+
if err := api.service.UpdateService(ctx, &svc); err != nil {
247+
if err == service.ErrServiceNotFound {
247248
c.JSON(http.StatusNotFound, map[string]any{
248249
"error": "not found",
249250
"message": "service not found",
@@ -278,7 +279,7 @@ func (api *Api) DeleteService(c *fox.Context) {
278279
}
279280

280281
if err := api.service.DeleteService(ctx, serviceName); err != nil {
281-
if err.Error() == "service not found" {
282+
if err == service.ErrServiceNotFound {
282283
c.JSON(http.StatusNotFound, map[string]any{
283284
"error": "not found",
284285
"message": "service not found",

0 commit comments

Comments
 (0)