From c999e7247164ee8643b00e3d50a5c9e3101adf38 Mon Sep 17 00:00:00 2001 From: alexey Date: Thu, 5 Jul 2018 22:36:18 +0300 Subject: [PATCH 1/4] 1. Fix error creating based Aanda error messages 2. All tests passed --- aanda-sdk.go | 10 ++++++++-- error.go | 45 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/aanda-sdk.go b/aanda-sdk.go index a4b6ab7..783a1d2 100644 --- a/aanda-sdk.go +++ b/aanda-sdk.go @@ -42,8 +42,14 @@ func parseError(body []byte) error { // Try to parse known struct var aandaErr AandaError err := json.Unmarshal(body, &aandaErr) - if err == nil { - return &aandaErr + if err == nil && !aandaErr.IsEmpty() { + return aandaErr.ToError() + } + + var aandaErrMsg AandaErrorMsg + err = json.Unmarshal(body, &aandaErrMsg) + if err == nil && !aandaErrMsg.IsEmpty() { + return aandaErrMsg.ToError() } //Try parse as JSON diff --git a/error.go b/error.go index feb5f8e..3822629 100644 --- a/error.go +++ b/error.go @@ -1,14 +1,49 @@ package aandaSdk -import "fmt" +import ( + "errors" + "fmt" + "strings" +) type AandaError struct { - Status string `json:"status"` - Code int `json:"code"` - Type string `json:"type"` - Note string `json:"note"` + Status string `json:"status" json:"Status"` + Code int `json:"code" json:"Code"` + Type string `json:"type" json:"Type"` + Note string `json:"note" json:"Note"` +} + +func (o *AandaError) IsEmpty() bool { + return o.Status == "" && o.Code == 0 && o.Type == "" && o.Note == "" +} + +func (o *AandaError) ToError() error { + msgs := []string{} + for _, s := range []string{o.Type, o.Note} { + if s != "" { + msgs = append(msgs, s) + } + } + + return errors.New(strings.Join(msgs, "; ")) } func (o *AandaError) Error() string { return fmt.Sprintf("[%s] %d - %s; %s", o.Status, o.Code, o.Type, o.Note) } + +type AandaErrorMsg struct { + Err string `json:"error"` +} + +func (o *AandaErrorMsg) IsEmpty() bool { + return o.Err == "" +} + +func (o *AandaErrorMsg) ToError() error { + return errors.New(o.Err) +} + +func (o *AandaErrorMsg) Error() string { + return o.Err +} From 06126c67805be3216a7fea969a8c2dc1a3fa2636 Mon Sep 17 00:00:00 2001 From: alexey Date: Thu, 5 Jul 2018 22:57:30 +0300 Subject: [PATCH 2/4] 1. Doesn't hide an unmarshal error and if doesn't get an error response from the service --- aanda-sdk.go | 140 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 123 insertions(+), 17 deletions(-) diff --git a/aanda-sdk.go b/aanda-sdk.go index 783a1d2..8a36d16 100644 --- a/aanda-sdk.go +++ b/aanda-sdk.go @@ -125,7 +125,13 @@ func (self *Api) CountryListRequest() ([]CountryListResponse, error) { jsonData := []CountryListResponse{} err := json.Unmarshal(body, &jsonData) if err != nil { - return nil, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return nil, err + } else { + return nil, respErr + } } return jsonData, nil @@ -145,7 +151,13 @@ func (self *Api) CityListRequest(countryCode int) (CityListResponse, error) { err := json.Unmarshal([]byte(bodyStr), &jsonData) if err != nil { - return CityListResponse{}, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return CityListResponse{}, err + } else { + return CityListResponse{}, respErr + } } return jsonData, nil @@ -163,7 +175,13 @@ func (self *Api) HotelListRequest(cityCode int) ([]HotelListResponse, error) { err := json.Unmarshal(body, &jsonData) if err != nil { - return nil, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return nil, err + } else { + return nil, respErr + } } return jsonData, nil @@ -181,7 +199,13 @@ func (self *Api) HotelDescriptionRequest(hotelCode int) (HotelDescriptionRespons err := json.Unmarshal(body, &jsonData) if err != nil { - return HotelDescriptionResponse{}, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return HotelDescriptionResponse{}, err + } else { + return HotelDescriptionResponse{}, respErr + } } return jsonData, nil @@ -197,7 +221,13 @@ func (self *Api) CurrencyListRequest() ([]CurrencyListResponse, error) { jsonData := []CurrencyListResponse{} err := json.Unmarshal(body, &jsonData) if err != nil { - return nil, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return nil, err + } else { + return nil, respErr + } } return jsonData, nil @@ -213,7 +243,13 @@ func (self *Api) MealTypeRequest() ([]MealTypeResponse, error) { jsonData := []MealTypeResponse{} err := json.Unmarshal(body, &jsonData) if err != nil { - return nil, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return nil, err + } else { + return nil, respErr + } } return jsonData, nil @@ -229,7 +265,13 @@ func (self *Api) MealCategoryRequest() ([]MealCategoryResponse, error) { jsonData := []MealCategoryResponse{} err := json.Unmarshal(body, &jsonData) if err != nil { - return nil, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return nil, err + } else { + return nil, respErr + } } return jsonData, nil @@ -245,7 +287,13 @@ func (self *Api) ServiceTypeRequest() ([]ServiceTypeResponse, error) { jsonData := []ServiceTypeResponse{} err := json.Unmarshal(body, &jsonData) if err != nil { - return nil, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return nil, err + } else { + return nil, respErr + } } return jsonData, nil @@ -272,7 +320,13 @@ func (self *Api) HotelPricingRequest(priceReq HotelPricingRequest) (HotelPricing jsonData := HotelPricingResponse{} err = json.Unmarshal(body, &jsonData) if err != nil { - return HotelPricingResponse{}, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return HotelPricingResponse{}, err + } else { + return HotelPricingResponse{}, respErr + } } return jsonData, nil @@ -332,7 +386,17 @@ func (self *Api) OrderRequest(orderReq OrderRequest) (OrderRequestResponse, erro err = json.Unmarshal(body, &jsonData) vErr := validator.ValidateStruct(jsonData) if err != nil || vErr != nil { - return OrderRequestResponse{}, parseError(body) + respErr := parseError(body) + + if respErr == nil { + if err != nil { + return OrderRequestResponse{}, err + } else { + return OrderRequestResponse{}, vErr + } + } else { + return OrderRequestResponse{}, respErr + } } return jsonData, nil @@ -361,7 +425,13 @@ func (self *Api) OrderInfoRequest(id int) (OrderInfoResponse, error) { jsonData := OrderInfoResponse{} err = json.Unmarshal(body, &jsonData) if err != nil { - return OrderInfoResponse{}, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return OrderInfoResponse{}, err + } else { + return OrderInfoResponse{}, respErr + } } return jsonData, nil @@ -378,7 +448,13 @@ func (self *Api) OrderMessagesRequest(orderId int) ([]OrderMessagesResponse, err jsonData := []OrderMessagesResponse{} err := json.Unmarshal(body, &jsonData) if err != nil { - return nil, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return nil, err + } else { + return nil, respErr + } } return jsonData, nil @@ -396,7 +472,13 @@ func (self *Api) SendOrderMessageRequest(somReq SendOrderMessageRequest) (SendOr jsonData := SendOrderMessageResponse{} err := json.Unmarshal(body, &jsonData) if err != nil { - return SendOrderMessageResponse{}, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return SendOrderMessageResponse{}, err + } else { + return SendOrderMessageResponse{}, respErr + } } return jsonData, nil @@ -423,7 +505,13 @@ func (self *Api) OrderListRequest(orderReq OrderListRequest) ([]OrderListRespons jsonData := []OrderListResponse{} err = json.Unmarshal(body, &jsonData) if err != nil { - return nil, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return nil, err + } else { + return nil, respErr + } } return jsonData, nil @@ -439,7 +527,13 @@ func (self *Api) ClientStatusRequest() ([]ClientStatusResponse, error) { jsonData := []ClientStatusResponse{} err := json.Unmarshal(body, &jsonData) if err != nil { - return nil, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return nil, err + } else { + return nil, respErr + } } return jsonData, nil @@ -455,7 +549,13 @@ func (self *Api) HotelAmenitiesRequest() ([]HotelAmenitiesResponse, error) { jsonData := []HotelAmenitiesResponse{} err := json.Unmarshal(body, &jsonData) if err != nil { - return nil, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return nil, err + } else { + return nil, respErr + } } return jsonData, nil @@ -471,7 +571,13 @@ func (self *Api) RoomAmenitiesRequest() ([]RoomAmenitiesResponse, error) { jsonData := []RoomAmenitiesResponse{} err := json.Unmarshal(body, &jsonData) if err != nil { - return nil, parseError(body) + respErr := parseError(body) + + if respErr == nil { + return nil, err + } else { + return nil, respErr + } } return jsonData, nil From efe2d751cc51324cf6ee77e7b1cd2d606ab12e1e Mon Sep 17 00:00:00 2001 From: alexey Date: Thu, 5 Jul 2018 23:14:30 +0300 Subject: [PATCH 3/4] 1. Made compatible hotel_detail response --- supplierStructures.go | 84 +++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/supplierStructures.go b/supplierStructures.go index d91d51d..19ea9a1 100644 --- a/supplierStructures.go +++ b/supplierStructures.go @@ -106,66 +106,66 @@ type HotelListResponse struct { } type HotelDescriptionResponse struct { - HotelCode string `json:"hotel_code"` - HotelName string `json:"hotel_name"` - Vat string `json:"vat"` - Address string `json:"address"` - Phone string `json:"phone"` - Description string `json:"description"` - FullDescription string `json:"full_description"` + HotelCode MustString `json:"hotel_code"` + HotelName string `json:"hotel_name"` + Vat MustString `json:"vat"` + Address string `json:"address"` + Phone string `json:"phone"` + Description string `json:"description"` + FullDescription string `json:"full_description"` FullAddress struct { - Zip string `json:"zip"` - Region string `json:"region"` - City string `json:"city"` - Addinfo string `json:"addinfo"` - Strtype string `json:"strtype"` - Strname string `json:"strname"` - Proptype string `json:"proptype"` - Propnumber string `json:"propnumber"` - Buildingattr string `json:"buildingattr"` - Buildingnumber string `json:"buildingnumber"` - Addinfo2 string `json:"addinfo2"` - CityCode string `json:"city_code"` - CityName string `json:"city_name"` - CityLatitude string `json:"city_latitude"` - CityLongitude string `json:"city_longitude"` - HotelLatitude string `json:"hotel_latitude"` - HotelLongitude string `json:"hotel_longitude"` - CountryCode string `json:"country_code"` - CountryName string `json:"country_name"` + Zip MustString `json:"zip"` + Region string `json:"region"` + City string `json:"city"` + Addinfo string `json:"addinfo"` + Strtype string `json:"strtype"` + Strname string `json:"strname"` + Proptype string `json:"proptype"` + Propnumber MustString `json:"propnumber"` + Buildingattr string `json:"buildingattr"` + Buildingnumber string `json:"buildingnumber"` + Addinfo2 string `json:"addinfo2"` + CityCode MustString `json:"city_code"` + CityName string `json:"city_name"` + CityLatitude MustString `json:"city_latitude"` + CityLongitude MustString `json:"city_longitude"` + HotelLatitude MustString `json:"hotel_latitude"` + HotelLongitude MustString `json:"hotel_longitude"` + CountryCode MustString `json:"country_code"` + CountryName string `json:"country_name"` } `json:"full_address"` - RatingCode string `json:"rating_code"` - RatingName string `json:"rating_name"` - StarsCode string `json:"stars_code"` - StarsName string `json:"stars_name"` + RatingCode MustString `json:"rating_code"` + RatingName string `json:"rating_name"` + StarsCode MustString `json:"stars_code"` + StarsName MustString `json:"stars_name"` Images []struct { URL string `json:"Url"` Desc string `json:"desc"` } `json:"images"` - CurrencyCode string `json:"currency_code"` - CurrencyName string `json:"currency_name"` + CurrencyCode MustString `json:"currency_code"` + CurrencyName string `json:"currency_name"` HotelAmenities []struct { - Name string `json:"name"` - Id string `json:"id"` + Name string `json:"name"` + Id MustString `json:"id"` } `json:"hotel_amenities"` Rooms []struct { - RoomCode string `json:"room_code"` + RoomCode MustString `json:"room_code"` RoomName string `json:"room_name"` NumberOfGuests MustString `json:"number_of_guests"` RoomDescription string `json:"room_description"` Images string `json:"images"` RoomAmenities []struct { - Name string `json:"name"` - Id string `json:"id"` + Name string `json:"name"` + Id MustString `json:"id"` } `json:"room_amenities"` } `json:"rooms"` Conference []interface{} `json:"conference"` Group struct { - Qty string `json:"qty"` - Type string `json:"type"` - Typename string `json:"typename"` - Note string `json:"note"` - Rule string `json:"rule"` + Qty MustString `json:"qty"` + Type MustString `json:"type"` + Typename string `json:"typename"` + Note string `json:"note"` + Rule string `json:"rule"` } `json:"group"` } From e559492e42e230791dd994d6ca070c8e743dfb71 Mon Sep 17 00:00:00 2001 From: alexey Date: Thu, 5 Jul 2018 23:43:58 +0300 Subject: [PATCH 4/4] 1. HotelDetail struct will catch an error --- aanda-sdk.go | 2 +- error.go | 14 +++++++------- supplierStructures.go | 1 + 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/aanda-sdk.go b/aanda-sdk.go index 8a36d16..82a2f67 100644 --- a/aanda-sdk.go +++ b/aanda-sdk.go @@ -198,7 +198,7 @@ func (self *Api) HotelDescriptionRequest(hotelCode int) (HotelDescriptionRespons jsonData := HotelDescriptionResponse{} err := json.Unmarshal(body, &jsonData) - if err != nil { + if err != nil || jsonData.Status != nil { respErr := parseError(body) if respErr == nil { diff --git a/error.go b/error.go index 3822629..76edcf2 100644 --- a/error.go +++ b/error.go @@ -7,18 +7,18 @@ import ( ) type AandaError struct { - Status string `json:"status" json:"Status"` - Code int `json:"code" json:"Code"` - Type string `json:"type" json:"Type"` - Note string `json:"note" json:"Note"` + Status string `json:"status" json:"Status"` + Code MustString `json:"code" json:"Code"` + Type string `json:"type" json:"Type"` + Note string `json:"note" json:"Note"` } func (o *AandaError) IsEmpty() bool { - return o.Status == "" && o.Code == 0 && o.Type == "" && o.Note == "" + return o.Status == "" && (o.Code == "" || o.Code == "0") && o.Type == "" && o.Note == "" } func (o *AandaError) ToError() error { - msgs := []string{} + var msgs []string for _, s := range []string{o.Type, o.Note} { if s != "" { msgs = append(msgs, s) @@ -29,7 +29,7 @@ func (o *AandaError) ToError() error { } func (o *AandaError) Error() string { - return fmt.Sprintf("[%s] %d - %s; %s", o.Status, o.Code, o.Type, o.Note) + return fmt.Sprintf("[%s] %s - %s; %s", o.Status, o.Code, o.Type, o.Note) } type AandaErrorMsg struct { diff --git a/supplierStructures.go b/supplierStructures.go index 19ea9a1..7d558a4 100644 --- a/supplierStructures.go +++ b/supplierStructures.go @@ -106,6 +106,7 @@ type HotelListResponse struct { } type HotelDescriptionResponse struct { + Status *string `json:"status,omitempty"` HotelCode MustString `json:"hotel_code"` HotelName string `json:"hotel_name"` Vat MustString `json:"vat"`