Skip to content

Commit

Permalink
Better error handling for storage personalizer uploads.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro committed Oct 23, 2024
1 parent a22e099 commit 1c093aa
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions personalizer_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,20 @@ func rpcStoragePersonalizerUpload(initializer runtime.Initializer, p *StoragePer
return "", ErrSessionUser
}

decoder := json.NewDecoder(strings.NewReader(payload))
decoder.DisallowUnknownFields()

req := &storagePersonalizerUploadRequest{}
err := json.Unmarshal([]byte(payload), req)
if err != nil {

if err := decoder.Decode(req); err != nil {
logger.WithField("error", err.Error()).Error("decoder.Decode error")
if strings.HasPrefix(err.Error(), "json: unknown field") {
return "", runtime.NewError(err.Error(), 3)
}
return "", ErrPayloadDecode
}

writes := []*runtime.StorageWrite{}
writes := make([]*runtime.StorageWrite, 0, 15)

if req.Achievements != nil {
write, err := p.newStorageWrite(req.Achievements, storagePersonalizerKeyAchievements)
Expand Down Expand Up @@ -267,9 +274,11 @@ func rpcStoragePersonalizerUpload(initializer runtime.Initializer, p *StoragePer
writes = append(writes, write)
}

_, err = nk.StorageWrite(ctx, writes)
if err != nil {
return "", err
if len(writes) > 0 {
if _, err := nk.StorageWrite(ctx, writes); err != nil {
logger.WithField("error", err.Error()).Error("nk.StorageWrite error")
return "", err
}
}

return "{}", nil
Expand Down

0 comments on commit 1c093aa

Please sign in to comment.