Skip to content

Commit

Permalink
Avoid ominous 'unreachable' errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkleeman committed Jul 8, 2024
1 parent 03d8276 commit 988d15c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
5 changes: 5 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (

type Code uint16

const (
ErrJournalMismatch Code = 570
ErrProtocolViolation Code = 571
)

type codeError struct {
code Code
inner error
Expand Down
2 changes: 1 addition & 1 deletion internal/state/awakeable.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *Machine) awakeable() (restate.Awakeable[[]byte], error) {
case *protocol.AwakeableEntryMessage_Failure:
return completedAwakeable[[]byte]{invocationID: c.id, result: restate.Result[[]byte]{Err: restate.TerminalError(fmt.Errorf(result.Failure.Message), restate.Code(result.Failure.Code))}}, nil
default:
panic("unreachable")
return nil, restate.TerminalError(fmt.Errorf("awakeable entry had invalid result: %v", entry.Payload.Result), restate.ErrProtocolViolation)
}
},
func() (awakeable[[]byte], error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/state/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (m *Machine) doCall(service, key, method string, params []byte) ([]byte, er
return result.Value, nil
}

return nil, errUnreachable
return nil, restate.TerminalError(fmt.Errorf("sync call entry had invalid result: %v", entry.Payload.Result), restate.ErrProtocolViolation)
}, func() ([]byte, error) {
return m._doCall(service, key, method, params)
})
Expand Down Expand Up @@ -129,7 +129,7 @@ func (m *Machine) _doCall(service, key, method string, params []byte) ([]byte, e
return result.Value, nil
}

return nil, errUnreachable
return nil, restate.TerminalError(fmt.Errorf("sync call completion had invalid result: %v", completion.Payload.Result), restate.ErrProtocolViolation)
}

func (c *Machine) sendCall(service, key, method string, body any, delay time.Duration) error {
Expand Down
10 changes: 5 additions & 5 deletions internal/state/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

var (
errEntryMismatch = restate.WithErrorCode(fmt.Errorf("log entry mismatch"), 32)
errUnreachable = fmt.Errorf("unreachable")
)

func (m *Machine) set(key string, value []byte) error {
Expand Down Expand Up @@ -126,7 +125,7 @@ func (m *Machine) get(key string) ([]byte, error) {
return result.Value, nil
}

return nil, fmt.Errorf("unreachable")
return nil, restate.TerminalError(fmt.Errorf("get state entry had invalid result: %v", entry.Payload.Result), restate.ErrProtocolViolation)
}, func() ([]byte, error) {
return m._get(key)
})
Expand Down Expand Up @@ -199,13 +198,14 @@ func (m *Machine) _get(key string) ([]byte, error) {
case *protocol.CompletionMessage_Failure:
// the get state entry message is not failable so this should
// never happen
// TODO terminal?
return nil, fmt.Errorf("[%d] %s", value.Failure.Code, value.Failure.Message)
case *protocol.CompletionMessage_Value:
m.current[key] = value.Value
return value.Value, nil
}

return nil, fmt.Errorf("unreachable")
return nil, restate.TerminalError(fmt.Errorf("get state completion had invalid result: %v", completion.Payload.Result), restate.ErrProtocolViolation)
}

func (m *Machine) keys() ([]string, error) {
Expand All @@ -224,7 +224,7 @@ func (m *Machine) keys() ([]string, error) {
return keys, nil
}

return nil, errUnreachable
return nil, restate.TerminalError(fmt.Errorf("found get state keys entry with invalid completion: %v", entry.Payload.Result), 571)
},
m._keys,
)
Expand Down Expand Up @@ -332,7 +332,7 @@ func (m *Machine) sideEffect(fn func() ([]byte, error)) ([]byte, error) {
return result.Value, nil
}

return nil, errUnreachable
return nil, restate.TerminalError(fmt.Errorf("side effect entry had invalid result: %v", entry.Payload.Result), restate.ErrProtocolViolation)
},
func() ([]byte, error) {
return m._sideEffect(fn)
Expand Down

0 comments on commit 988d15c

Please sign in to comment.