Skip to content

Commit

Permalink
feat(context): add json marshal error handler to error bubble
Browse files Browse the repository at this point in the history
  • Loading branch information
bastean committed May 26, 2024
1 parent 77eb4a9 commit 68819fe
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
11 changes: 8 additions & 3 deletions pkg/context/shared/domain/serror/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ func (err *Bubble) Error() string {
message := fmt.Sprintf("%s (%s): %s", err.When.Format(time.RFC3339Nano), err.Where, err.What)

if err.Why != nil {
why, _ := json.Marshal(err.Why)
why, err := json.Marshal(err.Why)

if err != nil {
sservice.PanicOnError("Error", fmt.Sprintf("cannot json encoding why from error bubble: %s: [%s]", message, err.Error()))
}

message = fmt.Sprintf("%s: %s", message, why)
}

Expand All @@ -35,11 +40,11 @@ func (err *Bubble) Error() string {

func NewBubble(where, what string, why Meta, who error) *Bubble {
if where == "" {
sservice.PanicOnError("NewBubble", "cannot create a bubble if where is not defined")
sservice.PanicOnError("NewBubble", "cannot create a error bubble if where is not defined")
}

if what == "" {
sservice.PanicOnError("NewBubble", "cannot create a bubble if what is not defined")
sservice.PanicOnError("NewBubble", "cannot create a error bubble if what is not defined")
}

return &Bubble{
Expand Down
2 changes: 1 addition & 1 deletion pkg/context/shared/domain/smodel/queryHandler.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package smodel

type QueryHandler[Query any, Response any] interface {
type QueryHandler[Query, Response any] interface {
Handle(Query) (Response, error)
}
2 changes: 1 addition & 1 deletion pkg/context/shared/domain/smodel/useCase.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package smodel

type UseCase[Input any, Output any] interface {
type UseCase[Input, Output any] interface {
Run(Input) (Output, error)
}
10 changes: 2 additions & 8 deletions pkg/context/user/application/read/query.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ import (
"github.com/bastean/codexgo/pkg/context/user/domain/valueobj"
)

type Input struct {
Id smodel.ValueObject[string]
}

type QueryHandler struct {
smodel.UseCase[*Input, *aggregate.User]
smodel.UseCase[smodel.ValueObject[string], *aggregate.User]
}

func (handler *QueryHandler) Handle(query *Query) (*Response, error) {
Expand All @@ -22,9 +18,7 @@ func (handler *QueryHandler) Handle(query *Query) (*Response, error) {
return nil, serror.BubbleUp(err, "Handle")
}

user, err := handler.UseCase.Run(&Input{
Id: id,
})
user, err := handler.UseCase.Run(id)

if err != nil {
return nil, serror.BubbleUp(err, "Handle")
Expand Down
2 changes: 1 addition & 1 deletion pkg/context/user/application/read/query.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type UserReadTestSuite struct {
suite.Suite
sut smodel.QueryHandler[*read.Query, *read.Response]
useCase smodel.UseCase[*read.Input, *aggregate.User]
useCase smodel.UseCase[smodel.ValueObject[string], *aggregate.User]
repository *persistence.RepositoryMock
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/context/user/application/read/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package read

import (
"github.com/bastean/codexgo/pkg/context/shared/domain/serror"
"github.com/bastean/codexgo/pkg/context/shared/domain/smodel"
"github.com/bastean/codexgo/pkg/context/user/domain/aggregate"
"github.com/bastean/codexgo/pkg/context/user/domain/model"
)
Expand All @@ -10,9 +11,9 @@ type Read struct {
model.Repository
}

func (read *Read) Run(input *Input) (*aggregate.User, error) {
func (read *Read) Run(id smodel.ValueObject[string]) (*aggregate.User, error) {
user, err := read.Repository.Search(model.RepositorySearchCriteria{
Id: input.Id,
Id: id,
})

if err != nil {
Expand Down

0 comments on commit 68819fe

Please sign in to comment.