github.com/cyverse/go-irodsclient@v0.13.2/irods/message/error.go (about)

     1  package message
     2  
     3  import (
     4  	"encoding/xml"
     5  
     6  	"github.com/cyverse/go-irodsclient/irods/common"
     7  	"golang.org/x/xerrors"
     8  )
     9  
    10  type IRODSMessageError struct {
    11  	XMLName xml.Name `xml:"RError_PI"`
    12  	Count   int      `xml:"count"`
    13  	Errors  []ErrorMsg
    14  }
    15  
    16  type ErrorMsg struct {
    17  	XMLName xml.Name `xml:"RErrMsg_PI"`
    18  	Status  int      `xml:"status"`
    19  	Message string   `xml:"msg"`
    20  }
    21  
    22  func NewIRODSMessageError(status int, msg string) *IRODSMessageError {
    23  	return &IRODSMessageError{
    24  		Count: 1,
    25  		Errors: []ErrorMsg{{
    26  			Status:  status,
    27  			Message: msg,
    28  		}},
    29  	}
    30  }
    31  
    32  // GetBytes returns byte array
    33  func (msg *IRODSMessageError) GetBytes() ([]byte, error) {
    34  	if msg.Count != len(msg.Errors) {
    35  		return nil, xerrors.Errorf("invalid count %d, error length is %d", msg.Count, len(msg.Errors))
    36  	}
    37  
    38  	if msg.Count == 0 {
    39  		return nil, nil
    40  	}
    41  
    42  	xmlBytes, err := xml.Marshal(msg)
    43  	if err != nil {
    44  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    45  	}
    46  	return xmlBytes, nil
    47  }
    48  
    49  // FromBytes returns struct from bytes
    50  func (msg *IRODSMessageError) FromBytes(bytes []byte) error {
    51  	if bytes == nil {
    52  		msg.Count = 0
    53  		msg.Errors = nil
    54  
    55  		return nil
    56  	}
    57  
    58  	err := xml.Unmarshal(bytes, msg)
    59  	if err != nil {
    60  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    61  	}
    62  	return nil
    63  }
    64  
    65  // GetMessage builds a message
    66  func (msg *IRODSMessageError) GetMessage() (*IRODSMessage, error) {
    67  	bytes, err := msg.GetBytes()
    68  	if err != nil {
    69  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    70  	}
    71  
    72  	msgBody := IRODSMessageBody{
    73  		Type:    RODS_MESSAGE_API_REPLY_TYPE,
    74  		Message: nil,
    75  		Error:   bytes,
    76  		Bs:      nil,
    77  		IntInfo: int32(common.ACTION_FAILED_ERR),
    78  	}
    79  
    80  	msgHeader, err := msgBody.BuildHeader()
    81  	if err != nil {
    82  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    83  	}
    84  
    85  	return &IRODSMessage{
    86  		Header: msgHeader,
    87  		Body:   &msgBody,
    88  	}, nil
    89  }
    90  
    91  // FromMessage returns struct from IRODSMessage
    92  func (msg *IRODSMessageError) FromMessage(msgIn *IRODSMessage) error {
    93  	if msgIn.Body == nil {
    94  		return xerrors.Errorf("empty message body")
    95  	}
    96  
    97  	err := msg.FromBytes(msgIn.Body.Message)
    98  	if err != nil {
    99  		return xerrors.Errorf("failed to get irods message from message body")
   100  	}
   101  	return nil
   102  }