前言:为了友好的展现接口参数与返回值,让前端人员操作更清晰,更效率,以及接口异常参数反馈,相关必要日志记录,Furion就推出这样一套规范化开发框架
1.友好异常处理(接下来,示范一下数组越界异常)
在上一张文章基础上修改
using Furion.DynamicApiController;
using Furion.FriendlyException;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace F2.Application
{
public class FurionAppService : IDynamicApiController
{
public string Getwhat(int num)
{
string[] str= new string[] { "222", "333" };
if (str.Count()>num)
return str[num];
else
{
string errorMessage = $"{num} 不能大于等于{str.Count()}";
throw Oops.Oh(errorMessage);
}
}
}
}
正常情况下输出:
异常情况下输出:
这样相比于 大量的 try catch
代码,结构更小,可维护性更清晰,看起来也比较舒适,
2.appsetting.json
中配置 配置通用异常提示
"ErrorCodeMessageSettings": {
"Definitions": [
[ "-2001", "添加异常" ],
[ "-2002", "啥啥异常" ],
[ "-2003", "Oops! 出错了" ]
]
}
public string Getwhat(int num)
{
string[] str= new string[] { "222", "333" };
if (str.Count()>num)
return str[num];
else
{
//string errorMessage = $"{num} 不能大于等于{str.Count()}";
throw Oops.Oh(-2003);
}
}
输出: