规范api返回异常,统一改用400异常

This commit is contained in:
falcon 2020-05-06 15:57:09 +08:00
parent 1fbf0ada64
commit 4ea469a1fa
11 changed files with 89 additions and 53 deletions

View File

@ -1,6 +1,7 @@
using System;
using FAuth.Extensions;
using FAuth.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
@ -12,6 +13,7 @@ namespace FAuth.Controllers.api
[ApiController, Route("api/[Controller]/[Action]")]
[ServiceFilter(typeof(ApiExceptionFilterAttribute))]
[ProducesResponseType(typeof(ApiErrorResult),500)]
[ProducesResponseType(typeof(ApiErrorResult),400)]
public abstract class ApiControllerBase<LoggerType>:ControllerBase<LoggerType>
{
public ApiControllerBase(ILogger<LoggerType> logger,IServiceProvider service) : base(logger,service) {

View File

@ -8,6 +8,7 @@ using FAuth.DataBase;
using System.Linq;
using FAuth.DataBase.Tables;
using Microsoft.EntityFrameworkCore;
using FAuth.Extensions;
namespace FAuth.Controllers.api
{
@ -29,11 +30,11 @@ namespace FAuth.Controllers.api
[ProducesResponseType(typeof(Apps),200)]
public Apps AddNewApp(string appName,string description) {
if(appName.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(appName));
throw new ApiArgumentNullException(nameof(appName));
}
var qu = this.Db.Apps.Where(m => m.Name == appName);
if(qu.Any()) {
throw new Exception($"应用{appName}已经存在,不可以重复创建!");
throw new ApiException($"应用{appName}已经存在,不可以重复创建!");
}
var newApp = new Apps {
Name = appName,
@ -47,7 +48,7 @@ namespace FAuth.Controllers.api
[HttpPost]
public bool RemoveApp(string appName) {
if(appName.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(appName));
throw new ApiArgumentNullException(nameof(appName));
}
var qu = this.Db.Apps.Where(m => m.Name == appName);
foreach(var item in qu) {
@ -66,12 +67,12 @@ namespace FAuth.Controllers.api
[ProducesResponseType(typeof(Apps),200)]
public Apps GetApps(string appName) {
if(string.IsNullOrEmpty(appName))
throw new ArgumentException("message",nameof(appName));
throw new ApiArgumentNullException(nameof(appName));
var qu = this.Db.Apps.Where(m => m.Name == appName);
if(qu.Any()) {
return qu.First();
} else {
throw new Exception("没有找到该应用");
throw new ApiException("没有找到该应用");
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Linq;
using FAuth.DataBase.Tables;
using FAuth.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
@ -24,11 +25,11 @@ namespace FAuth.Controllers.api
public bool Binding(int appId,int roleGroupId) {
var aQu = this.Db.Apps.Where(m => m.Id == appId);
if(!aQu.Any()) {
throw new Exception("没有找到指定的应用");
throw new ApiException("没有找到指定的应用");
}
var rQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId);
if(!rQu.Any()) {
throw new Exception("没有找到指定的角色组");
throw new ApiException("没有找到指定的角色组");
}
var arQu = this.Db.App_RoleGroups.Where(m => m.AppId == appId && m.RoleGroupId == roleGroupId);
if(!arQu.Any()) {
@ -51,11 +52,11 @@ namespace FAuth.Controllers.api
public bool Unbind(int appId,int roleGroupId) {
var aQu = this.Db.Apps.Where(m => m.Id == appId);
if(!aQu.Any()) {
throw new Exception("没有找到指定的应用");
throw new ApiException("没有找到指定的应用");
}
var rQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId);
if(!rQu.Any()) {
throw new Exception("没有找到指定的角色组");
throw new ApiException("没有找到指定的角色组");
}
var arQu = this.Db.App_RoleGroups.Where(m => m.AppId == appId && m.RoleGroupId == roleGroupId);
if(arQu.Any()) {

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Threading.Tasks;
using Falcon.Extend;
using FAuth.DataBase.Tables;
using FAuth.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
@ -28,11 +29,11 @@ namespace FAuth.Controllers.api
[ProducesResponseType(typeof(Roles),200)]
public Roles AddNewRole(string roleName,string description) {
if(roleName.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(roleName));
throw new ApiArgumentNullException(nameof(roleName));
}
var qu = this.Db.Roles.Where(m => m.Name == roleName);
if(qu.Any()) {
throw new Exception($"角色{roleName}已经存在,不可以重复创建!");
throw new ApiException($"角色{roleName}已经存在,不可以重复创建!");
}
var newRole = new Roles {
Name = roleName,
@ -51,13 +52,11 @@ namespace FAuth.Controllers.api
[HttpPost]
[ProducesResponseType(typeof(Roles),200)]
public Roles GetApps(string roleName) {
if(string.IsNullOrEmpty(roleName))
throw new ArgumentException("message",nameof(roleName));
var qu = this.Db.Roles.Where(m => m.Name == roleName);
if(qu.Any()) {
return qu.First();
} else {
throw new Exception("没有找到该角色");
throw new ApiException("没有找到该角色");
}
}

View File

@ -2,6 +2,7 @@
using System.Linq;
using Falcon.Extend;
using FAuth.DataBase.Tables;
using FAuth.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
@ -26,11 +27,11 @@ namespace FAuth.Controllers.api
[ProducesResponseType(typeof(RoleGroup),200)]
public RoleGroup AddNew(string name,string description) {
if(name.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(name));
throw new ApiArgumentNullException(nameof(name));
}
var qu = this.Db.RoleGroups.Where(m => m.Name == name);
if(qu.Any()) {
throw new Exception($"角色组{name}已经存在,不可以重复创建!");
throw new ApiException($"角色组{name}已经存在,不可以重复创建!");
}
var model = new RoleGroup {
Name = name,
@ -50,7 +51,7 @@ namespace FAuth.Controllers.api
[ProducesResponseType(typeof(RoleGroup),200)]
public RoleGroup GetApps(string name) {
if(string.IsNullOrEmpty(name))
throw new ArgumentException("message",nameof(name));
throw new ApiArgumentNullException(nameof(name));
var qu = this.Db.RoleGroups.Where(m => m.Name == name);
if(qu.Any()) {
return qu.First();

View File

@ -1,6 +1,7 @@
using System;
using System.Linq;
using FAuth.DataBase.Tables;
using FAuth.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
@ -25,11 +26,11 @@ namespace FAuth.Controllers.api
public bool Binding(int roleGroupId,int roleId) {
var rgQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId);
if(!rgQu.Any()) {
throw new Exception("没有找到指定的角色组");
throw new ApiException("没有找到指定的角色组");
}
var rQu = this.Db.Roles.Where(m => m.Id == roleId);
if(!rQu.Any()) {
throw new Exception("没有找到指定的角色");
throw new ApiException("没有找到指定的角色");
}
var rgr = this.Db.RoleGroup_Roles.Where(m => m.RoleGroupId == roleGroupId && m.RoleId == roleId);
if(!rgr.Any()) {
@ -52,11 +53,11 @@ namespace FAuth.Controllers.api
public bool Unbind(int roleGroupId,int roleId) {
var rgQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId);
if(!rgQu.Any()) {
throw new Exception("没有找到指定的角色组");
throw new ApiException("没有找到指定的角色组");
}
var rQu = this.Db.Roles.Where(m => m.Id == roleId);
if(!rQu.Any()) {
throw new Exception("没有找到指定的角色");
throw new ApiException("没有找到指定的角色");
}
var rgr = this.Db.RoleGroup_Roles.Where(m => m.RoleGroupId == roleGroupId && m.RoleId == roleId);
if(rgr.Any()) {

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FAuth.DataBase.Tables;
using FAuth.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
@ -26,11 +27,11 @@ namespace FAuth.Controllers.api
public bool Binding(int roleId,int userId) {
var rQu = this.Db.Roles.Where(m => m.Id == roleId);
if(!rQu.Any()) {
throw new Exception("没有找到指定的角色");
throw new ApiException("没有找到指定的角色");
}
var uQu = this.Db.Users.Where(m => m.Id == userId);
if(!uQu.Any()) {
throw new Exception("没有找到指定的用户");
throw new ApiException("没有找到指定的用户");
}
var ruQu = this.Db.Role_Users.Where(m => m.RoleId == roleId && m.UserId == userId);
if(!ruQu.Any()) {
@ -53,11 +54,11 @@ namespace FAuth.Controllers.api
public bool Unbind(int roleId,int userId) {
var rQu = this.Db.Roles.Where(m => m.Id == roleId);
if(!rQu.Any()) {
throw new Exception("没有找到指定的角色");
throw new ApiException("没有找到指定的角色");
}
var uQu = this.Db.Users.Where(m => m.Id == userId);
if(!uQu.Any()) {
throw new Exception("没有找到指定的用户");
throw new ApiException("没有找到指定的用户");
}
var ruQu = this.Db.Role_Users.Where(m => m.RoleId == roleId && m.UserId == userId);
if(ruQu.Any()) {

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using Falcon.Extend;
using FAuth.DataBase.Tables;
using FAuth.Extensions;
using FAuth.Extensions.Account;
using FAuth.Extensions.Decryptor;
using FAuth.Models;
@ -38,11 +39,11 @@ namespace FAuth.Controllers.api
[ProducesResponseType(typeof(CheckUserResult),200)]
public CheckUserResult Login(string userName,string password) {
if(userName.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(userName));
throw new ApiArgumentNullException(nameof(userName));
}
var qu = this.Db.Users.Where(m => m.UserName == userName && m.Password == password);
if(!qu.Any()) {
throw new Exception("提供的用户名或密码不正确");
throw new ApiException("提供的用户名或密码不正确");
}
var fir = qu.First();
var now = DateTimeOffset.Now;
@ -66,12 +67,12 @@ namespace FAuth.Controllers.api
[HttpPost]
public bool Logout(string ticket) {
if(ticket.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(ticket));
throw new ApiArgumentNullException(nameof(ticket));
}
var userTicketModel = this.UserTicketDryptor.Decrypt(ticket);
var qu = this.Db.Users.Where(m => m.Id == userTicketModel.Id);
if(!qu.Any()) {
throw new Exception("提交的票据信息错误");
throw new ApiException("提交的票据信息错误");
}
var fir = qu.First();
fir.Status &= ~FUserStatusEnum.Login;
@ -90,16 +91,16 @@ namespace FAuth.Controllers.api
[ProducesResponseType(typeof(UserInfo),200)]
public UserInfo GetUserByTicket([BindRequired]string ticket) {
if(ticket.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(ticket));
throw new ApiArgumentNullException(nameof(ticket));
}
var userTicketModel = this.UserTicketDryptor.Decrypt(ticket);
var qu = this.Db.Users.Where(m => m.Id == userTicketModel.Id);
if(!qu.Any()) {
throw new Exception("提交的票据信息错误");
throw new ApiException("提交的票据信息错误");
}
var fir = qu.First();
if(fir.LastLogoutDatetime.HasValue && userTicketModel.LoginDatetime < fir.LastLogoutDatetime.Value) {
throw new Exception("用户已登出");
throw new ApiException("用户已登出");
}
return new UserInfo {
Id = fir.Id,
@ -137,12 +138,12 @@ namespace FAuth.Controllers.api
[HttpPost]
public bool ChangePassword(string ticket,string nPassword) {
if(ticket.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(ticket));
throw new ApiArgumentNullException(nameof(ticket));
}
var userTicketModel = this.UserTicketDryptor.Decrypt(ticket);
var qu = this.Db.Users.Where(m => m.Id == userTicketModel.Id);
if(!qu.Any()) {
throw new Exception("没找到票据对应用户");
throw new ApiException("没找到票据对应用户");
}
foreach(var item in qu) {
item.Password = nPassword;
@ -162,21 +163,19 @@ namespace FAuth.Controllers.api
/// <returns>是否成功</returns>
[HttpPost]
public bool AddNewUser(string adminTicket,string userName,string name,string password) {
if(string.IsNullOrEmpty(adminTicket))
throw new ArgumentException("必须提供管理票据",nameof(adminTicket));
if(string.IsNullOrEmpty(userName))
throw new ArgumentException("新用户登录名不能为空",nameof(userName));
if(string.IsNullOrEmpty(name))
throw new ArgumentException("用户姓名不能为空",nameof(name));
if(string.IsNullOrEmpty(password))
throw new ArgumentException("密码不能为空",nameof(password));
if(adminTicket is null)
throw new ApiArgumentNullException(nameof(adminTicket));
if(userName is null)
throw new ApiArgumentNullException(nameof(userName));
if(password is null)
throw new ApiArgumentNullException(nameof(password));
if(this.Account.IsNotSystemAdmin(adminTicket)) {
throw new Exception($"用户必须在应用FAuth中具有Admin角色");
throw new ApiException($"用户必须在应用FAuth中具有Admin角色");
}
var qu = this.Db.Users.Where(m => m.UserName == userName);
if(qu.Any()) {
throw new Exception("用户登录名已经存在,不能重复添加");
throw new ApiException("用户登录名已经存在,不能重复添加");
}
var nUser = new FUser {
Name = name,
@ -198,19 +197,19 @@ namespace FAuth.Controllers.api
/// <returns>是否成功。成功True 否则返回False</returns>
[HttpPost]
public bool ResetUserPassword(string adminTicket,string userName,string newPassword) {
if(string.IsNullOrEmpty(adminTicket))
throw new ArgumentException("必须提供管理票据",nameof(adminTicket));
if(string.IsNullOrEmpty(userName))
throw new ArgumentException("新用户登录名不能为空",nameof(userName));
if(string.IsNullOrEmpty(newPassword))
throw new ArgumentException("密码不能为空",nameof(newPassword));
if(adminTicket is null)
throw new ApiArgumentNullException(nameof(adminTicket));
if(userName is null)
throw new ApiArgumentNullException(nameof(userName));
if(newPassword is null)
throw new ApiArgumentNullException(nameof(newPassword));
if(this.Account.IsNotSystemAdmin(adminTicket)) {
throw new Exception($"用户必须在应用FAuth中具有Admin角色");
throw new ApiException($"用户必须在应用FAuth中具有Admin角色");
}
var qu = this.Db.Users.Where(m => m.UserName == userName);
if(!qu.Any()) {
throw new Exception("用户登录名不存在");
throw new ApiException("用户登录名不存在");
}
foreach(var item in qu) {
item.Password = newPassword;

View File

@ -0,0 +1,11 @@
namespace FAuth.Extensions
{
/// <summary>
/// 表示请求的参数为空错误
/// </summary>
public class ApiArgumentNullException:ApiException
{
public ApiArgumentNullException(string msg) : base($"{msg} 参数不能为空") {
}
}
}

View File

@ -0,0 +1,16 @@
using System;
namespace FAuth.Extensions
{
/// <summary>
/// 表示请求错误,服务器无法处理
/// </summary>
public class ApiException:Exception
{
/// <summary>
/// 通过提供异常信息返回api错误异常
/// </summary>
/// <param name="msg"></param>
public ApiException(string msg) : base(msg) { }
}
}

View File

@ -25,7 +25,11 @@ namespace FAuth.Extensions
Message = context.Exception.Message,
Id = id,
};
context.Result = new JsonResult(result) { StatusCode = StatusCodes.Status500InternalServerError };
var sc =
context.Exception is ApiException ? StatusCodes.Status400BadRequest :
StatusCodes.Status500InternalServerError;
context.Result = new JsonResult(result) { StatusCode = sc };
}
}