Skip to content

Commit

Permalink
handle the case with no input data
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkleeman committed Jul 6, 2024
1 parent 2239713 commit 990bf71
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ func NewServiceHandler[I any, O any](fn ServiceHandlerFn[I, O]) *ServiceHandler
func (h *ServiceHandler) Call(ctx Context, bytes []byte) ([]byte, error) {
input := reflect.New(h.input)

if err := json.Unmarshal(bytes, input.Interface()); err != nil {
return nil, TerminalError(fmt.Errorf("request doesn't match handler signature: %w", err))
if len(bytes) > 0 {
// use the zero value if there is no input data at all
if err := json.Unmarshal(bytes, input.Interface()); err != nil {
return nil, TerminalError(fmt.Errorf("request doesn't match handler signature: %w", err))
}
}

// we are sure about the fn signature so it's safe to do this
Expand Down Expand Up @@ -80,8 +83,11 @@ func NewObjectHandler[I any, O any](fn ObjectHandlerFn[I, O]) *ObjectHandler {
func (h *ObjectHandler) Call(ctx ObjectContext, bytes []byte) ([]byte, error) {
input := reflect.New(h.input)

if err := json.Unmarshal(bytes, input.Interface()); err != nil {
return nil, TerminalError(fmt.Errorf("request doesn't match handler signature: %w", err))
if len(bytes) > 0 {
// use the zero value if there is no input data at all
if err := json.Unmarshal(bytes, input.Interface()); err != nil {
return nil, TerminalError(fmt.Errorf("request doesn't match handler signature: %w", err))
}
}

// we are sure about the fn signature so it's safe to do this
Expand Down

0 comments on commit 990bf71

Please sign in to comment.