src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

error_utils.go (1492B)


      1 package xml
      2 
      3 import (
      4 	"encoding/xml"
      5 	"fmt"
      6 	"io"
      7 )
      8 
      9 // ErrorComponents represents the error response fields
     10 // that will be deserialized from an xml error response body
     11 type ErrorComponents struct {
     12 	Code    string
     13 	Message string
     14 }
     15 
     16 // GetErrorResponseComponents returns the error fields from an xml error response body
     17 func GetErrorResponseComponents(r io.Reader, noErrorWrapping bool) (ErrorComponents, error) {
     18 	if noErrorWrapping {
     19 		var errResponse noWrappedErrorResponse
     20 		if err := xml.NewDecoder(r).Decode(&errResponse); err != nil && err != io.EOF {
     21 			return ErrorComponents{}, fmt.Errorf("error while deserializing xml error response: %w", err)
     22 		}
     23 		return ErrorComponents{
     24 			Code:    errResponse.Code,
     25 			Message: errResponse.Message,
     26 		}, nil
     27 	}
     28 
     29 	var errResponse wrappedErrorResponse
     30 	if err := xml.NewDecoder(r).Decode(&errResponse); err != nil && err != io.EOF {
     31 		return ErrorComponents{}, fmt.Errorf("error while deserializing xml error response: %w", err)
     32 	}
     33 	return ErrorComponents{
     34 		Code:    errResponse.Code,
     35 		Message: errResponse.Message,
     36 	}, nil
     37 }
     38 
     39 // noWrappedErrorResponse represents the error response body with
     40 // no internal <Error></Error wrapping
     41 type noWrappedErrorResponse struct {
     42 	Code    string `xml:"Code"`
     43 	Message string `xml:"Message"`
     44 }
     45 
     46 // wrappedErrorResponse represents the error response body
     47 // wrapped within <Error>...</Error>
     48 type wrappedErrorResponse struct {
     49 	Code    string `xml:"Error>Code"`
     50 	Message string `xml:"Error>Message"`
     51 }