git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/client/status/common.go (about)

     1  package apistatus
     2  
     3  import (
     4  	"encoding/binary"
     5  
     6  	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/status"
     7  )
     8  
     9  // ServerInternal describes failure statuses related to internal server errors.
    10  // Instances provide Status and StatusV2 interfaces.
    11  //
    12  // The status is purely informative, the client should not go into details of the error except for debugging needs.
    13  type ServerInternal struct {
    14  	v2 status.Status
    15  }
    16  
    17  func (x *ServerInternal) Error() string {
    18  	return errMessageStatusV2(
    19  		globalizeCodeV2(status.Internal, status.GlobalizeCommonFail),
    20  		x.v2.Message(),
    21  	)
    22  }
    23  
    24  // implements local interface defined in FromStatusV2 func.
    25  func (x *ServerInternal) fromStatusV2(st *status.Status) {
    26  	x.v2 = *st
    27  }
    28  
    29  // ToStatusV2 implements StatusV2 interface method.
    30  // If the value was returned by FromStatusV2, returns the source message.
    31  // Otherwise, returns message with
    32  //   - code: INTERNAL;
    33  //   - string message: empty;
    34  //   - details: empty.
    35  func (x ServerInternal) ToStatusV2() *status.Status {
    36  	x.v2.SetCode(globalizeCodeV2(status.Internal, status.GlobalizeCommonFail))
    37  	return &x.v2
    38  }
    39  
    40  // SetMessage sets message describing internal error.
    41  //
    42  // Message should be used for debug purposes only.
    43  func (x *ServerInternal) SetMessage(msg string) {
    44  	x.v2.SetMessage(msg)
    45  }
    46  
    47  // Message returns message describing internal server error.
    48  //
    49  // Message should be used for debug purposes only. By default, it is empty.
    50  func (x ServerInternal) Message() string {
    51  	return x.v2.Message()
    52  }
    53  
    54  // WriteInternalServerErr writes err message to ServerInternal instance.
    55  func WriteInternalServerErr(x *ServerInternal, err error) {
    56  	x.SetMessage(err.Error())
    57  }
    58  
    59  // WrongMagicNumber describes failure status related to incorrect network magic.
    60  // Instances provide Status and StatusV2 interfaces.
    61  type WrongMagicNumber struct {
    62  	v2 status.Status
    63  }
    64  
    65  func (x *WrongMagicNumber) Error() string {
    66  	return errMessageStatusV2(
    67  		globalizeCodeV2(status.WrongMagicNumber, status.GlobalizeCommonFail),
    68  		x.v2.Message(),
    69  	)
    70  }
    71  
    72  // implements local interface defined in FromStatusV2 func.
    73  func (x *WrongMagicNumber) fromStatusV2(st *status.Status) {
    74  	x.v2 = *st
    75  }
    76  
    77  // ToStatusV2 implements StatusV2 interface method.
    78  // If the value was returned by FromStatusV2, returns the source message.
    79  // Otherwise, returns message with
    80  //   - code: WRONG_MAGIC_NUMBER;
    81  //   - string message: empty;
    82  //   - details: empty.
    83  func (x WrongMagicNumber) ToStatusV2() *status.Status {
    84  	x.v2.SetCode(globalizeCodeV2(status.WrongMagicNumber, status.GlobalizeCommonFail))
    85  	return &x.v2
    86  }
    87  
    88  // WriteCorrectMagic writes correct network magic.
    89  func (x *WrongMagicNumber) WriteCorrectMagic(magic uint64) {
    90  	// serialize the number
    91  	buf := make([]byte, 8)
    92  
    93  	binary.BigEndian.PutUint64(buf, magic)
    94  
    95  	// create corresponding detail
    96  	var d status.Detail
    97  
    98  	d.SetID(status.DetailIDCorrectMagic)
    99  	d.SetValue(buf)
   100  
   101  	// attach the detail
   102  	x.v2.AppendDetails(d)
   103  }
   104  
   105  // CorrectMagic returns network magic returned by the server.
   106  // Second value indicates presence status:
   107  //   - -1 if number is presented in incorrect format
   108  //   - 0 if number is not presented
   109  //   - +1 otherwise
   110  func (x WrongMagicNumber) CorrectMagic() (magic uint64, ok int8) {
   111  	x.v2.IterateDetails(func(d *status.Detail) bool {
   112  		if d.ID() == status.DetailIDCorrectMagic {
   113  			if val := d.Value(); len(val) == 8 {
   114  				magic = binary.BigEndian.Uint64(val)
   115  				ok = 1
   116  			} else {
   117  				ok = -1
   118  			}
   119  		}
   120  
   121  		return ok != 0
   122  	})
   123  
   124  	return
   125  }
   126  
   127  // SignatureVerification describes failure status related to signature verification.
   128  // Instances provide Status and StatusV2 interfaces.
   129  type SignatureVerification struct {
   130  	v2 status.Status
   131  }
   132  
   133  const defaultSignatureVerificationMsg = "signature verification failed"
   134  
   135  func (x *SignatureVerification) Error() string {
   136  	msg := x.v2.Message()
   137  	if msg == "" {
   138  		msg = defaultSignatureVerificationMsg
   139  	}
   140  
   141  	return errMessageStatusV2(
   142  		globalizeCodeV2(status.SignatureVerificationFail, status.GlobalizeCommonFail),
   143  		msg,
   144  	)
   145  }
   146  
   147  // implements local interface defined in FromStatusV2 func.
   148  func (x *SignatureVerification) fromStatusV2(st *status.Status) {
   149  	x.v2 = *st
   150  }
   151  
   152  // ToStatusV2 implements StatusV2 interface method.
   153  // If the value was returned by FromStatusV2, returns the source message.
   154  // Otherwise, returns message with
   155  //   - code: SIGNATURE_VERIFICATION_FAIL;
   156  //   - string message: written message via SetMessage or
   157  //     "signature verification failed" as a default message;
   158  //   - details: empty.
   159  func (x SignatureVerification) ToStatusV2() *status.Status {
   160  	x.v2.SetCode(globalizeCodeV2(status.SignatureVerificationFail, status.GlobalizeCommonFail))
   161  
   162  	if x.v2.Message() == "" {
   163  		x.v2.SetMessage(defaultSignatureVerificationMsg)
   164  	}
   165  
   166  	return &x.v2
   167  }
   168  
   169  // SetMessage writes signature verification failure message.
   170  // Message should be used for debug purposes only.
   171  //
   172  // See also Message.
   173  func (x *SignatureVerification) SetMessage(v string) {
   174  	x.v2.SetMessage(v)
   175  }
   176  
   177  // Message returns status message. Zero status returns empty message.
   178  // Message should be used for debug purposes only.
   179  //
   180  // See also SetMessage.
   181  func (x SignatureVerification) Message() string {
   182  	return x.v2.Message()
   183  }
   184  
   185  // NodeUnderMaintenance describes failure status for nodes being under maintenance.
   186  // Instances provide Status and StatusV2 interfaces.
   187  type NodeUnderMaintenance struct {
   188  	v2 status.Status
   189  }
   190  
   191  const defaultNodeUnderMaintenanceMsg = "node is under maintenance"
   192  
   193  // Error implements the error interface.
   194  func (x *NodeUnderMaintenance) Error() string {
   195  	msg := x.Message()
   196  	if msg == "" {
   197  		msg = defaultNodeUnderMaintenanceMsg
   198  	}
   199  
   200  	return errMessageStatusV2(
   201  		globalizeCodeV2(status.NodeUnderMaintenance, status.GlobalizeCommonFail),
   202  		msg,
   203  	)
   204  }
   205  
   206  func (x *NodeUnderMaintenance) fromStatusV2(st *status.Status) {
   207  	x.v2 = *st
   208  }
   209  
   210  // ToStatusV2 implements StatusV2 interface method.
   211  // If the value was returned by FromStatusV2, returns the source message.
   212  // Otherwise, returns message with
   213  //   - code: NODE_UNDER_MAINTENANCE;
   214  //   - string message: written message via SetMessage or
   215  //     "node is under maintenance" as a default message;
   216  //   - details: empty.
   217  func (x NodeUnderMaintenance) ToStatusV2() *status.Status {
   218  	x.v2.SetCode(globalizeCodeV2(status.NodeUnderMaintenance, status.GlobalizeCommonFail))
   219  	if x.v2.Message() == "" {
   220  		x.v2.SetMessage(defaultNodeUnderMaintenanceMsg)
   221  	}
   222  
   223  	return &x.v2
   224  }
   225  
   226  // SetMessage writes signature verification failure message.
   227  // Message should be used for debug purposes only.
   228  //
   229  // See also Message.
   230  func (x *NodeUnderMaintenance) SetMessage(v string) {
   231  	x.v2.SetMessage(v)
   232  }
   233  
   234  // Message returns status message. Zero status returns empty message.
   235  // Message should be used for debug purposes only.
   236  //
   237  // See also SetMessage.
   238  func (x NodeUnderMaintenance) Message() string {
   239  	return x.v2.Message()
   240  }
   241  
   242  // InvalidArgument describes failure status related to invalid argument.
   243  // Instances provide Status and StatusV2 interfaces.
   244  type InvalidArgument struct {
   245  	v2 status.Status
   246  }
   247  
   248  const defaultInvalidArgumentMsg = "argument is invalid"
   249  
   250  // Error implements the error interface.
   251  func (x *InvalidArgument) Error() string {
   252  	msg := x.v2.Message()
   253  	if msg == "" {
   254  		msg = defaultInvalidArgumentMsg
   255  	}
   256  
   257  	return errMessageStatusV2(
   258  		globalizeCodeV2(status.InvalidArgument, status.GlobalizeCommonFail),
   259  		msg,
   260  	)
   261  }
   262  
   263  // implements local interface defined in FromStatusV2 func.
   264  func (x *InvalidArgument) fromStatusV2(st *status.Status) {
   265  	x.v2 = *st
   266  }
   267  
   268  // ToStatusV2 implements StatusV2 interface method.
   269  // If the value was returned by FromStatusV2, returns the source message.
   270  // Otherwise, returns message with
   271  //   - code: INVALID_ARGUMENT;
   272  //   - string message: written message via SetMessage or
   273  //     "argument is invalid" as a default message;
   274  //   - details: empty.
   275  func (x InvalidArgument) ToStatusV2() *status.Status {
   276  	x.v2.SetCode(globalizeCodeV2(status.InvalidArgument, status.GlobalizeCommonFail))
   277  	if x.v2.Message() == "" {
   278  		x.v2.SetMessage(defaultInvalidArgumentMsg)
   279  	}
   280  
   281  	return &x.v2
   282  }
   283  
   284  // SetMessage writes invalid argument failure message.
   285  // Message should be used for debug purposes only.
   286  //
   287  // See also Message.
   288  func (x *InvalidArgument) SetMessage(v string) {
   289  	x.v2.SetMessage(v)
   290  }
   291  
   292  // Message returns status message. Zero status returns empty message.
   293  // Message should be used for debug purposes only.
   294  //
   295  // See also SetMessage.
   296  func (x InvalidArgument) Message() string {
   297  	return x.v2.Message()
   298  }