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 0be795a
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 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

0 comments on commit 0be795a

Please sign in to comment.