response.go raw

   1  package xmlrpc
   2  
   3  import (
   4  	"fmt"
   5  	"regexp"
   6  )
   7  
   8  var (
   9  	faultRx = regexp.MustCompile(`<fault>(\s|\S)+</fault>`)
  10  )
  11  
  12  // FaultError is returned from the server when an invalid call is made
  13  type FaultError struct {
  14  	Code   int    `xmlrpc:"faultCode"`
  15  	String string `xmlrpc:"faultString"`
  16  }
  17  
  18  // Error implements the error interface
  19  func (e FaultError) Error() string {
  20  	return fmt.Sprintf("Fault(%d): %s", e.Code, e.String)
  21  }
  22  
  23  type Response []byte
  24  
  25  func (r Response) Err() error {
  26  	if !faultRx.Match(r) {
  27  		return nil
  28  	}
  29  	var fault FaultError
  30  	if err := unmarshal(r, &fault); err != nil {
  31  		return err
  32  	}
  33  	return fault
  34  }
  35  
  36  func (r Response) Unmarshal(v interface{}) error {
  37  	if err := unmarshal(r, v); err != nil {
  38  		return err
  39  	}
  40  
  41  	return nil
  42  }
  43