Make storiface.CallError json-friendly

This commit is contained in:
Łukasz Magiera 2020-11-17 16:28:41 +01:00
parent b8853aa4d5
commit b242d69805
2 changed files with 16 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package storiface
import (
"context"
"errors"
"fmt"
"io"
"time"
@ -104,22 +105,29 @@ const (
)
type CallError struct {
Code ErrorCode
Sub error
Code ErrorCode
Message string
sub error
}
func (c *CallError) Error() string {
return fmt.Sprintf("storage call error %d: %s", c.Code, c.Sub.Error())
return fmt.Sprintf("storage call error %d: %s", c.Code, c.Message)
}
func (c *CallError) Unwrap() error {
return c.Sub
if c.sub != nil {
return c.sub
}
return errors.New(c.Message)
}
func Err(code ErrorCode, sub error) *CallError {
return &CallError{
Code: code,
Sub: sub,
Code: code,
Message: sub.Error(),
sub: sub,
}
}

View File

@ -90,10 +90,7 @@ func newLocalWorker(executor ExecutorFunc, wcfg WorkerConfig, store stores.Store
go func() {
for _, call := range unfinished {
err := &storiface.CallError{
Sub: xerrors.New("worker restarted"),
Code: storiface.ErrTempWorkerRestart,
}
err := storiface.Err(storiface.ErrTempWorkerRestart, xerrors.New("worker restarted"))
// TODO: Handle restarting PC1 once support is merged
@ -261,10 +258,7 @@ func (l *LocalWorker) asyncCall(ctx context.Context, sector storage.SectorRef, r
func toCallError(err error) *storiface.CallError {
var serr *storiface.CallError
if err != nil && !xerrors.As(err, &serr) {
serr = &storiface.CallError{
Sub: err,
Code: storiface.ErrUnknown,
}
serr = storiface.Err(storiface.ErrUnknown, err)
}
return serr