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 ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"time" "time"
@ -105,21 +106,28 @@ const (
type CallError struct { type CallError struct {
Code ErrorCode Code ErrorCode
Sub error Message string
sub error
} }
func (c *CallError) Error() string { 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 { 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 { func Err(code ErrorCode, sub error) *CallError {
return &CallError{ return &CallError{
Code: code, Code: code,
Sub: sub, Message: sub.Error(),
sub: sub,
} }
} }

View File

@ -90,10 +90,7 @@ func newLocalWorker(executor ExecutorFunc, wcfg WorkerConfig, store stores.Store
go func() { go func() {
for _, call := range unfinished { for _, call := range unfinished {
err := &storiface.CallError{ err := storiface.Err(storiface.ErrTempWorkerRestart, xerrors.New("worker restarted"))
Sub: xerrors.New("worker restarted"),
Code: storiface.ErrTempWorkerRestart,
}
// TODO: Handle restarting PC1 once support is merged // 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 { func toCallError(err error) *storiface.CallError {
var serr *storiface.CallError var serr *storiface.CallError
if err != nil && !xerrors.As(err, &serr) { if err != nil && !xerrors.As(err, &serr) {
serr = &storiface.CallError{ serr = storiface.Err(storiface.ErrUnknown, err)
Sub: err,
Code: storiface.ErrUnknown,
}
} }
return serr return serr