Improve API error handling.

This commit is contained in:
Yang Luo
2021-03-28 00:48:34 +08:00
parent 6c2c5be33d
commit d6715c7601
18 changed files with 54 additions and 69 deletions

View File

@ -43,7 +43,7 @@ func (c *ApiController) UpdateApplication() {
panic(err)
}
c.Data["json"] = object.UpdateApplication(id, &application)
c.Data["json"] = wrapActionResponse(object.UpdateApplication(id, &application))
c.ServeJSON()
}
@ -54,7 +54,7 @@ func (c *ApiController) AddApplication() {
panic(err)
}
c.Data["json"] = object.AddApplication(&application)
c.Data["json"] = wrapActionResponse(object.AddApplication(&application))
c.ServeJSON()
}
@ -65,6 +65,6 @@ func (c *ApiController) DeleteApplication() {
panic(err)
}
c.Data["json"] = object.DeleteApplication(&application)
c.Data["json"] = wrapActionResponse(object.DeleteApplication(&application))
c.ServeJSON()
}

View File

@ -32,3 +32,11 @@ func (c *ApiController) GetSessionUser() string {
func (c *ApiController) SetSessionUser(user string) {
c.SetSession("username", user)
}
func wrapActionResponse(affected bool) *Response {
if affected {
return &Response{Status: "ok", Msg: "", Data: "affected"}
} else {
return &Response{Status: "ok", Msg: "", Data: "unaffected"}
}
}

View File

@ -43,7 +43,7 @@ func (c *ApiController) UpdateOrganization() {
panic(err)
}
c.Data["json"] = object.UpdateOrganization(id, &organization)
c.Data["json"] = wrapActionResponse(object.UpdateOrganization(id, &organization))
c.ServeJSON()
}
@ -54,7 +54,7 @@ func (c *ApiController) AddOrganization() {
panic(err)
}
c.Data["json"] = object.AddOrganization(&organization)
c.Data["json"] = wrapActionResponse(object.AddOrganization(&organization))
c.ServeJSON()
}
@ -65,6 +65,6 @@ func (c *ApiController) DeleteOrganization() {
panic(err)
}
c.Data["json"] = object.DeleteOrganization(&organization)
c.Data["json"] = wrapActionResponse(object.DeleteOrganization(&organization))
c.ServeJSON()
}

View File

@ -43,7 +43,7 @@ func (c *ApiController) UpdateProvider() {
panic(err)
}
c.Data["json"] = object.UpdateProvider(id, &provider)
c.Data["json"] = wrapActionResponse(object.UpdateProvider(id, &provider))
c.ServeJSON()
}
@ -54,7 +54,7 @@ func (c *ApiController) AddProvider() {
panic(err)
}
c.Data["json"] = object.AddProvider(&provider)
c.Data["json"] = wrapActionResponse(object.AddProvider(&provider))
c.ServeJSON()
}
@ -65,6 +65,6 @@ func (c *ApiController) DeleteProvider() {
panic(err)
}
c.Data["json"] = object.DeleteProvider(&provider)
c.Data["json"] = wrapActionResponse(object.DeleteProvider(&provider))
c.ServeJSON()
}

View File

@ -43,7 +43,7 @@ func (c *ApiController) UpdateToken() {
panic(err)
}
c.Data["json"] = object.UpdateToken(id, &token)
c.Data["json"] = wrapActionResponse(object.UpdateToken(id, &token))
c.ServeJSON()
}
@ -54,7 +54,7 @@ func (c *ApiController) AddToken() {
panic(err)
}
c.Data["json"] = object.AddToken(&token)
c.Data["json"] = wrapActionResponse(object.AddToken(&token))
c.ServeJSON()
}
@ -65,7 +65,7 @@ func (c *ApiController) DeleteToken() {
panic(err)
}
c.Data["json"] = object.DeleteToken(&token)
c.Data["json"] = wrapActionResponse(object.DeleteToken(&token))
c.ServeJSON()
}

View File

@ -1,21 +0,0 @@
// Copyright 2021 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
type authResponse struct {
Email string `json:"email"`
Avatar string `json:"avatar"`
Method string `json:"method"`
}

View File

@ -48,7 +48,7 @@ func (c *ApiController) UpdateUser() {
panic(err)
}
c.Data["json"] = object.UpdateUser(id, &user)
c.Data["json"] = wrapActionResponse(object.UpdateUser(id, &user))
c.ServeJSON()
}
@ -59,7 +59,7 @@ func (c *ApiController) AddUser() {
panic(err)
}
c.Data["json"] = object.AddUser(&user)
c.Data["json"] = wrapActionResponse(object.AddUser(&user))
c.ServeJSON()
}
@ -70,6 +70,6 @@ func (c *ApiController) DeleteUser() {
panic(err)
}
c.Data["json"] = object.DeleteUser(&user)
c.Data["json"] = wrapActionResponse(object.DeleteUser(&user))
c.ServeJSON()
}