Skip to content

Commit

Permalink
refactor: adjust error lock
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Feb 25, 2024
1 parent 0492be9 commit 22f4816
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 31 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ jobs:
strategy:
matrix:
go:
- '1.22'
- '1.21'
- '1.20'
- '1.19'
- '1.18'
- '1.17'
- '1.16'
- '1.15'
- '1.14'
- '1.13'
steps:

- name: Go ${{ matrix.go }} test
Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/vicanso/hes

go 1.12
go 1.19

require github.com/stretchr/testify v1.8.1
require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 2 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
27 changes: 15 additions & 12 deletions http_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type (
// category
Category string `json:"category,omitempty"`
// sub category
SubCategory string `json:"subCategory"`
SubCategory string `json:"subCategory,omitempty"`
// title
Title string `json:"title,omitempty"`
// message
Expand Down Expand Up @@ -168,16 +168,7 @@ func (e *Error) exists(he *Error) bool {
}
return false
}

// Add add error to error list
func (e *Error) Add(errs ...error) {
if len(errs) == 0 {
return
}
if e.lock != nil {
e.lock.Lock()
defer e.lock.Unlock()
}
func (e *Error) add(errs ...error) {
if len(e.Errs) == 0 {
e.Errs = make([]*Error, 0)
}
Expand All @@ -189,7 +180,7 @@ func (e *Error) Add(errs ...error) {
// 如果包括子错误,则直接添加子错误列表
if he.IsNotEmpty() {
for _, err := range he.Errs {
e.Add(err)
e.add(err)
}
continue
}
Expand All @@ -203,6 +194,18 @@ func (e *Error) Add(errs ...error) {
}
}

// Add add error to error list
func (e *Error) Add(errs ...error) {
if len(errs) == 0 {
return
}
if e.lock != nil {
e.lock.Lock()
defer e.lock.Unlock()
}
e.add(errs...)
}

// Clone clone error
func (e *Error) Clone() *Error {
he := new(Error)
Expand Down
4 changes: 2 additions & 2 deletions http_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestNewHTTPError(t *testing.T) {
he.Add(he1, nil)
assert.False(he.IsEmpty())
assert.True(he.IsNotEmpty())
assert.Equal("{\"statusCode\":400,\"subCategory\":\"\",\"message\":\"message\",\"errs\":[{\"statusCode\":400,\"subCategory\":\"\",\"message\":\"messsage1\"}]}", string(he.ToJSON()))
assert.Equal("{\"statusCode\":400,\"message\":\"message\",\"errs\":[{\"statusCode\":400,\"message\":\"messsage1\"}]}", string(he.ToJSON()))
assert.Equal("statusCode=400, message=message, errs:(statusCode=400, message=messsage1)", he.Error())

he2 := New("message2")
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestToJSON(t *testing.T) {
"a": 1,
"b": "2",
}
str := fmt.Sprintf(`{"statusCode":500,"code":"code-001","category":"cat","subCategory":"","message":"my error","exception":true,"file":"%s","line":%d,"extra":{"a":1,"b":"2"}}`, he.File, he.Line)
str := fmt.Sprintf(`{"statusCode":500,"code":"code-001","category":"cat","message":"my error","exception":true,"file":"%s","line":%d,"extra":{"a":1,"b":"2"}}`, he.File, he.Line)
assert.Equal(str, string(he.ToJSON()))
}

Expand Down

0 comments on commit 22f4816

Please sign in to comment.