From 23ccb0896131f93e51e7a00c3f58f38f2ffa47d9 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Thu, 29 Jun 2017 17:06:36 +1000 Subject: [PATCH] special http response error is created for further diagnostic of errors when doing CRM http request. --- errorHttp.go | 34 ++++++++++++++++++++++++++++++++++ errorHttp_test.go | 7 +++++++ 2 files changed, 41 insertions(+) create mode 100644 errorHttp.go create mode 100644 errorHttp_test.go diff --git a/errorHttp.go b/errorHttp.go new file mode 100644 index 0000000..aebaa41 --- /dev/null +++ b/errorHttp.go @@ -0,0 +1,34 @@ +package main + +import ( + "encoding/json" + "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 string `json:"data"` +} + +func (e *errorHTTPResponse) XStatusReason() (r crmdReason) { + jsonStr := e.HTTPHeader.Get("X-Status-Reason") + json.Unmarshal([]byte(jsonStr), &r) + return +} diff --git a/errorHttp_test.go b/errorHttp_test.go new file mode 100644 index 0000000..8695488 --- /dev/null +++ b/errorHttp_test.go @@ -0,0 +1,7 @@ +package main + +import "testing" + +func TestXStatusReason(t *testing.T) { + //TODO +}