-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return errors instead of panic (#414)
close #390
- Loading branch information
Showing
22 changed files
with
210 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,106 @@ | ||
package client | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
errMissingResponseField missingResponseFieldErr | ||
// ErrMissingServer is returned when server endpoint is empty in parameters. | ||
ErrMissingServer = errors.New("server address is unset or empty") | ||
// ErrNonPositiveTimeout is returned when any timeout is below zero in parameters. | ||
ErrNonPositiveTimeout = errors.New("non-positive timeout") | ||
|
||
// ErrMissingContainer is returned when container is not provided. | ||
ErrMissingContainer = errors.New("missing container") | ||
// ErrMissingObject is returned when object is not provided. | ||
ErrMissingObject = errors.New("missing object") | ||
// ErrMissingAccount is returned when account/owner is not provided. | ||
ErrMissingAccount = errors.New("missing account") | ||
// ErrMissingEACL is returned when eACL table is not provided. | ||
ErrMissingEACL = errors.New("missing eACL table") | ||
// ErrMissingEACLContainer is returned when container info is not provided in eACL table. | ||
ErrMissingEACLContainer = errors.New("missing container in eACL table") | ||
// ErrMissingAnnouncements is returned when announcements are not provided. | ||
ErrMissingAnnouncements = errors.New("missing announcements") | ||
// ErrZeroRangeLength is returned when range parameter has zero length. | ||
ErrZeroRangeLength = errors.New("zero range length") | ||
// ErrMissingRanges is returned when empty ranges list is provided. | ||
ErrMissingRanges = errors.New("missing ranges") | ||
// ErrZeroEpoch is returned when zero epoch is provided. | ||
ErrZeroEpoch = errors.New("zero epoch") | ||
// ErrMissingTrusts is returned when empty slice of trusts is provided. | ||
ErrMissingTrusts = errors.New("missing trusts") | ||
// ErrMissingTrust is returned when empty trust is not provided. | ||
ErrMissingTrust = errors.New("missing trust") | ||
|
||
// ErrUnexpectedReadCall is returned when we already got all data but truing to get more. | ||
ErrUnexpectedReadCall = errors.New("unexpected call to `Read`") | ||
|
||
// ErrSign is returned when unable to sign service message. | ||
ErrSign SignError | ||
|
||
// ErrMissingResponseField is returned when required field is not exists in NeoFS api response. | ||
ErrMissingResponseField MissingResponseFieldErr | ||
) | ||
|
||
type missingResponseFieldErr struct { | ||
// MissingResponseFieldErr contains field name which should be in NeoFS API response. | ||
type MissingResponseFieldErr struct { | ||
name string | ||
} | ||
|
||
func (e missingResponseFieldErr) Error() string { | ||
// Error implements the error interface. | ||
func (e MissingResponseFieldErr) Error() string { | ||
return fmt.Sprintf("missing %s field in the response", e.name) | ||
} | ||
|
||
func (e missingResponseFieldErr) Is(target error) bool { | ||
// Is implements interface for correct checking current error type with [errors.Is]. | ||
func (e MissingResponseFieldErr) Is(target error) bool { | ||
switch target.(type) { | ||
default: | ||
return false | ||
case missingResponseFieldErr, *missingResponseFieldErr: | ||
case MissingResponseFieldErr, *MissingResponseFieldErr: | ||
return true | ||
} | ||
} | ||
|
||
// returns error describing missing field with the given name. | ||
func newErrMissingResponseField(name string) error { | ||
return missingResponseFieldErr{name: name} | ||
return MissingResponseFieldErr{name: name} | ||
} | ||
|
||
// returns error describing invalid field (according to the NeoFS protocol) | ||
// with the given name and format violation err. | ||
func newErrInvalidResponseField(name string, err error) error { | ||
return fmt.Errorf("invalid %s field in the response: %w", name, err) | ||
} | ||
|
||
// SignError wraps another error with reason why sign process was failed. | ||
type SignError struct { | ||
err error | ||
} | ||
|
||
// NewSignError is a constructor for [SignError]. | ||
func NewSignError(err error) SignError { | ||
return SignError{err: err} | ||
} | ||
|
||
// Error implements the error interface. | ||
func (e SignError) Error() string { | ||
return fmt.Sprintf("sign: %v", e.err) | ||
} | ||
|
||
// Unwrap implements the error interface. | ||
func (e SignError) Unwrap() error { | ||
return e.err | ||
} | ||
|
||
// Is implements interface for correct checking current error type with [errors.Is]. | ||
func (e SignError) Is(target error) bool { | ||
switch target.(type) { | ||
default: | ||
return false | ||
case SignError, *SignError: | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package client_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/nspcc-dev/neofs-sdk-go/client" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_SignError(t *testing.T) { | ||
someErr := errors.New("some error") | ||
signErr := client.NewSignError(someErr) | ||
|
||
require.ErrorIs(t, signErr, someErr) | ||
require.ErrorIs(t, signErr, client.ErrSign) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.