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

     1  package apistatus
     2  
     3  // Status defines a variety of FrostFS API status returns.
     4  //
     5  // All statuses are split into two disjoint subsets: successful and failed, and:
     6  //   - statuses that implement the build-in error interface are considered failed statuses;
     7  //   - all other value types are considered successes (nil is a default success).
     8  //
     9  // In Go code type of success can be determined by a type switch, failure - by a switch with errors.As calls.
    10  // Nil should be considered as a success, and default switch section - as an unrecognized Status.
    11  //
    12  // To convert statuses into errors and vice versa, use functions ErrToStatus and ErrFromStatus, respectively.
    13  // ErrFromStatus function returns nil for successful statuses. However, to simplify the check of statuses for success,
    14  // IsSuccessful function should be used (try to avoid nil comparison).
    15  // It should be noted that using direct typecasting is not a compatible approach.
    16  //
    17  // To transport statuses using the FrostFS API V2 protocol, see StatusV2 interface and FromStatusV2 and ToStatusV2 functions.
    18  type Status any
    19  
    20  // ErrFromStatus converts Status instance to error if it is failed. Returns nil on successful Status.
    21  //
    22  // Note: direct assignment may not be compatibility-safe.
    23  func ErrFromStatus(st Status) error {
    24  	if err, ok := st.(error); ok {
    25  		return err
    26  	}
    27  
    28  	return nil
    29  }
    30  
    31  // ErrToStatus converts the error instance to Status instance.
    32  //
    33  // Note: direct assignment may not be compatibility-safe.
    34  func ErrToStatus(err error) Status {
    35  	return err
    36  }
    37  
    38  // IsSuccessful checks if status is successful.
    39  //
    40  // Note: direct cast may not be compatibility-safe.
    41  func IsSuccessful(st Status) bool {
    42  	_, ok := st.(error)
    43  	return !ok
    44  }