-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67dddd6
commit 38733d8
Showing
5 changed files
with
71 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package response | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/infraboard/mcube/v2/exception" | ||
"github.com/infraboard/mcube/v2/http/response" | ||
"github.com/infraboard/mcube/v2/ioc/config/application" | ||
) | ||
|
||
// 正常请求数据返回 | ||
func Success(c *gin.Context, data any) { | ||
// 是否需要脱敏 | ||
if v, ok := data.(response.DesenseObj); ok { | ||
v.Desense() | ||
} | ||
|
||
c.JSON(http.StatusOK, data) | ||
} | ||
|
||
// 异常情况的数据返回, 返回我们的业务Exception | ||
func Failed(c *gin.Context, err error) { | ||
// 如果出现多个Handler, 需要通过手动abord | ||
defer c.Abort() | ||
|
||
var e *exception.APIException | ||
if v, ok := err.(*exception.APIException); ok { | ||
e = v | ||
} else { | ||
// 非可以预期, 没有定义业务的情况 | ||
e = exception.NewAPIException( | ||
http.StatusInternalServerError, | ||
http.StatusText(http.StatusInternalServerError), | ||
"%s", | ||
err.Error(), | ||
) | ||
e.HttpCode = http.StatusInternalServerError | ||
|
||
} | ||
|
||
if e.Namespace == "" { | ||
e.WithNamespace(application.Get().AppName) | ||
} | ||
|
||
c.JSON(e.HttpCode, e) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters