Skip to content

Commit be19f60

Browse files
authored
Refactor: Migrate to use the rest package in util/ (#1060)
1 parent fa23b9f commit be19f60

Some content is hidden

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

87 files changed

+866
-687
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ linters:
3232
- exhaustive
3333
- exportloopref
3434
- godot
35-
- gofmt
35+
- gofumpt
3636
- goheader
3737
- goimports
3838
- gosec

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ yarn test
9292
9393
## Additional Guides
9494

95+
### Style Guidelines
96+
97+
This project follows the [Uber's Golang style guide](https://github.com/uber-go/guide/blob/master/style.md). Please refer to [its documentation](https://github.com/uber-go/guide/blob/master/style.md) for details.
98+
9599
### Swagger UI
96100

97101
We use [Swagger] to generate the API server and corresponding clients. Swagger provides a web UI in which you can

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ endif
4646
go build -o bin/tidb-dashboard -ldflags '$(LDFLAGS)' -tags "${BUILD_TAGS}" cmd/tidb-dashboard/main.go
4747

4848
run:
49-
bin/tidb-dashboard --debug --experimental
49+
bin/tidb-dashboard --debug --experimental --feature-version "6.0.0"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/Xeoncross/go-aesctr-with-hmac v0.0.0-20200623134604-12b17a7ff502
1111
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
1212
github.com/antonmedv/expr v1.9.0
13-
github.com/appleboy/gin-jwt/v2 v2.6.3
13+
github.com/breeswish/gin-jwt/v2 v2.6.4-jwt-patch
1414
github.com/cenkalti/backoff/v4 v4.0.2
1515
github.com/fatih/structtag v1.2.0
1616
github.com/gin-contrib/gzip v0.0.1

go.sum

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ github.com/alvaroloes/enumer v1.1.2/go.mod h1:FxrjvuXoDAx9isTJrv4c+T410zFi0DtXIT
2929
github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6/go.mod h1:V8iCPQYkqmusNa815XgQio277wI47sdRh1dUOLdyC6Q=
3030
github.com/antonmedv/expr v1.9.0 h1:j4HI3NHEdgDnN9p6oI6Ndr0G5QryMY0FNxT4ONrFDGU=
3131
github.com/antonmedv/expr v1.9.0/go.mod h1:5qsM3oLGDND7sDmQGDXHkYfkjYMUX14qsgqmHhwGEk8=
32-
github.com/appleboy/gin-jwt/v2 v2.6.3 h1:aK4E3DjihWEBUTjEeRnGkA5nUkmwJPL1CPonMa2usRs=
33-
github.com/appleboy/gin-jwt/v2 v2.6.3/go.mod h1:MfPYA4ogzvOcVkRwAxT7quHOtQmVKDpTwxyUrC2DNw0=
3432
github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4=
3533
github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw=
3634
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
@@ -39,6 +37,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
3937
github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
4038
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
4139
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
40+
github.com/breeswish/gin-jwt/v2 v2.6.4-jwt-patch h1:KLE/YeX+9FNaGVW5MtImRVPhjDpfpgJhvkuYWBmOYbo=
41+
github.com/breeswish/gin-jwt/v2 v2.6.4-jwt-patch/go.mod h1:KjBLriHXe7L6fGceqWzTod8HUB/TP1WWDtfuSYtYXaI=
4242
github.com/cenkalti/backoff/v4 v4.0.2 h1:JIufpQLbh4DkbQoii76ItQIUFzevQSqOLZca4eamEDs=
4343
github.com/cenkalti/backoff/v4 v4.0.2/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg=
4444
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@@ -86,6 +86,7 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
8686
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
8787
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
8888
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
89+
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
8990
github.com/gin-gonic/gin v1.7.4 h1:QmUZXrvJ9qZ3GfWvQ+2wnW/1ePrTEJqPKMYEU3lD/DM=
9091
github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
9192
github.com/go-chi/chi v4.0.2+incompatible h1:maB6vn6FqCxrpz4FqWdh4+lwpyZIQS7YEAUcHlgXVRs=
@@ -116,6 +117,7 @@ github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8c
116117
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
117118
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
118119
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
120+
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
119121
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
120122
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
121123
github.com/go-resty/resty/v2 v2.6.0 h1:joIR5PNLM2EFqqESUjCMGXrWmXNHEU9CEiK813oKYS4=
@@ -324,8 +326,8 @@ github.com/swaggo/swag v1.6.6-0.20200529100950-7c765ddd0476 h1:UjnSXdNPIG+5FJ6xL
324326
github.com/swaggo/swag v1.6.6-0.20200529100950-7c765ddd0476/go.mod h1:xDhTyuFIujYiN3DKWC/H/83xcfHp+UE/IzWWampG7Zc=
325327
github.com/thoas/go-funk v0.8.0 h1:JP9tKSvnpFVclYgDM0Is7FD9M4fhPvqA0s0BsXmzSRQ=
326328
github.com/thoas/go-funk v0.8.0/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
327-
github.com/tidwall/gjson v1.3.5 h1:2oW9FBNu8qt9jy5URgrzsVx/T/KSn3qn/smJQ0crlDQ=
328-
github.com/tidwall/gjson v1.3.5/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
329+
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
330+
github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
329331
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
330332
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
331333
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
@@ -519,7 +521,6 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
519521
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
520522
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
521523
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
522-
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
523524
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
524525
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
525526
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/apiserver/apiserver.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ import (
3030
"github.com/pingcap/tidb-dashboard/pkg/apiserver/user/sso"
3131
"github.com/pingcap/tidb-dashboard/pkg/apiserver/user/sso/ssoauth"
3232
"github.com/pingcap/tidb-dashboard/pkg/tiflash"
33+
"github.com/pingcap/tidb-dashboard/util/rest"
3334

3435
// "github.com/pingcap/tidb-dashboard/pkg/apiserver/__APP_NAME__"
3536
// NOTE: Don't remove above comment line, it is a placeholder for code generator.
36-
3737
"github.com/pingcap/tidb-dashboard/pkg/apiserver/slowquery"
3838
"github.com/pingcap/tidb-dashboard/pkg/apiserver/statement"
3939
"github.com/pingcap/tidb-dashboard/pkg/apiserver/user"
40-
apiutils "github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
4140
"github.com/pingcap/tidb-dashboard/pkg/config"
4241
"github.com/pingcap/tidb-dashboard/pkg/dbstore"
4342
"github.com/pingcap/tidb-dashboard/pkg/httpc"
@@ -54,9 +53,7 @@ func Handler(s *Service) http.Handler {
5453
return s.NewStatusAwareHandler(http.HandlerFunc(s.handler), s.stoppedHandler)
5554
}
5655

57-
var (
58-
once sync.Once
59-
)
56+
var once sync.Once
6057

6158
type Service struct {
6259
app *fx.App
@@ -209,7 +206,7 @@ func newAPIHandlerEngine() (apiHandlerEngine *gin.Engine, endpoint *gin.RouterGr
209206
apiHandlerEngine.Use(gin.Recovery())
210207
apiHandlerEngine.Use(cors.AllowAll())
211208
apiHandlerEngine.Use(gzip.Gzip(gzip.DefaultCompression))
212-
apiHandlerEngine.Use(apiutils.MWHandleErrors())
209+
apiHandlerEngine.Use(rest.ErrorHandlerFn())
213210

214211
endpoint = apiHandlerEngine.Group("/dashboard/api")
215212

pkg/apiserver/clusterinfo/service.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/pingcap/tidb-dashboard/pkg/pd"
2424
"github.com/pingcap/tidb-dashboard/pkg/tidb"
2525
"github.com/pingcap/tidb-dashboard/pkg/utils/topology"
26+
"github.com/pingcap/tidb-dashboard/util/rest"
2627
)
2728

2829
type ServiceParams struct {
@@ -72,7 +73,7 @@ func RegisterRouter(r *gin.RouterGroup, auth *user.AuthService, s *Service) {
7273
// @Summary Hide a TiDB instance
7374
// @Param address path string true "ip:port"
7475
// @Success 200 "delete ok"
75-
// @Failure 401 {object} utils.APIError "Unauthorized failure"
76+
// @Failure 401 {object} rest.ErrorResponse
7677
// @Security JwtAuth
7778
// @Router /topology/tidb/{address} [delete]
7879
func (s *Service) deleteTiDBTopology(c *gin.Context) {
@@ -113,7 +114,7 @@ func (s *Service) deleteTiDBTopology(c *gin.Context) {
113114
// @Success 200 {array} topology.TiDBInfo
114115
// @Router /topology/tidb [get]
115116
// @Security JwtAuth
116-
// @Failure 401 {object} utils.APIError "Unauthorized failure"
117+
// @Failure 401 {object} rest.ErrorResponse
117118
func (s *Service) getTiDBTopology(c *gin.Context) {
118119
instances, err := topology.FetchTiDBTopology(s.lifecycleCtx, s.params.EtcdClient)
119120
if err != nil {
@@ -133,7 +134,7 @@ type StoreTopologyResponse struct {
133134
// @Success 200 {object} StoreTopologyResponse
134135
// @Router /topology/store [get]
135136
// @Security JwtAuth
136-
// @Failure 401 {object} utils.APIError "Unauthorized failure"
137+
// @Failure 401 {object} rest.ErrorResponse
137138
func (s *Service) getStoreTopology(c *gin.Context) {
138139
tikvInstances, tiFlashInstances, err := topology.FetchStoreTopology(s.params.PDClient)
139140
if err != nil {
@@ -151,7 +152,7 @@ func (s *Service) getStoreTopology(c *gin.Context) {
151152
// @Success 200 {object} topology.StoreLocation
152153
// @Router /topology/store_location [get]
153154
// @Security JwtAuth
154-
// @Failure 401 {object} utils.APIError "Unauthorized failure"
155+
// @Failure 401 {object} rest.ErrorResponse
155156
func (s *Service) getStoreLocationTopology(c *gin.Context) {
156157
storeLocation, err := topology.FetchStoreLocation(s.params.PDClient)
157158
if err != nil {
@@ -166,7 +167,7 @@ func (s *Service) getStoreLocationTopology(c *gin.Context) {
166167
// @Success 200 {array} topology.PDInfo
167168
// @Router /topology/pd [get]
168169
// @Security JwtAuth
169-
// @Failure 401 {object} utils.APIError "Unauthorized failure"
170+
// @Failure 401 {object} rest.ErrorResponse
170171
func (s *Service) getPDTopology(c *gin.Context) {
171172
instances, err := topology.FetchPDTopology(s.params.PDClient)
172173
if err != nil {
@@ -181,7 +182,7 @@ func (s *Service) getPDTopology(c *gin.Context) {
181182
// @Success 200 {object} topology.AlertManagerInfo
182183
// @Router /topology/alertmanager [get]
183184
// @Security JwtAuth
184-
// @Failure 401 {object} utils.APIError "Unauthorized failure"
185+
// @Failure 401 {object} rest.ErrorResponse
185186
func (s *Service) getAlertManagerTopology(c *gin.Context) {
186187
instance, err := topology.FetchAlertManagerTopology(s.lifecycleCtx, s.params.EtcdClient)
187188
if err != nil {
@@ -196,7 +197,7 @@ func (s *Service) getAlertManagerTopology(c *gin.Context) {
196197
// @Success 200 {object} topology.GrafanaInfo
197198
// @Router /topology/grafana [get]
198199
// @Security JwtAuth
199-
// @Failure 401 {object} utils.APIError "Unauthorized failure"
200+
// @Failure 401 {object} rest.ErrorResponse
200201
func (s *Service) getGrafanaTopology(c *gin.Context) {
201202
instance, err := topology.FetchGrafanaTopology(s.lifecycleCtx, s.params.EtcdClient)
202203
if err != nil {
@@ -212,7 +213,7 @@ func (s *Service) getGrafanaTopology(c *gin.Context) {
212213
// @Param address path string true "ip:port"
213214
// @Router /topology/alertmanager/{address}/count [get]
214215
// @Security JwtAuth
215-
// @Failure 401 {object} utils.APIError "Unauthorized failure"
216+
// @Failure 401 {object} rest.ErrorResponse
216217
func (s *Service) getAlertManagerCounts(c *gin.Context) {
217218
address := c.Param("address")
218219
cnt, err := fetchAlertManagerCounts(s.lifecycleCtx, address, s.params.HTTPClient)
@@ -224,16 +225,16 @@ func (s *Service) getAlertManagerCounts(c *gin.Context) {
224225
}
225226

226227
type GetHostsInfoResponse struct {
227-
Hosts []*hostinfo.Info `json:"hosts"`
228-
Warning *utils.APIError `json:"warning"`
228+
Hosts []*hostinfo.Info `json:"hosts"`
229+
Warning rest.ErrorResponse `json:"warning"`
229230
}
230231

231232
// @ID clusterInfoGetHostsInfo
232233
// @Summary Get information of all hosts
233234
// @Router /host/all [get]
234235
// @Security JwtAuth
235236
// @Success 200 {object} GetHostsInfoResponse
236-
// @Failure 401 {object} utils.APIError "Unauthorized failure"
237+
// @Failure 401 {object} rest.ErrorResponse
237238
func (s *Service) getHostsInfo(c *gin.Context) {
238239
db := utils.GetTiDBConnection(c)
239240

@@ -243,9 +244,9 @@ func (s *Service) getHostsInfo(c *gin.Context) {
243244
return
244245
}
245246

246-
var warning *utils.APIError
247+
var warning rest.ErrorResponse
247248
if err != nil {
248-
warning = utils.NewAPIError(err)
249+
warning = rest.NewErrorResponse(err)
249250
}
250251

251252
c.JSON(http.StatusOK, GetHostsInfoResponse{
@@ -259,7 +260,7 @@ func (s *Service) getHostsInfo(c *gin.Context) {
259260
// @Router /host/statistics [get]
260261
// @Security JwtAuth
261262
// @Success 200 {object} ClusterStatistics
262-
// @Failure 401 {object} utils.APIError "Unauthorized failure"
263+
// @Failure 401 {object} rest.ErrorResponse
263264
func (s *Service) getStatistics(c *gin.Context) {
264265
db := utils.GetTiDBConnection(c)
265266
stats, err := s.calculateStatistics(db)

pkg/apiserver/configuration/router.go

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

1010
"github.com/pingcap/tidb-dashboard/pkg/apiserver/user"
1111
"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
12+
"github.com/pingcap/tidb-dashboard/util/rest"
1213
)
1314

1415
func RegisterRouter(r *gin.RouterGroup, auth *user.AuthService, s *Service) {
@@ -25,9 +26,9 @@ func RegisterRouter(r *gin.RouterGroup, auth *user.AuthService, s *Service) {
2526
// @Success 200 {object} AllConfigItems
2627
// @Router /configuration/all [get]
2728
// @Security JwtAuth
28-
// @Failure 401 {object} utils.APIError "Unauthorized failure"
29-
// @Failure 403 {object} utils.APIError "Experimental feature not enabled"
30-
// @Failure 500 {object} utils.APIError "Internal error"
29+
// @Failure 401 {object} rest.ErrorResponse
30+
// @Failure 403 {object} rest.ErrorResponse
31+
// @Failure 500 {object} rest.ErrorResponse
3132
func (s *Service) getHandler(c *gin.Context) {
3233
db := utils.GetTiDBConnection(c)
3334
r, err := s.getAllConfigItems(db)
@@ -45,7 +46,7 @@ type EditRequest struct {
4546
}
4647

4748
type EditResponse struct {
48-
Warnings []*utils.APIError `json:"warnings"`
49+
Warnings []rest.ErrorResponse `json:"warnings"`
4950
}
5051

5152
// @ID configurationEdit
@@ -54,14 +55,14 @@ type EditResponse struct {
5455
// @Success 200 {object} EditResponse
5556
// @Router /configuration/edit [post]
5657
// @Security JwtAuth
57-
// @Failure 400 {object} utils.APIError "Bad request"
58-
// @Failure 401 {object} utils.APIError "Unauthorized failure"
59-
// @Failure 403 {object} utils.APIError "Experimental feature not enabled"
60-
// @Failure 500 {object} utils.APIError "Internal error"
58+
// @Failure 400 {object} rest.ErrorResponse
59+
// @Failure 401 {object} rest.ErrorResponse
60+
// @Failure 403 {object} rest.ErrorResponse
61+
// @Failure 500 {object} rest.ErrorResponse
6162
func (s *Service) editHandler(c *gin.Context) {
6263
var req EditRequest
6364
if err := c.ShouldBindJSON(&req); err != nil {
64-
utils.MakeInvalidRequestErrorFromError(c, err)
65+
_ = c.Error(rest.ErrBadRequest.NewWithNoMessage())
6566
return
6667
}
6768

pkg/apiserver/configuration/service.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import (
1414
"go.uber.org/fx"
1515
"gorm.io/gorm"
1616

17-
"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
1817
"github.com/pingcap/tidb-dashboard/pkg/config"
1918
"github.com/pingcap/tidb-dashboard/pkg/pd"
2019
"github.com/pingcap/tidb-dashboard/pkg/tidb"
2120
"github.com/pingcap/tidb-dashboard/pkg/tikv"
2221
"github.com/pingcap/tidb-dashboard/pkg/utils/distro"
2322
"github.com/pingcap/tidb-dashboard/pkg/utils/topology"
23+
"github.com/pingcap/tidb-dashboard/util/rest"
2424
)
2525

2626
var (
@@ -190,8 +190,8 @@ type Item struct {
190190
}
191191

192192
type AllConfigItems struct {
193-
Errors []*utils.APIError `json:"errors"`
194-
Items map[ItemKind][]Item `json:"items"`
193+
Errors []rest.ErrorResponse `json:"errors"`
194+
Items map[ItemKind][]Item `json:"items"`
195195
}
196196

197197
func (s *Service) getAllConfigItems(db *gorm.DB) (*AllConfigItems, error) {
@@ -228,13 +228,13 @@ func (s *Service) getAllConfigItems(db *gorm.DB) (*AllConfigItems, error) {
228228
go s.getConfigItemsFromTiDBToChannel(&item2, ch)
229229
}
230230

231-
errors := make([]*utils.APIError, 0)
231+
errors := make([]rest.ErrorResponse, 0)
232232
successItems := make([]channelItem, 0)
233233

234234
for i := 0; i < waitItems; i++ {
235235
item := <-ch
236236
if item.Err != nil {
237-
errors = append(errors, utils.NewAPIError(err))
237+
errors = append(errors, rest.NewErrorResponse(err))
238238
continue
239239
}
240240
successItems = append(successItems, item)
@@ -308,7 +308,7 @@ func (s *Service) getAllConfigItems(db *gorm.DB) (*AllConfigItems, error) {
308308
}, nil
309309
}
310310

311-
func (s *Service) editConfig(db *gorm.DB, kind ItemKind, id string, newValue interface{}) ([]*utils.APIError, error) {
311+
func (s *Service) editConfig(db *gorm.DB, kind ItemKind, id string, newValue interface{}) ([]rest.ErrorResponse, error) {
312312
if !isConfigItemEditable(kind, id) {
313313
return nil, ErrNotEditable.New("Configuration `%s` is not editable", id)
314314
}
@@ -344,9 +344,9 @@ func (s *Service) editConfig(db *gorm.DB, kind ItemKind, id string, newValue int
344344
}
345345
return nil, nil
346346
}
347-
warnings := make([]*utils.APIError, 0)
347+
warnings := make([]rest.ErrorResponse, 0)
348348
for _, err := range failures {
349-
warnings = append(warnings, utils.NewAPIError(err))
349+
warnings = append(warnings, rest.NewErrorResponse(err))
350350
}
351351
return warnings, nil
352352
case ItemKindTiDBVariable:

0 commit comments

Comments
 (0)