diff --git a/internal/satori/satori.go b/internal/satori/satori.go index 770b5e5bb..2137835b3 100644 --- a/internal/satori/satori.go +++ b/internal/satori/satori.go @@ -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. @@ -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) } } @@ -408,13 +421,13 @@ 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 @@ -422,6 +435,10 @@ func (s *SatoriClient) ExperimentsList(ctx context.Context, id string, names ... 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) } } @@ -465,14 +482,13 @@ 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 @@ -480,6 +496,10 @@ func (s *SatoriClient) FlagsList(ctx context.Context, id string, names ...string 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) } } @@ -523,13 +543,13 @@ 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 @@ -537,6 +557,9 @@ func (s *SatoriClient) LiveEventsList(ctx context.Context, id string, names ...s 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) } } @@ -585,13 +608,13 @@ 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 @@ -599,6 +622,9 @@ func (s *SatoriClient) MessagesList(ctx context.Context, id string, limit int, f 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) } } @@ -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) } } @@ -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) } }