package main import ( "encoding/json" "errors" "fmt" "log" "net/http" ) type errorHTTPResponse struct { ErrorMsg string HTTPStatusCode int HTTPHeader http.Header } func (e errorHTTPResponse) Error() string { return e.ErrorMsg } func errorHTTPResponseNew(r *http.Response, msg string) (e errorHTTPResponse) { e.ErrorMsg = msg e.HTTPStatusCode = r.StatusCode e.HTTPHeader = r.Header return e } type crmdReason struct { Reason string `json:"reason"` Data map[string]*json.RawMessage `json:"data"` } func (e errorHTTPResponse) XStatusReason() (r crmdReason) { jsonStr := e.HTTPHeader.Get("X-Status-Reason") json.Unmarshal([]byte(jsonStr), &r) return } func (m crmdReason) Data2Entity(entityType string) (r interface{}, err error) { if len(m.Data) > 1 { log.Println("Warning: Multiple data object found, only choose first one") log.Println(m) } //only take one object for _, v := range m.Data { switch entityType { case "Lead": e := crmdLead{} err = json.Unmarshal(*v, &e) r = e case "Account": //r = crmdAccount{} default: msg := fmt.Sprintf("(crmdReason) Data2Entity: Unknown EntityType %s", entityType) err = errors.New(msg) log.Fatalln(err) } break //after procesing first ojbect, we quite, neglect all the rest } return }