github.com/webdestroya/awsmocker@v0.2.6/error.go (about)

     1  package awsmocker
     2  
     3  import (
     4  	"encoding/xml"
     5  	"fmt"
     6  	"net/http"
     7  )
     8  
     9  // aws/protocol/restjson/decoder_util.go
    10  
    11  type errorResponse struct {
    12  	XMLName xml.Name `xml:"ErrorResponse" json:"-"`
    13  
    14  	Type    string `xml:"Error>Type" json:"-"`
    15  	Code    string `xml:"Error>Code" json:"code"`
    16  	Message string `xml:"Error>Message" json:"message"`
    17  
    18  	RequestId string `xml:"RequestId" json:"-"`
    19  
    20  	statusCode int `xml:"-" json:"-"`
    21  }
    22  
    23  func (e *errorResponse) getResponse(rr *ReceivedRequest) *httpResponse {
    24  
    25  	if e.statusCode == 0 {
    26  		e.statusCode = http.StatusBadRequest
    27  		// e.statusCode = http.StatusTeapot
    28  	}
    29  
    30  	switch rr.AssumedResponseType {
    31  	case ContentTypeJSON:
    32  		return &httpResponse{
    33  			contentType: ContentTypeJSON,
    34  			Body:        EncodeAsJson(e),
    35  			StatusCode:  e.statusCode, // 501
    36  		}
    37  	case ContentTypeXML:
    38  		return &httpResponse{
    39  			contentType: ContentTypeXML,
    40  			Body:        encodeAsXml(e),
    41  			StatusCode:  e.statusCode, // 501
    42  		}
    43  	default:
    44  		return &httpResponse{
    45  			contentType: ContentTypeText,
    46  			Body:        fmt.Sprintf("ERROR! %s: %s", e.Code, e.Message),
    47  			StatusCode:  e.statusCode, // 501
    48  		}
    49  	}
    50  }
    51  
    52  func generateErrorStruct(statusCode int, code string, message string, args ...any) *errorResponse {
    53  	return &errorResponse{
    54  		Type:       "Sender",
    55  		Code:       code,
    56  		Message:    fmt.Sprintf(message, args...),
    57  		RequestId:  "7a62c49f-347e-4fc4-9331-6e8eEXAMPLE",
    58  		statusCode: statusCode,
    59  	}
    60  }