2021-11-16 18:15:56 +00:00
|
|
|
package apistatus
|
|
|
|
|
|
|
|
import (
|
2022-01-25 12:37:30 +00:00
|
|
|
"encoding/binary"
|
2023-05-16 08:21:59 +00:00
|
|
|
"errors"
|
2022-01-25 12:37:30 +00:00
|
|
|
|
2021-11-16 18:15:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/status"
|
|
|
|
)
|
|
|
|
|
2023-05-16 08:21:59 +00:00
|
|
|
// Error describes common error which is a grouping type for any [apistatus] errors. Any [apistatus] error may be checked
|
|
|
|
// explicitly via it's type of just check the group via errors.Is(err, [apistatus.Error]).
|
|
|
|
var Error = errors.New("api error")
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrServerInternal is an instance of ServerInternal error status. It's expected to be used for [errors.Is]
|
|
|
|
// and MUST NOT be changed.
|
|
|
|
ErrServerInternal ServerInternal
|
|
|
|
// ErrWrongMagicNumber is an instance of WrongMagicNumber error status. It's expected to be used for [errors.Is]
|
|
|
|
// and MUST NOT be changed.
|
|
|
|
ErrWrongMagicNumber WrongMagicNumber
|
|
|
|
// ErrSignatureVerification is an instance of SignatureVerification error status. It's expected to be used for [errors.Is]
|
|
|
|
// and MUST NOT be changed.
|
|
|
|
ErrSignatureVerification SignatureVerification
|
|
|
|
// ErrNodeUnderMaintenance is an instance of NodeUnderMaintenance error status. It's expected to be used for [errors.Is]
|
|
|
|
// and MUST NOT be changed.
|
|
|
|
ErrNodeUnderMaintenance NodeUnderMaintenance
|
|
|
|
)
|
|
|
|
|
2021-11-16 18:15:56 +00:00
|
|
|
// ServerInternal describes failure statuses related to internal server errors.
|
2023-05-05 06:14:38 +00:00
|
|
|
// Instances provide [Status], [StatusV2] and error interfaces.
|
2021-11-16 18:15:56 +00:00
|
|
|
//
|
|
|
|
// The status is purely informative, the client should not go into details of the error except for debugging needs.
|
|
|
|
type ServerInternal struct {
|
|
|
|
v2 status.Status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x ServerInternal) Error() string {
|
|
|
|
return errMessageStatusV2(
|
|
|
|
globalizeCodeV2(status.Internal, status.GlobalizeCommonFail),
|
|
|
|
x.v2.Message(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-16 08:21:59 +00:00
|
|
|
// Is implements interface for correct checking current error type with [errors.Is].
|
|
|
|
func (x ServerInternal) Is(target error) bool {
|
|
|
|
switch target.(type) {
|
|
|
|
default:
|
2023-05-16 11:44:58 +00:00
|
|
|
return errors.Is(Error, target)
|
2023-05-16 08:21:59 +00:00
|
|
|
case ServerInternal, *ServerInternal:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 11:55:21 +00:00
|
|
|
// implements local interface defined in FromStatusV2 func.
|
2021-11-16 18:15:56 +00:00
|
|
|
func (x *ServerInternal) fromStatusV2(st *status.Status) {
|
|
|
|
x.v2 = *st
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStatusV2 implements StatusV2 interface method.
|
|
|
|
// If the value was returned by FromStatusV2, returns the source message.
|
|
|
|
// Otherwise, returns message with
|
2022-08-24 14:17:40 +00:00
|
|
|
// - code: INTERNAL;
|
|
|
|
// - string message: empty;
|
|
|
|
// - details: empty.
|
2021-11-16 18:15:56 +00:00
|
|
|
func (x ServerInternal) ToStatusV2() *status.Status {
|
|
|
|
x.v2.SetCode(globalizeCodeV2(status.Internal, status.GlobalizeCommonFail))
|
|
|
|
return &x.v2
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMessage sets message describing internal error.
|
|
|
|
//
|
|
|
|
// Message should be used for debug purposes only.
|
|
|
|
func (x *ServerInternal) SetMessage(msg string) {
|
|
|
|
x.v2.SetMessage(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message returns message describing internal server error.
|
|
|
|
//
|
|
|
|
// Message should be used for debug purposes only. By default, it is empty.
|
|
|
|
func (x ServerInternal) Message() string {
|
|
|
|
return x.v2.Message()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteInternalServerErr writes err message to ServerInternal instance.
|
|
|
|
func WriteInternalServerErr(x *ServerInternal, err error) {
|
|
|
|
x.SetMessage(err.Error())
|
|
|
|
}
|
2022-01-25 12:37:30 +00:00
|
|
|
|
|
|
|
// WrongMagicNumber describes failure status related to incorrect network magic.
|
2023-05-05 06:14:38 +00:00
|
|
|
// Instances provide [Status], [StatusV2] and error interfaces.
|
2022-01-25 12:37:30 +00:00
|
|
|
type WrongMagicNumber struct {
|
|
|
|
v2 status.Status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x WrongMagicNumber) Error() string {
|
|
|
|
return errMessageStatusV2(
|
|
|
|
globalizeCodeV2(status.WrongMagicNumber, status.GlobalizeCommonFail),
|
|
|
|
x.v2.Message(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-16 08:21:59 +00:00
|
|
|
// Is implements interface for correct checking current error type with [errors.Is].
|
|
|
|
func (x WrongMagicNumber) Is(target error) bool {
|
|
|
|
switch target.(type) {
|
|
|
|
default:
|
2023-05-16 11:44:58 +00:00
|
|
|
return errors.Is(Error, target)
|
2023-05-16 08:21:59 +00:00
|
|
|
case WrongMagicNumber, *WrongMagicNumber:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 11:55:21 +00:00
|
|
|
// implements local interface defined in FromStatusV2 func.
|
2022-01-25 12:37:30 +00:00
|
|
|
func (x *WrongMagicNumber) fromStatusV2(st *status.Status) {
|
|
|
|
x.v2 = *st
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStatusV2 implements StatusV2 interface method.
|
|
|
|
// If the value was returned by FromStatusV2, returns the source message.
|
|
|
|
// Otherwise, returns message with
|
2022-08-24 14:17:40 +00:00
|
|
|
// - code: WRONG_MAGIC_NUMBER;
|
|
|
|
// - string message: empty;
|
|
|
|
// - details: empty.
|
2022-01-25 12:37:30 +00:00
|
|
|
func (x WrongMagicNumber) ToStatusV2() *status.Status {
|
|
|
|
x.v2.SetCode(globalizeCodeV2(status.WrongMagicNumber, status.GlobalizeCommonFail))
|
|
|
|
return &x.v2
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteCorrectMagic writes correct network magic.
|
|
|
|
func (x *WrongMagicNumber) WriteCorrectMagic(magic uint64) {
|
|
|
|
// serialize the number
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(buf, magic)
|
|
|
|
|
|
|
|
// create corresponding detail
|
|
|
|
var d status.Detail
|
|
|
|
|
|
|
|
d.SetID(status.DetailIDCorrectMagic)
|
|
|
|
d.SetValue(buf)
|
|
|
|
|
|
|
|
// attach the detail
|
2022-03-11 09:16:08 +00:00
|
|
|
x.v2.AppendDetails(d)
|
2022-01-25 12:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CorrectMagic returns network magic returned by the server.
|
|
|
|
// Second value indicates presence status:
|
2022-08-24 14:17:40 +00:00
|
|
|
// - -1 if number is presented in incorrect format
|
|
|
|
// - 0 if number is not presented
|
|
|
|
// - +1 otherwise
|
2022-01-25 12:37:30 +00:00
|
|
|
func (x WrongMagicNumber) CorrectMagic() (magic uint64, ok int8) {
|
|
|
|
x.v2.IterateDetails(func(d *status.Detail) bool {
|
|
|
|
if d.ID() == status.DetailIDCorrectMagic {
|
|
|
|
if val := d.Value(); len(val) == 8 {
|
|
|
|
magic = binary.BigEndian.Uint64(val)
|
|
|
|
ok = 1
|
|
|
|
} else {
|
|
|
|
ok = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok != 0
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2022-06-16 21:04:23 +00:00
|
|
|
|
|
|
|
// SignatureVerification describes failure status related to signature verification.
|
2023-05-05 06:14:38 +00:00
|
|
|
// Instances provide [Status], [StatusV2] and error interfaces.
|
2022-06-16 21:04:23 +00:00
|
|
|
type SignatureVerification struct {
|
|
|
|
v2 status.Status
|
|
|
|
}
|
|
|
|
|
2022-12-08 16:34:52 +00:00
|
|
|
const defaultSignatureVerificationMsg = "signature verification failed"
|
|
|
|
|
2022-06-16 21:04:23 +00:00
|
|
|
func (x SignatureVerification) Error() string {
|
2022-12-08 16:34:52 +00:00
|
|
|
msg := x.v2.Message()
|
|
|
|
if msg == "" {
|
|
|
|
msg = defaultSignatureVerificationMsg
|
|
|
|
}
|
|
|
|
|
2022-06-16 21:04:23 +00:00
|
|
|
return errMessageStatusV2(
|
|
|
|
globalizeCodeV2(status.SignatureVerificationFail, status.GlobalizeCommonFail),
|
2022-12-08 16:34:52 +00:00
|
|
|
msg,
|
2022-06-16 21:04:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-16 08:21:59 +00:00
|
|
|
// Is implements interface for correct checking current error type with [errors.Is].
|
|
|
|
func (x SignatureVerification) Is(target error) bool {
|
|
|
|
switch target.(type) {
|
|
|
|
default:
|
2023-05-16 11:44:58 +00:00
|
|
|
return errors.Is(Error, target)
|
2023-05-16 08:21:59 +00:00
|
|
|
case SignatureVerification, *SignatureVerification:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 21:04:23 +00:00
|
|
|
// implements local interface defined in FromStatusV2 func.
|
|
|
|
func (x *SignatureVerification) fromStatusV2(st *status.Status) {
|
|
|
|
x.v2 = *st
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStatusV2 implements StatusV2 interface method.
|
|
|
|
// If the value was returned by FromStatusV2, returns the source message.
|
|
|
|
// Otherwise, returns message with
|
2022-08-24 14:17:40 +00:00
|
|
|
// - code: SIGNATURE_VERIFICATION_FAIL;
|
|
|
|
// - string message: written message via SetMessage or
|
|
|
|
// "signature verification failed" as a default message;
|
|
|
|
// - details: empty.
|
2022-06-16 21:04:23 +00:00
|
|
|
func (x SignatureVerification) ToStatusV2() *status.Status {
|
|
|
|
x.v2.SetCode(globalizeCodeV2(status.SignatureVerificationFail, status.GlobalizeCommonFail))
|
|
|
|
|
|
|
|
if x.v2.Message() == "" {
|
2022-12-08 16:34:52 +00:00
|
|
|
x.v2.SetMessage(defaultSignatureVerificationMsg)
|
2022-06-16 21:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &x.v2
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMessage writes signature verification failure message.
|
|
|
|
// Message should be used for debug purposes only.
|
|
|
|
//
|
|
|
|
// See also Message.
|
|
|
|
func (x *SignatureVerification) SetMessage(v string) {
|
|
|
|
x.v2.SetMessage(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message returns status message. Zero status returns empty message.
|
|
|
|
// Message should be used for debug purposes only.
|
|
|
|
//
|
|
|
|
// See also SetMessage.
|
|
|
|
func (x SignatureVerification) Message() string {
|
|
|
|
return x.v2.Message()
|
|
|
|
}
|
2022-09-15 05:35:27 +00:00
|
|
|
|
|
|
|
// NodeUnderMaintenance describes failure status for nodes being under maintenance.
|
2023-05-05 06:14:38 +00:00
|
|
|
// Instances provide [Status], [StatusV2] and error interfaces.
|
2022-09-15 05:35:27 +00:00
|
|
|
type NodeUnderMaintenance struct {
|
|
|
|
v2 status.Status
|
|
|
|
}
|
|
|
|
|
2022-12-08 16:34:52 +00:00
|
|
|
const defaultNodeUnderMaintenanceMsg = "node is under maintenance"
|
|
|
|
|
2022-09-15 05:35:27 +00:00
|
|
|
// Error implements the error interface.
|
|
|
|
func (x NodeUnderMaintenance) Error() string {
|
2022-09-19 17:35:33 +00:00
|
|
|
msg := x.Message()
|
|
|
|
if msg == "" {
|
2022-12-08 16:34:52 +00:00
|
|
|
msg = defaultNodeUnderMaintenanceMsg
|
2022-09-19 17:35:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 05:35:27 +00:00
|
|
|
return errMessageStatusV2(
|
|
|
|
globalizeCodeV2(status.NodeUnderMaintenance, status.GlobalizeCommonFail),
|
2022-09-19 17:35:33 +00:00
|
|
|
msg,
|
2022-09-15 05:35:27 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-16 08:21:59 +00:00
|
|
|
// Is implements interface for correct checking current error type with [errors.Is].
|
|
|
|
func (x NodeUnderMaintenance) Is(target error) bool {
|
|
|
|
switch target.(type) {
|
|
|
|
default:
|
2023-05-16 11:44:58 +00:00
|
|
|
return errors.Is(Error, target)
|
2023-05-16 08:21:59 +00:00
|
|
|
case NodeUnderMaintenance, *NodeUnderMaintenance:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 05:35:27 +00:00
|
|
|
func (x *NodeUnderMaintenance) fromStatusV2(st *status.Status) {
|
|
|
|
x.v2 = *st
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStatusV2 implements StatusV2 interface method.
|
|
|
|
// If the value was returned by FromStatusV2, returns the source message.
|
|
|
|
// Otherwise, returns message with
|
|
|
|
// - code: NODE_UNDER_MAINTENANCE;
|
2022-12-08 16:34:52 +00:00
|
|
|
// - string message: written message via SetMessage or
|
|
|
|
// "node is under maintenance" as a default message;
|
2022-09-15 05:35:27 +00:00
|
|
|
// - details: empty.
|
|
|
|
func (x NodeUnderMaintenance) ToStatusV2() *status.Status {
|
|
|
|
x.v2.SetCode(globalizeCodeV2(status.NodeUnderMaintenance, status.GlobalizeCommonFail))
|
2022-12-08 16:34:52 +00:00
|
|
|
if x.v2.Message() == "" {
|
|
|
|
x.v2.SetMessage(defaultNodeUnderMaintenanceMsg)
|
|
|
|
}
|
|
|
|
|
2022-09-15 05:35:27 +00:00
|
|
|
return &x.v2
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMessage writes signature verification failure message.
|
|
|
|
// Message should be used for debug purposes only.
|
|
|
|
//
|
|
|
|
// See also Message.
|
|
|
|
func (x *NodeUnderMaintenance) SetMessage(v string) {
|
|
|
|
x.v2.SetMessage(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message returns status message. Zero status returns empty message.
|
|
|
|
// Message should be used for debug purposes only.
|
|
|
|
//
|
|
|
|
// See also SetMessage.
|
|
|
|
func (x NodeUnderMaintenance) Message() string {
|
|
|
|
return x.v2.Message()
|
|
|
|
}
|