github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/nsadapter/httputil/model.go (about)

     1  package httputil
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/tidwall/gjson"
     9  
    10  	"github.com/kyma-incubator/compass/components/director/pkg/log"
    11  )
    12  
    13  // ErrorResponse on failed notification service request
    14  type ErrorResponse struct {
    15  	Error error `json:"error"`
    16  }
    17  
    18  // UnmarshalJSON implements Unmarshaler interface. The method unmarshal the data from b into ErrorResponse structure.
    19  func (e *ErrorResponse) UnmarshalJSON(b []byte) error {
    20  	code, success := gjson.Get(string(b), "error.code").Value().(float64)
    21  	if !success {
    22  		return errors.New("failed to parse code")
    23  	}
    24  
    25  	msg, success := gjson.Get(string(b), "error.message").Value().(string)
    26  	if !success {
    27  		return errors.New("failed to parse message")
    28  	}
    29  
    30  	e.Error = Error{
    31  		Code:    int(code),
    32  		Message: msg,
    33  	}
    34  	return nil
    35  }
    36  
    37  // Error indicates processing failure of on-premise systems
    38  type Error struct {
    39  	Code    int    `json:"code"`
    40  	Message string `json:"message"`
    41  }
    42  
    43  func (e Error) Error() string {
    44  	return e.Message
    45  }
    46  
    47  // DetailedError indicates partial processing failure of on-premise systems
    48  type DetailedError struct {
    49  	Code    int      `json:"code"`
    50  	Message string   `json:"message"`
    51  	Details []Detail `json:"details"`
    52  }
    53  
    54  func (d DetailedError) Error() string {
    55  	return d.Message
    56  }
    57  
    58  // Detail contains error details for scc
    59  type Detail struct {
    60  	Message    string `json:"message"`
    61  	Subaccount string `json:"subaccount"`
    62  	LocationID string `json:"locationId"`
    63  }
    64  
    65  // GetTimeoutMessage returns ErrorResponse with status code 408 and message "timeout"
    66  func GetTimeoutMessage() []byte {
    67  	marshal, err := json.Marshal(ErrorResponse{Error: Error{
    68  		Code:    http.StatusRequestTimeout,
    69  		Message: "timeout",
    70  	}})
    71  	if err != nil {
    72  		log.D().Errorf("while marshaling error message  %s", err.Error())
    73  	}
    74  
    75  	return marshal
    76  }