Skip to content

Commit

Permalink
Improve Satori client error handling.
Browse files Browse the repository at this point in the history
Add warning logs if Satori request context values are not found.
  • Loading branch information
sesposito committed Nov 13, 2024
1 parent 55ec6b6 commit 1f083ce
Showing 1 changed file with 56 additions and 22 deletions.
78 changes: 56 additions & 22 deletions internal/satori/satori.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,23 @@ func (stc *sessionTokenClaims) Valid() error {
}

func (s *SatoriClient) generateToken(ctx context.Context, id string) (string, error) {
tid, _ := ctx.Value(ctxkeys.TokenIDKey{}).(string)
tIssuedAt, _ := ctx.Value(ctxkeys.TokenIssuedAtKey{}).(int64)
tExpirySec, _ := ctx.Value(ctxkeys.ExpiryKey{}).(int64)
tid, ok := ctx.Value(ctxkeys.TokenIDKey{}).(string)
if !ok {
s.logger.Warn("satori request token id was not found in ctx")
}
tIssuedAt, ok := ctx.Value(ctxkeys.TokenIssuedAtKey{}).(int64)
if !ok {
s.logger.Warn("satori request token issued at was not found in ctx")
}
tExpirySec, ok := ctx.Value(ctxkeys.ExpiryKey{}).(int64)
if !ok {
s.logger.Warn("satori request token expires at was not found in ctx")
}

timestamp := time.Now().UTC()
if tIssuedAt == 0 && tExpirySec > s.nakamaTokenExpirySec {
// Token was issued before 'IssuedAt' had been added to the session token.
// Thus Nakama will make a guess of that value.
// Thus, Nakama will make a guess of that value.
tIssuedAt = tExpirySec - s.nakamaTokenExpirySec
} else if tIssuedAt == 0 {
// Unable to determine the token's issued at.
Expand Down Expand Up @@ -364,6 +373,10 @@ func (s *SatoriClient) EventsPublish(ctx context.Context, id string, events []*r
case 200:
return nil
default:
errBody, err := io.ReadAll(res.Body)
if err == nil && len(errBody) > 0 {
return fmt.Errorf("%d status code: %s", res.StatusCode, string(errBody))
}
return fmt.Errorf("%d status code", res.StatusCode)
}
}
Expand Down Expand Up @@ -408,20 +421,24 @@ func (s *SatoriClient) ExperimentsList(ctx context.Context, id string, names ...

defer res.Body.Close()

resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

switch res.StatusCode {
case 200:
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

var experiments runtime.ExperimentList
if err = json.Unmarshal(resBody, &experiments); err != nil {
return nil, err
}

return &experiments, nil
default:
if len(resBody) > 0 {
return nil, fmt.Errorf("%d status code: %s", res.StatusCode, string(resBody))
}

return nil, fmt.Errorf("%d status code", res.StatusCode)
}
}
Expand Down Expand Up @@ -465,21 +482,24 @@ func (s *SatoriClient) FlagsList(ctx context.Context, id string, names ...string
}

defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

switch res.StatusCode {
case 200:
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

var flags runtime.FlagList
if err = json.Unmarshal(resBody, &flags); err != nil {
return nil, err
}

return &flags, nil
default:
if len(resBody) > 0 {
return nil, fmt.Errorf("%d status code: %s", res.StatusCode, string(resBody))
}

return nil, fmt.Errorf("%d status code", res.StatusCode)
}
}
Expand Down Expand Up @@ -523,20 +543,23 @@ func (s *SatoriClient) LiveEventsList(ctx context.Context, id string, names ...s
}

defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

switch res.StatusCode {
case 200:
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
var liveEvents runtime.LiveEventList
if err = json.Unmarshal(resBody, &liveEvents); err != nil {
return nil, err
}

return &liveEvents, nil
default:
if len(resBody) > 0 {
return nil, fmt.Errorf("%d status code: %s", res.StatusCode, string(resBody))
}
return nil, fmt.Errorf("%d status code", res.StatusCode)
}
}
Expand Down Expand Up @@ -585,20 +608,23 @@ func (s *SatoriClient) MessagesList(ctx context.Context, id string, limit int, f
}

defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

switch res.StatusCode {
case 200:
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
var messages runtime.MessageList
if err = json.Unmarshal(resBody, &messages); err != nil {
return nil, err
}

return &messages, nil
default:
if len(resBody) > 0 {
return nil, fmt.Errorf("%d status code: %s", res.StatusCode, string(resBody))
}
return nil, fmt.Errorf("%d status code", res.StatusCode)
}
}
Expand Down Expand Up @@ -648,6 +674,10 @@ func (s *SatoriClient) MessageUpdate(ctx context.Context, id, messageId string,
case 200:
return nil
default:
errBody, err := io.ReadAll(res.Body)
if err == nil && len(errBody) > 0 {
return fmt.Errorf("%d status code: %s", res.StatusCode, string(errBody))
}
return fmt.Errorf("%d status code", res.StatusCode)
}
}
Expand Down Expand Up @@ -691,6 +721,10 @@ func (s *SatoriClient) MessageDelete(ctx context.Context, id, messageId string)
case 200:
return nil
default:
errBody, err := io.ReadAll(res.Body)
if err == nil && len(errBody) > 0 {
return fmt.Errorf("%d status code: %s", res.StatusCode, string(errBody))
}
return fmt.Errorf("%d status code", res.StatusCode)
}
}

0 comments on commit 1f083ce

Please sign in to comment.