Skip to content

Commit

Permalink
Merge pull request #24 from gone-io/feature/1.x
Browse files Browse the repository at this point in the history
doc: Adjust the Chinese comments in the code to English
  • Loading branch information
Degfy authored Jun 14, 2024
2 parents aae0663 + f044933 commit 22a986b
Show file tree
Hide file tree
Showing 23 changed files with 632 additions and 305 deletions.
4 changes: 2 additions & 2 deletions cemetery.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (c *cemetery) reviveFieldById(tag string, field reflect.StructField, v refl

goner := tomb.GetGoner()
if IsCompatible(field.Type, goner) {
c.setFieldValue(v, goner)
setFieldValue(v, goner)
suc = true
return
}
Expand Down Expand Up @@ -241,7 +241,7 @@ func (c *cemetery) reviveByVampire2(goner Goner, tomb Tomb, extConfig string, v
func (c *cemetery) reviveFieldByType(field reflect.StructField, v reflect.Value, goneTypeName string) (deps []Tomb, suc bool) {
container := c.getGonerContainerByType(field.Type, fmt.Sprintf("%s.%s", goneTypeName, field.Name))
if container != nil {
c.setFieldValue(v, container.GetGoner())
setFieldValue(v, container.GetGoner())
suc = true
deps = append(deps, container)
}
Expand Down
84 changes: 76 additions & 8 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,29 @@ package gone

import (
"fmt"
"net/http"
"reflect"
)

// BError Business error implementation
type BError struct {
err Error
data any
}

func (e *BError) Msg() string {
return e.err.Msg()
}
func (e *BError) Code() int {
return e.err.Code()
}
func (e *BError) Error() string {
return e.err.Error()
}
func (e *BError) Data() any {
return e.data
}

type defaultErr struct {
code int
msg string
Expand All @@ -21,10 +41,54 @@ func (e *defaultErr) Code() int {
return e.code
}

// NewError create a error
func NewError(code int, msg string) Error {
return &defaultErr{code: code, msg: msg}
}

// NewParameterError create a Parameter error
func NewParameterError(msg string, ext ...int) Error {
var code = http.StatusBadRequest
if len(ext) > 0 {
code = ext[0]
}
return NewError(code, msg)
}

// NewBusinessError create a business error
func NewBusinessError(msg string, ext ...any) BusinessError {
var code = 0
var data any = nil
if len(ext) > 0 {
i, ok := ext[0].(int)
if ok {
code = i
}
}
if len(ext) > 1 {
data = ext[1]
}

return &BError{err: NewError(code, msg), data: data}
}

// ToError translate any type to An Error
func ToError(input any) Error {
if input == nil {
return nil
}
switch input.(type) {
case Error:
return input.(Error)
case error:
return NewInnerError(input.(error).Error(), http.StatusInternalServerError)
case string:
return NewInnerError(input.(string), http.StatusInternalServerError)
default:
return NewInnerError(fmt.Sprintf("%v", input), http.StatusInternalServerError)
}
}

type iError struct {
*defaultErr
trace []byte
Expand All @@ -47,33 +111,37 @@ func NewInnerErrorSkip(msg string, code int, skip int) Error {
return &iError{defaultErr: &defaultErr{code: code, msg: msg}, trace: PanicTrace(2, skip)}
}

// 错误代码:gone框架内部错误代码编码空间:1001~1999
// Error Code:1001~1999 used for gone framework.
const (
// GonerIdIsExisted GonerId 不存在
// GonerIdIsExisted Goner for the GonerId is existed.
GonerIdIsExisted = 1001 + iota

// CannotFoundGonerById 通过GonerId查找Goner失败
// CannotFoundGonerById cannot find the Goner by the GonerId.
CannotFoundGonerById

// CannotFoundGonerByType 通过类型查找Goner失败
// CannotFoundGonerByType cannot find the Goner by the Type.
CannotFoundGonerByType

//NotCompatible 类型不兼容
//NotCompatible Goner is not compatible with the Type.
NotCompatible

//ReplaceBuryIdParamEmpty 替换性下葬,GonerId不能为空
//ReplaceBuryIdParamEmpty Cemetery.ReplaceBury error for the GonerId is empty.
ReplaceBuryIdParamEmpty

//StartError Gone Start flow error.
StartError

//StopError Gone Stop flow error.
StopError

//DbRollForPanic error in rollback of DB transaction for panic.
DbRollForPanic

//MustHaveGonerId error for the GonerId is empty.
MustHaveGonerId

//InjectError error for dependence injection error
InjectError

ConfigError
)

func GonerIdIsExistedError(id GonerId) Error {
Expand Down
153 changes: 153 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gone

import (
"errors"
"github.com/stretchr/testify/assert"
"strings"
"testing"
Expand All @@ -12,3 +13,155 @@ func Test_iError_Error(t *testing.T) {

assert.True(t, strings.Contains(s, "Test_iError_Error"))
}

func TestNewBusinessError(t *testing.T) {
type args struct {
msg string
ext []any
}

var data = map[string]any{}
tests := []struct {
name string
args args
want BError
}{
{
name: "single parameter",
args: args{
msg: "error",
ext: []any{},
},
want: BError{
err: NewError(0, "error"),
},
},
{
name: "two parameters",
args: args{
msg: "error",
ext: []any{100},
},
want: BError{
err: NewError(100, "error"),
},
},
{
name: "three parameters",
args: args{
msg: "error",
ext: []any{100, data},
},
want: BError{
err: NewError(100, "error"),
data: data,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want,
*(NewBusinessError(tt.args.msg, tt.args.ext...).(*BError)),
"NewBusinessError(%v, %v)",
tt.args.msg,
tt.args.ext,
)
})
}

businessError := NewBusinessError("error", 100, data)
assert.Equal(t, "error", businessError.Msg())
assert.Equal(t, 100, businessError.Code())
assert.Equal(t, data, businessError.Data())
assert.Equal(t, "GoneError(code=100):error", businessError.Error())
}

func TestNewParameterError(t *testing.T) {
type args struct {
msg string
ext []int
}
tests := []struct {
name string
args args
want defaultErr
}{
{
name: "single parameter",
args: args{
msg: "error",
ext: []int{},
},
want: defaultErr{msg: "error", code: 400},
},
{
name: "single parameter",
args: args{
msg: "error",
ext: []int{401},
},
want: defaultErr{msg: "error", code: 401},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t,
tt.want,
*NewParameterError(tt.args.msg, tt.args.ext...).(*defaultErr),
"NewParameterError(%v, %v)",
tt.args.msg,
tt.args.ext,
)
})
}
}

func TestToError(t *testing.T) {
type args struct {
err error
}
tests := []struct {
name string
args args
want Error
}{
{
name: "err = nil",
args: args{
err: nil,
},
want: nil,
},
{
name: "err is Error",
args: args{
err: NewBusinessError("error", 100),
},
want: NewBusinessError("error", 100),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, ToError(tt.args.err), "ToError(%v)", tt.args.err)
})
}

t.Run("err is not Error", func(t *testing.T) {
err := errors.New("error")
innerError := ToError(err)
assert.Equal(t, 500, innerError.Code())
assert.Equal(t, "error", innerError.Msg())
assert.NotNil(t, innerError.(InnerError).Stack())
})
t.Run("err is string", func(t *testing.T) {
toError := ToError("error")
assert.Equal(t, 500, toError.Code())
assert.Equal(t, "error", toError.Msg())
})

t.Run("err is int", func(t *testing.T) {
toError := ToError(100)
assert.Equal(t, 500, toError.Code())
assert.Equal(t, "100", toError.Msg())
})
}
83 changes: 0 additions & 83 deletions gin_error.go

This file was deleted.

Loading

0 comments on commit 22a986b

Please sign in to comment.