mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Improve API error handling.
This commit is contained in:
parent
6c2c5be33d
commit
d6715c7601
@ -43,7 +43,7 @@ func (c *ApiController) UpdateApplication() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.UpdateApplication(id, &application)
|
c.Data["json"] = wrapActionResponse(object.UpdateApplication(id, &application))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ func (c *ApiController) AddApplication() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.AddApplication(&application)
|
c.Data["json"] = wrapActionResponse(object.AddApplication(&application))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +65,6 @@ func (c *ApiController) DeleteApplication() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.DeleteApplication(&application)
|
c.Data["json"] = wrapActionResponse(object.DeleteApplication(&application))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
@ -32,3 +32,11 @@ func (c *ApiController) GetSessionUser() string {
|
|||||||
func (c *ApiController) SetSessionUser(user string) {
|
func (c *ApiController) SetSessionUser(user string) {
|
||||||
c.SetSession("username", user)
|
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"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -43,7 +43,7 @@ func (c *ApiController) UpdateOrganization() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.UpdateOrganization(id, &organization)
|
c.Data["json"] = wrapActionResponse(object.UpdateOrganization(id, &organization))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ func (c *ApiController) AddOrganization() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.AddOrganization(&organization)
|
c.Data["json"] = wrapActionResponse(object.AddOrganization(&organization))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +65,6 @@ func (c *ApiController) DeleteOrganization() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.DeleteOrganization(&organization)
|
c.Data["json"] = wrapActionResponse(object.DeleteOrganization(&organization))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ func (c *ApiController) UpdateProvider() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.UpdateProvider(id, &provider)
|
c.Data["json"] = wrapActionResponse(object.UpdateProvider(id, &provider))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ func (c *ApiController) AddProvider() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.AddProvider(&provider)
|
c.Data["json"] = wrapActionResponse(object.AddProvider(&provider))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +65,6 @@ func (c *ApiController) DeleteProvider() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.DeleteProvider(&provider)
|
c.Data["json"] = wrapActionResponse(object.DeleteProvider(&provider))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ func (c *ApiController) UpdateToken() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.UpdateToken(id, &token)
|
c.Data["json"] = wrapActionResponse(object.UpdateToken(id, &token))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ func (c *ApiController) AddToken() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.AddToken(&token)
|
c.Data["json"] = wrapActionResponse(object.AddToken(&token))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ func (c *ApiController) DeleteToken() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.DeleteToken(&token)
|
c.Data["json"] = wrapActionResponse(object.DeleteToken(&token))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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"`
|
|
||||||
}
|
|
@ -48,7 +48,7 @@ func (c *ApiController) UpdateUser() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.UpdateUser(id, &user)
|
c.Data["json"] = wrapActionResponse(object.UpdateUser(id, &user))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ func (c *ApiController) AddUser() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.AddUser(&user)
|
c.Data["json"] = wrapActionResponse(object.AddUser(&user))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,6 +70,6 @@ func (c *ApiController) DeleteUser() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = object.DeleteUser(&user)
|
c.Data["json"] = wrapActionResponse(object.DeleteUser(&user))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
@ -106,13 +106,12 @@ func UpdateApplication(id string, application *Application) bool {
|
|||||||
return false
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//return affected != 0
|
return affected != 0
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddApplication(application *Application) bool {
|
func AddApplication(application *Application) bool {
|
||||||
|
@ -63,13 +63,12 @@ func UpdateOrganization(id string, organization *Organization) bool {
|
|||||||
return false
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//return affected != 0
|
return affected != 0
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddOrganization(organization *Organization) bool {
|
func AddOrganization(organization *Organization) bool {
|
||||||
|
@ -66,13 +66,12 @@ func UpdateProvider(id string, provider *Provider) bool {
|
|||||||
return false
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//return affected != 0
|
return affected != 0
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddProvider(provider *Provider) bool {
|
func AddProvider(provider *Provider) bool {
|
||||||
|
@ -96,13 +96,12 @@ func UpdateToken(id string, token *Token) bool {
|
|||||||
return false
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//return affected != 0
|
return affected != 0
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddToken(token *Token) bool {
|
func AddToken(token *Token) bool {
|
||||||
|
@ -97,13 +97,12 @@ func UpdateUser(id string, user *User) bool {
|
|||||||
return false
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//return affected != 0
|
return affected != 0
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddUser(user *User) bool {
|
func AddUser(user *User) bool {
|
||||||
|
@ -22,6 +22,8 @@ import (
|
|||||||
|
|
||||||
"github.com/astaxie/beego/context"
|
"github.com/astaxie/beego/context"
|
||||||
"github.com/casdoor/casdoor/authz"
|
"github.com/casdoor/casdoor/authz"
|
||||||
|
"github.com/casdoor/casdoor/controllers"
|
||||||
|
"github.com/casdoor/casdoor/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Object struct {
|
type Object struct {
|
||||||
@ -91,7 +93,8 @@ func getObject(ctx *context.Context) (string, string) {
|
|||||||
func denyRequest(ctx *context.Context) {
|
func denyRequest(ctx *context.Context) {
|
||||||
w := ctx.ResponseWriter
|
w := ctx.ResponseWriter
|
||||||
w.WriteHeader(403)
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ class ApplicationEditPage extends React.Component {
|
|||||||
OrganizationBackend.getOrganizations("admin")
|
OrganizationBackend.getOrganizations("admin")
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
this.setState({
|
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);
|
let application = Setting.deepCopy(this.state.application);
|
||||||
ApplicationBackend.updateApplication(this.state.application.owner, this.state.applicationName, application)
|
ApplicationBackend.updateApplication(this.state.application.owner, this.state.applicationName, application)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res) {
|
if (res.msg === "") {
|
||||||
Setting.showMessage("success", `Successfully saved`);
|
Setting.showMessage("success", `Successfully saved`);
|
||||||
this.setState({
|
this.setState({
|
||||||
applicationName: this.state.application.name,
|
applicationName: this.state.application.name,
|
||||||
});
|
});
|
||||||
this.props.history.push(`/applications/${this.state.application.name}`);
|
this.props.history.push(`/applications/${this.state.application.name}`);
|
||||||
} else {
|
} else {
|
||||||
Setting.showMessage("error", `failed to save: server side failure`);
|
Setting.showMessage("error", res.msg);
|
||||||
this.updateApplicationField('name', this.state.applicationName);
|
this.updateApplicationField('name', this.state.applicationName);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
Setting.showMessage("error", `failed to save: ${error}`);
|
Setting.showMessage("error", `failed to connect to server: ${error}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,19 +104,19 @@ class OrganizationEditPage extends React.Component {
|
|||||||
let organization = Setting.deepCopy(this.state.organization);
|
let organization = Setting.deepCopy(this.state.organization);
|
||||||
OrganizationBackend.updateOrganization(this.state.organization.owner, this.state.organizationName, organization)
|
OrganizationBackend.updateOrganization(this.state.organization.owner, this.state.organizationName, organization)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res) {
|
if (res.msg === "") {
|
||||||
Setting.showMessage("success", `Successfully saved`);
|
Setting.showMessage("success", `Successfully saved`);
|
||||||
this.setState({
|
this.setState({
|
||||||
organizationName: this.state.organization.name,
|
organizationName: this.state.organization.name,
|
||||||
});
|
});
|
||||||
this.props.history.push(`/organizations/${this.state.organization.name}`);
|
this.props.history.push(`/organizations/${this.state.organization.name}`);
|
||||||
} else {
|
} else {
|
||||||
Setting.showMessage("error", `failed to save: server side failure`);
|
Setting.showMessage("error", res.msg);
|
||||||
this.updateOrganizationField('name', this.state.organizationName);
|
this.updateOrganizationField('name', this.state.organizationName);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
Setting.showMessage("error", `failed to save: ${error}`);
|
Setting.showMessage("error", `failed to connect to server: ${error}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,19 +143,19 @@ class ProviderEditPage extends React.Component {
|
|||||||
let provider = Setting.deepCopy(this.state.provider);
|
let provider = Setting.deepCopy(this.state.provider);
|
||||||
ProviderBackend.updateProvider(this.state.provider.owner, this.state.providerName, provider)
|
ProviderBackend.updateProvider(this.state.provider.owner, this.state.providerName, provider)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res) {
|
if (res.msg === "") {
|
||||||
Setting.showMessage("success", `Successfully saved`);
|
Setting.showMessage("success", `Successfully saved`);
|
||||||
this.setState({
|
this.setState({
|
||||||
providerName: this.state.provider.name,
|
providerName: this.state.provider.name,
|
||||||
});
|
});
|
||||||
this.props.history.push(`/providers/${this.state.provider.name}`);
|
this.props.history.push(`/providers/${this.state.provider.name}`);
|
||||||
} else {
|
} else {
|
||||||
Setting.showMessage("error", `failed to save: server side failure`);
|
Setting.showMessage("error", res.msg);
|
||||||
this.updateProviderField('name', this.state.providerName);
|
this.updateProviderField('name', this.state.providerName);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
Setting.showMessage("error", `failed to save: ${error}`);
|
Setting.showMessage("error", `failed to connect to server: ${error}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,19 +144,19 @@ class TokenEditPage extends React.Component {
|
|||||||
let token = Setting.deepCopy(this.state.token);
|
let token = Setting.deepCopy(this.state.token);
|
||||||
TokenBackend.updateToken(this.state.token.owner, this.state.tokenName, token)
|
TokenBackend.updateToken(this.state.token.owner, this.state.tokenName, token)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res) {
|
if (res.msg === "") {
|
||||||
Setting.showMessage("success", `Successfully saved`);
|
Setting.showMessage("success", `Successfully saved`);
|
||||||
this.setState({
|
this.setState({
|
||||||
tokenName: this.state.token.name,
|
tokenName: this.state.token.name,
|
||||||
});
|
});
|
||||||
this.props.history.push(`/tokens/${this.state.token.name}`);
|
this.props.history.push(`/tokens/${this.state.token.name}`);
|
||||||
} else {
|
} else {
|
||||||
Setting.showMessage("error", `failed to save: server side failure`);
|
Setting.showMessage("error", res.msg);
|
||||||
this.updateTokenField('name', this.state.tokenName);
|
this.updateTokenField('name', this.state.tokenName);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
Setting.showMessage("error", `failed to save: ${error}`);
|
Setting.showMessage("error", `failed to connect to server: ${error}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ class UserEditPage extends React.Component {
|
|||||||
OrganizationBackend.getOrganizations("admin")
|
OrganizationBackend.getOrganizations("admin")
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
this.setState({
|
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);
|
let user = Setting.deepCopy(this.state.user);
|
||||||
UserBackend.updateUser(this.state.organizationName, this.state.userName, user)
|
UserBackend.updateUser(this.state.organizationName, this.state.userName, user)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res) {
|
if (res.msg === "") {
|
||||||
Setting.showMessage("success", `Successfully saved`);
|
Setting.showMessage("success", `Successfully saved`);
|
||||||
this.setState({
|
this.setState({
|
||||||
organizationName: this.state.user.owner,
|
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}`);
|
this.props.history.push(`/users/${this.state.user.owner}/${this.state.user.name}`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Setting.showMessage("error", `failed to save: server side failure`);
|
Setting.showMessage("error", res.msg);
|
||||||
this.updateUserField('owner', this.state.organizationName);
|
this.updateUserField('owner', this.state.organizationName);
|
||||||
this.updateUserField('name', this.state.userName);
|
this.updateUserField('name', this.state.userName);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
Setting.showMessage("error", `failed to save: ${error}`);
|
Setting.showMessage("error", `failed to connect to server: ${error}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user