2021-11-16 18:15:56 +00:00
|
|
|
package apistatus
|
|
|
|
|
|
|
|
import (
|
2022-01-25 12:37:30 +00:00
|
|
|
"encoding/binary"
|
|
|
|
|
2021-11-16 18:15:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ServerInternal describes failure statuses related to internal server errors.
|
|
|
|
// Instances provide Status and StatusV2 interfaces.
|
|
|
|
//
|
|
|
|
// 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(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// * code: INTERNAL;
|
|
|
|
// * string message: empty;
|
|
|
|
// * details: empty.
|
|
|
|
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.
|
|
|
|
// Instances provide Status and StatusV2 interfaces.
|
|
|
|
type WrongMagicNumber struct {
|
|
|
|
v2 status.Status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x WrongMagicNumber) Error() string {
|
|
|
|
return errMessageStatusV2(
|
|
|
|
globalizeCodeV2(status.WrongMagicNumber, status.GlobalizeCommonFail),
|
|
|
|
x.v2.Message(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// * code: WRONG_MAGIC_NUMBER;
|
|
|
|
// * string message: empty;
|
|
|
|
// * details: empty.
|
|
|
|
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:
|
|
|
|
// * -1 if number is presented in incorrect format
|
|
|
|
// * 0 if number is not presented
|
|
|
|
// * +1 otherwise
|
|
|
|
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
|
|
|
|
}
|