优化异常实例化

This commit is contained in:
白茶清欢 2022-07-03 13:08:24 +08:00
parent b301bd1849
commit 0679ce5092
3 changed files with 11 additions and 8 deletions

View File

@ -4,7 +4,10 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="0c7a619f-b520-4d41-ab0d-cfa1799d3cdf" name="Changes" comment="" />
<list default="true" id="0c7a619f-b520-4d41-ab0d-cfa1799d3cdf" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/abstrace.go" beforeDir="false" afterPath="$PROJECT_DIR$/abstrace.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/exception.go" beforeDir="false" afterPath="$PROJECT_DIR$/exception.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />

View File

@ -20,7 +20,7 @@ type IException interface {
// GetMessage *获取错误信息
GetMessage() string
// GetData 获取异常时的返回数据
GetData() map[string]interface{}
GetData() interface{}
// GetHttpCode *获取当前异常要返回的http状态码, 不设置则 默认 200
GetHttpCode() int
}

View File

@ -46,7 +46,7 @@ func (e *Exception) GetHttpCode() int {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:22 2022/6/25
func NewWithCode(code interface{}) *Exception {
func NewWithCode(code interface{}) IException {
return New(code, defaultHttpCode, nil)
}
@ -55,7 +55,7 @@ func NewWithCode(code interface{}) *Exception {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:25 2022/6/25
func NewWithCodeAndHttpCode(code interface{}, httpCode int) *Exception {
func NewWithCodeAndHttpCode(code interface{}, httpCode int) IException {
return New(code, httpCode, nil)
}
@ -64,7 +64,7 @@ func NewWithCodeAndHttpCode(code interface{}, httpCode int) *Exception {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:28 2022/6/25
func NewWithCodeAndData(code interface{}, data interface{}) *Exception {
func NewWithCodeAndData(code interface{}, data interface{}) IException {
return New(code, defaultHttpCode, data)
}
@ -73,7 +73,7 @@ func NewWithCodeAndData(code interface{}, data interface{}) *Exception {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:28 2022/6/25
func New(code interface{}, httpCode int, data interface{}) *Exception {
func New(code interface{}, httpCode int, data interface{}) IException {
if nil == data {
// 保证数据结构的一致性, 同时避免后续使用出现空指针
data = map[string]interface{}{}
@ -91,7 +91,7 @@ func New(code interface{}, httpCode int, data interface{}) *Exception {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:36 2022/6/25
func NewFromError(code interface{}, err error) *Exception {
func NewFromError(code interface{}, err error) IException {
if nil == err {
return nil
}
@ -126,6 +126,6 @@ func IsSuccess(e *Exception) bool {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:35 2022/6/25
func NewSuccess(data interface{}) *Exception {
func NewSuccess(data interface{}) IException {
return New(defaultSuccessCode, defaultHttpCode, data)
}