升级exception

This commit is contained in:
2025-05-10 20:44:31 +08:00
parent 18dd156718
commit fcfba9c50b
3 changed files with 30 additions and 18 deletions

View File

@ -46,7 +46,7 @@ func SetBusinessSuccessCode(code any) {
// Date : 22:37 2022/6/25
func Success(ctx *gin.Context, data any) {
successException := exception.NewSuccess(data)
Send(ctx, successException.GetCode(), successException.GetHttpCode(), successException.GetData(), nil)
Send(ctx, successException.Code(), http.StatusOK, successException.Data(), nil)
}
// SuccessWithExtension 返回扩展数据
@ -56,7 +56,7 @@ func Success(ctx *gin.Context, data any) {
// Date : 14:52 2024/9/24
func SuccessWithExtension(ctx *gin.Context, data any, responseOption *define.ResponseOption) {
successException := exception.NewSuccess(data)
Send(ctx, successException.GetCode(), successException.GetHttpCode(), successException.GetData(), responseOption)
Send(ctx, successException.Code(), http.StatusOK, successException.Data(), responseOption)
}
// Send 基础的发送数据
@ -91,11 +91,11 @@ func Send(ctx *gin.Context, code any, httpCode int, data any, responseOption *de
responseConfig := define.GetHttpHandleConfig()
ctx.Set(responseConfig.FinishRequestTimeField, finishRequestTime.UnixMilli())
ctx.Set(responseConfig.ResponseDataField, responseData)
responseException := exception.New(code, httpCode, responseData)
responseException := exception.New(code, responseData)
responseContentType := getResponseDataType(responseOption.ContentType)
responseInstance, _ := wrapper.NewJson(serialize.JSON.MarshalForStringIgnoreError(responseException.GetData()), &wrapper.Option{XmlName: responseOption.XmlName})
responseInstance, _ := wrapper.NewJson(serialize.JSON.MarshalForStringIgnoreError(responseException.Data()), &wrapper.Option{XmlName: responseOption.XmlName})
finalResponseData, _ := responseInstance.Marshal(responseContentType)
ctx.Data(responseException.GetHttpCode(), responseOption.ContentType, finalResponseData)
ctx.Data(http.StatusOK, responseOption.ContentType, finalResponseData)
}
// getResponseDataType 获取相应数据类型
@ -138,9 +138,9 @@ func SendWithStatusOK(ctx *gin.Context, code any, data any) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 13:08 2022/6/26
func SendWithException(ctx *gin.Context, e exception.IException, data map[string]any, responseOption *define.ResponseOption) {
func SendWithException(ctx *gin.Context, e exception.IException, responseOption *define.ResponseOption) {
if nil == e {
e = exception.NewSuccess(data)
e = exception.NewSuccess(map[string]any{})
}
if !define.GetHttpHandleConfig().DisableDebugStackOutput && nil != e {
stack := e.GetStack()
@ -148,11 +148,7 @@ func SendWithException(ctx *gin.Context, e exception.IException, data map[string
fmt.Println(stack)
}
}
outputData := map[string]any{
"e_data": e.GetData(),
"u_e_data": data,
}
Send(ctx, e.GetCode(), e.GetHttpCode(), outputData, responseOption)
Send(ctx, e.Code(), http.StatusOK, e.Data(), responseOption)
}
// JSON ctx.JSON 的平替, 增加了数据是否已相应的标识