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()
}

View File

@ -106,13 +106,12 @@ func UpdateApplication(id string, application *Application) bool {
return false
}
_, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(application)
affected, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(application)
if err != nil {
panic(err)
}
//return affected != 0
return true
return affected != 0
}
func AddApplication(application *Application) bool {

View File

@ -63,13 +63,12 @@ func UpdateOrganization(id string, organization *Organization) bool {
return false
}
_, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(organization)
affected, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(organization)
if err != nil {
panic(err)
}
//return affected != 0
return true
return affected != 0
}
func AddOrganization(organization *Organization) bool {

View File

@ -66,13 +66,12 @@ func UpdateProvider(id string, provider *Provider) bool {
return false
}
_, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(provider)
affected, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(provider)
if err != nil {
panic(err)
}
//return affected != 0
return true
return affected != 0
}
func AddProvider(provider *Provider) bool {

View File

@ -96,13 +96,12 @@ func UpdateToken(id string, token *Token) bool {
return false
}
_, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(token)
affected, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(token)
if err != nil {
panic(err)
}
//return affected != 0
return true
return affected != 0
}
func AddToken(token *Token) bool {

View File

@ -97,13 +97,12 @@ func UpdateUser(id string, user *User) bool {
return false
}
_, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(user)
affected, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(user)
if err != nil {
panic(err)
}
//return affected != 0
return true
return affected != 0
}
func AddUser(user *User) bool {

View File

@ -22,6 +22,8 @@ import (
"github.com/astaxie/beego/context"
"github.com/casdoor/casdoor/authz"
"github.com/casdoor/casdoor/controllers"
"github.com/casdoor/casdoor/util"
)
type Object struct {
@ -91,7 +93,8 @@ func getObject(ctx *context.Context) (string, string) {
func denyRequest(ctx *context.Context) {
w := ctx.ResponseWriter
w.WriteHeader(403)
_, err := w.Write([]byte("403 Forbidden\n"))
resp := &controllers.Response{Status: "error", Msg: "unauthorized operation"}
_, err := w.Write([]byte(util.StructToJson(resp)))
if err != nil {
panic(err)
}

View File

@ -56,7 +56,7 @@ class ApplicationEditPage extends React.Component {
OrganizationBackend.getOrganizations("admin")
.then((res) => {
this.setState({
organizations: res,
organizations: (res.msg === undefined) ? res : [],
});
});
}
@ -277,19 +277,19 @@ class ApplicationEditPage extends React.Component {
let application = Setting.deepCopy(this.state.application);
ApplicationBackend.updateApplication(this.state.application.owner, this.state.applicationName, application)
.then((res) => {
if (res) {
if (res.msg === "") {
Setting.showMessage("success", `Successfully saved`);
this.setState({
applicationName: this.state.application.name,
});
this.props.history.push(`/applications/${this.state.application.name}`);
} else {
Setting.showMessage("error", `failed to save: server side failure`);
Setting.showMessage("error", res.msg);
this.updateApplicationField('name', this.state.applicationName);
}
})
.catch(error => {
Setting.showMessage("error", `failed to save: ${error}`);
Setting.showMessage("error", `failed to connect to server: ${error}`);
});
}

View File

@ -104,19 +104,19 @@ class OrganizationEditPage extends React.Component {
let organization = Setting.deepCopy(this.state.organization);
OrganizationBackend.updateOrganization(this.state.organization.owner, this.state.organizationName, organization)
.then((res) => {
if (res) {
if (res.msg === "") {
Setting.showMessage("success", `Successfully saved`);
this.setState({
organizationName: this.state.organization.name,
});
this.props.history.push(`/organizations/${this.state.organization.name}`);
} else {
Setting.showMessage("error", `failed to save: server side failure`);
Setting.showMessage("error", res.msg);
this.updateOrganizationField('name', this.state.organizationName);
}
})
.catch(error => {
Setting.showMessage("error", `failed to save: ${error}`);
Setting.showMessage("error", `failed to connect to server: ${error}`);
});
}

View File

@ -143,19 +143,19 @@ class ProviderEditPage extends React.Component {
let provider = Setting.deepCopy(this.state.provider);
ProviderBackend.updateProvider(this.state.provider.owner, this.state.providerName, provider)
.then((res) => {
if (res) {
if (res.msg === "") {
Setting.showMessage("success", `Successfully saved`);
this.setState({
providerName: this.state.provider.name,
});
this.props.history.push(`/providers/${this.state.provider.name}`);
} else {
Setting.showMessage("error", `failed to save: server side failure`);
Setting.showMessage("error", res.msg);
this.updateProviderField('name', this.state.providerName);
}
})
.catch(error => {
Setting.showMessage("error", `failed to save: ${error}`);
Setting.showMessage("error", `failed to connect to server: ${error}`);
});
}

View File

@ -144,19 +144,19 @@ class TokenEditPage extends React.Component {
let token = Setting.deepCopy(this.state.token);
TokenBackend.updateToken(this.state.token.owner, this.state.tokenName, token)
.then((res) => {
if (res) {
if (res.msg === "") {
Setting.showMessage("success", `Successfully saved`);
this.setState({
tokenName: this.state.token.name,
});
this.props.history.push(`/tokens/${this.state.token.name}`);
} else {
Setting.showMessage("error", `failed to save: server side failure`);
Setting.showMessage("error", res.msg);
this.updateTokenField('name', this.state.tokenName);
}
})
.catch(error => {
Setting.showMessage("error", `failed to save: ${error}`);
Setting.showMessage("error", `failed to connect to server: ${error}`);
});
}

View File

@ -53,7 +53,7 @@ class UserEditPage extends React.Component {
OrganizationBackend.getOrganizations("admin")
.then((res) => {
this.setState({
organizations: res,
organizations: (res.msg === undefined) ? res : [],
});
});
}
@ -266,7 +266,7 @@ class UserEditPage extends React.Component {
let user = Setting.deepCopy(this.state.user);
UserBackend.updateUser(this.state.organizationName, this.state.userName, user)
.then((res) => {
if (res) {
if (res.msg === "") {
Setting.showMessage("success", `Successfully saved`);
this.setState({
organizationName: this.state.user.owner,
@ -277,13 +277,13 @@ class UserEditPage extends React.Component {
this.props.history.push(`/users/${this.state.user.owner}/${this.state.user.name}`);
}
} else {
Setting.showMessage("error", `failed to save: server side failure`);
Setting.showMessage("error", res.msg);
this.updateUserField('owner', this.state.organizationName);
this.updateUserField('name', this.state.userName);
}
})
.catch(error => {
Setting.showMessage("error", `failed to save: ${error}`);
Setting.showMessage("error", `failed to connect to server: ${error}`);
});
}