fix #326 Differentiating between actor state key not found vs. unexpected error (#333)

* return the specific error type of the actor state, caused by empty key or unmarshal fail

Signed-off-by: wangxw <996268132@qq.com>

* Update state_async_provider.go

change log level of state load

Signed-off-by: wXwcoder <996268132@qq.com>

Signed-off-by: wangxw <996268132@qq.com>
Signed-off-by: wXwcoder <996268132@qq.com>
This commit is contained in:
wXwcoder 2022-11-14 13:19:34 +08:00 committed by GitHub
parent c2dfec6abf
commit 014b4d6836
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 2 deletions

View File

@ -15,6 +15,7 @@ package state
import (
"context"
"log"
"github.com/pkg/errors"
@ -23,6 +24,11 @@ import (
client "github.com/dapr/go-sdk/client"
)
var (
ErrStateEmpty = errors.New("get actor state result empty")
ErrStateUnmarshal = errors.New("unmarshal state data error")
)
type DaprStateAsyncProvider struct {
daprClient client.Client
stateSerializer codec.Codec
@ -50,10 +56,12 @@ func (d *DaprStateAsyncProvider) Load(actorType, actorID, stateName string, repl
return errors.Errorf("get actor state error = %s", err.Error())
}
if len(result.Data) == 0 {
return errors.Errorf("get actor state result empty, with actorType: %s, actorID: %s, stateName %s", actorType, actorID, stateName)
log.Errorf("get actor state result empty, with actorType: %s, actorID: %s, stateName %s", actorType, actorID, stateName)
return ErrStateEmpty
}
if err := d.stateSerializer.Unmarshal(result.Data, reply); err != nil {
return errors.Errorf("unmarshal state data error = %s", err.Error())
log.Errorf("unmarshal state data error = %s", err.Error())
return ErrStateUnmarshal
}
return nil
}