Skip to content

Commit 1246713

Browse files
committed
Update Error Handling
1 parent f7ba724 commit 1246713

3 files changed

Lines changed: 8 additions & 6 deletions

File tree

api/controller/dict_controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controller
22

33
import (
4+
"errors"
45
"net/http"
56
"shadmin/domain"
67
"strconv"
@@ -114,7 +115,7 @@ func (dc *DictController) CreateDictType(c *gin.Context) {
114115

115116
dictType, err := dc.DictUseCase.CreateDictType(c, &request)
116117
if err != nil {
117-
if err == domain.ErrDictTypeCodeExists {
118+
if errors.Is(err, domain.ErrDictTypeCodeExists) {
118119
c.JSON(http.StatusConflict, domain.RespError("Dictionary type code already exists"))
119120
return
120121
}
@@ -151,11 +152,11 @@ func (dc *DictController) UpdateDictType(c *gin.Context) {
151152

152153
err := dc.DictUseCase.UpdateDictType(c, id, request)
153154
if err != nil {
154-
if err == domain.ErrDictTypeNotFound {
155+
if errors.Is(err, domain.ErrDictTypeNotFound) {
155156
c.JSON(http.StatusNotFound, domain.RespError("Dictionary type not found"))
156157
return
157158
}
158-
if err == domain.ErrDictTypeCodeExists {
159+
if errors.Is(err, domain.ErrDictTypeCodeExists) {
159160
c.JSON(http.StatusConflict, domain.RespError("Dictionary type code already exists"))
160161
return
161162
}

api/controller/menu_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ func (mc *MenuController) CreateMenu(c *gin.Context) {
9292

9393
menu, err := mc.MenuUseCase.CreateMenu(c.Request.Context(), &request)
9494
if err != nil {
95-
if err == domain.ErrInvalidMenuType {
95+
if errors.Is(err, domain.ErrInvalidMenuType) {
9696
c.JSON(http.StatusBadRequest, domain.RespError("Invalid menu type"))
9797
return
9898
}
99-
if err == domain.ErrInvalidMenuStatus {
99+
if errors.Is(err, domain.ErrInvalidMenuStatus) {
100100
c.JSON(http.StatusBadRequest, domain.RespError("Invalid menu status"))
101101
return
102102
}

usecase/dict_usecase.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package usecase
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"shadmin/domain"
78
"shadmin/ent"
@@ -119,7 +120,7 @@ func (du *dictUsecase) CreateDictItem(ctx context.Context, request *domain.Creat
119120
// 验证字典类型是否存在
120121
_, err := du.dictRepository.GetTypeByID(ctx, request.TypeID)
121122
if err != nil {
122-
if err == domain.ErrDictTypeNotFound {
123+
if errors.Is(err, domain.ErrDictTypeNotFound) {
123124
return nil, fmt.Errorf("dictionary type not found")
124125
}
125126
return nil, err

0 commit comments

Comments
 (0)