fix error response for web APIs
This commit is contained in:
@ -102,28 +102,28 @@ func updateService(ctx *gin.Context) {
|
||||
ctx.ShouldBindUri(&req)
|
||||
ctx.ShouldBindJSON(&req.Data)
|
||||
|
||||
service := strings.TrimSpace(req.Service)
|
||||
name := strings.TrimSpace(req.Service)
|
||||
|
||||
old := registry.ServiceRegistry().Get(service)
|
||||
old := registry.ServiceRegistry().Get(name)
|
||||
if old == nil {
|
||||
writeError(ctx, NewError(http.StatusBadRequest, ErrCodeNotFound, fmt.Sprintf("service %s not found", service)))
|
||||
writeError(ctx, NewError(http.StatusBadRequest, ErrCodeNotFound, fmt.Sprintf("service %s not found", name)))
|
||||
return
|
||||
}
|
||||
old.Close()
|
||||
|
||||
req.Data.Name = service
|
||||
req.Data.Name = name
|
||||
|
||||
svc, err := parser.ParseService(&req.Data)
|
||||
if err != nil {
|
||||
writeError(ctx, NewError(http.StatusInternalServerError, ErrCodeFailed, fmt.Sprintf("create service %s failed: %s", service, err.Error())))
|
||||
writeError(ctx, NewError(http.StatusInternalServerError, ErrCodeFailed, fmt.Sprintf("create service %s failed: %s", name, err.Error())))
|
||||
return
|
||||
}
|
||||
|
||||
registry.ServiceRegistry().Unregister(req.Service)
|
||||
registry.ServiceRegistry().Unregister(name)
|
||||
|
||||
if err := registry.ServiceRegistry().Register(req.Service, svc); err != nil {
|
||||
if err := registry.ServiceRegistry().Register(name, svc); err != nil {
|
||||
svc.Close()
|
||||
writeError(ctx, NewError(http.StatusBadRequest, ErrCodeDup, fmt.Sprintf("service %s already exists", service)))
|
||||
writeError(ctx, NewError(http.StatusBadRequest, ErrCodeDup, fmt.Sprintf("service %s already exists", name)))
|
||||
return
|
||||
}
|
||||
|
||||
@ -131,7 +131,7 @@ func updateService(ctx *gin.Context) {
|
||||
|
||||
config.OnUpdate(func(c *config.Config) error {
|
||||
for i := range c.Services {
|
||||
if c.Services[i].Name == req.Service {
|
||||
if c.Services[i].Name == name {
|
||||
c.Services[i] = &req.Data
|
||||
break
|
||||
}
|
||||
@ -171,22 +171,22 @@ func deleteService(ctx *gin.Context) {
|
||||
var req deleteServiceRequest
|
||||
ctx.ShouldBindUri(&req)
|
||||
|
||||
service := strings.TrimSpace(req.Service)
|
||||
name := strings.TrimSpace(req.Service)
|
||||
|
||||
svc := registry.ServiceRegistry().Get(service)
|
||||
svc := registry.ServiceRegistry().Get(name)
|
||||
if svc == nil {
|
||||
writeError(ctx, NewError(http.StatusBadRequest, ErrCodeNotFound, fmt.Sprintf("service %s not found", service)))
|
||||
writeError(ctx, NewError(http.StatusBadRequest, ErrCodeNotFound, fmt.Sprintf("service %s not found", name)))
|
||||
return
|
||||
}
|
||||
|
||||
registry.ServiceRegistry().Unregister(service)
|
||||
registry.ServiceRegistry().Unregister(name)
|
||||
svc.Close()
|
||||
|
||||
config.OnUpdate(func(c *config.Config) error {
|
||||
services := c.Services
|
||||
c.Services = nil
|
||||
for _, s := range services {
|
||||
if s.Name == req.Service {
|
||||
if s.Name == name {
|
||||
continue
|
||||
}
|
||||
c.Services = append(c.Services, s)
|
||||
|
Reference in New Issue
Block a user