Adds ability to unwrap ipc errors into their original type
This only works for a specific whitelist of error types, which is currently all errors in the storagedriver package. Also improves storagedriver tests to enforce proper error types are returned
This commit is contained in:
parent
1e8f0ce50a
commit
a3481c5f1c
5 changed files with 85 additions and 21 deletions
|
@ -126,7 +126,7 @@ func (driver *StorageDriverClient) Start() error {
|
|||
}
|
||||
|
||||
if response.Error != nil {
|
||||
return response.Error
|
||||
return response.Error.Unwrap()
|
||||
}
|
||||
|
||||
driver.version = response.Version
|
||||
|
@ -194,7 +194,7 @@ func (driver *StorageDriverClient) GetContent(path string) ([]byte, error) {
|
|||
}
|
||||
|
||||
if response.Error != nil {
|
||||
return nil, response.Error
|
||||
return nil, response.Error.Unwrap()
|
||||
}
|
||||
|
||||
defer response.Reader.Close()
|
||||
|
@ -226,7 +226,7 @@ func (driver *StorageDriverClient) PutContent(path string, contents []byte) erro
|
|||
}
|
||||
|
||||
if response.Error != nil {
|
||||
return response.Error
|
||||
return response.Error.Unwrap()
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -253,7 +253,7 @@ func (driver *StorageDriverClient) ReadStream(path string, offset uint64) (io.Re
|
|||
}
|
||||
|
||||
if response.Error != nil {
|
||||
return nil, response.Error
|
||||
return nil, response.Error.Unwrap()
|
||||
}
|
||||
|
||||
return response.Reader, nil
|
||||
|
@ -280,7 +280,7 @@ func (driver *StorageDriverClient) WriteStream(path string, offset, size uint64,
|
|||
}
|
||||
|
||||
if response.Error != nil {
|
||||
return response.Error
|
||||
return response.Error.Unwrap()
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -307,7 +307,7 @@ func (driver *StorageDriverClient) CurrentSize(path string) (uint64, error) {
|
|||
}
|
||||
|
||||
if response.Error != nil {
|
||||
return 0, response.Error
|
||||
return 0, response.Error.Unwrap()
|
||||
}
|
||||
|
||||
return response.Position, nil
|
||||
|
@ -334,7 +334,7 @@ func (driver *StorageDriverClient) List(path string) ([]string, error) {
|
|||
}
|
||||
|
||||
if response.Error != nil {
|
||||
return nil, response.Error
|
||||
return nil, response.Error.Unwrap()
|
||||
}
|
||||
|
||||
return response.Keys, nil
|
||||
|
@ -361,7 +361,7 @@ func (driver *StorageDriverClient) Move(sourcePath string, destPath string) erro
|
|||
}
|
||||
|
||||
if response.Error != nil {
|
||||
return response.Error
|
||||
return response.Error.Unwrap()
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -387,7 +387,7 @@ func (driver *StorageDriverClient) Delete(path string) error {
|
|||
}
|
||||
|
||||
if response.Error != nil {
|
||||
return response.Error
|
||||
return response.Error.Unwrap()
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -37,9 +37,13 @@ type Request struct {
|
|||
}
|
||||
|
||||
// ResponseError is a serializable error type.
|
||||
// The Type and Parameters may be used to reconstruct the same error on the
|
||||
// client side, falling back to using the Type and Message if this cannot be
|
||||
// done.
|
||||
type ResponseError struct {
|
||||
Type string
|
||||
Message string
|
||||
Type string
|
||||
Message string
|
||||
Parameters map[string]interface{}
|
||||
}
|
||||
|
||||
// WrapError wraps an error in a serializable struct containing the error's type
|
||||
|
@ -48,10 +52,52 @@ func WrapError(err error) *ResponseError {
|
|||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return &ResponseError{
|
||||
Type: reflect.TypeOf(err).String(),
|
||||
v := reflect.ValueOf(err)
|
||||
re := ResponseError{
|
||||
Type: v.Type().String(),
|
||||
Message: err.Error(),
|
||||
}
|
||||
|
||||
if v.Kind() == reflect.Struct {
|
||||
re.Parameters = make(map[string]interface{})
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
field := v.Type().Field(i)
|
||||
re.Parameters[field.Name] = v.Field(i).Interface()
|
||||
}
|
||||
}
|
||||
return &re
|
||||
}
|
||||
|
||||
// Unwrap returns the underlying error if it can be reconstructed, or the
|
||||
// original ResponseError otherwise.
|
||||
func (err *ResponseError) Unwrap() error {
|
||||
var errVal reflect.Value
|
||||
var zeroVal reflect.Value
|
||||
|
||||
switch err.Type {
|
||||
case "storagedriver.PathNotFoundError":
|
||||
errVal = reflect.ValueOf(&storagedriver.PathNotFoundError{})
|
||||
case "storagedriver.InvalidOffsetError":
|
||||
errVal = reflect.ValueOf(&storagedriver.InvalidOffsetError{})
|
||||
}
|
||||
if errVal == zeroVal {
|
||||
return err
|
||||
}
|
||||
|
||||
for k, v := range err.Parameters {
|
||||
fieldVal := errVal.Elem().FieldByName(k)
|
||||
if fieldVal == zeroVal {
|
||||
return err
|
||||
}
|
||||
fieldVal.Set(reflect.ValueOf(v))
|
||||
}
|
||||
|
||||
if unwrapped, ok := errVal.Elem().Interface().(error); ok {
|
||||
return unwrapped
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func (err *ResponseError) Error() string {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue