Skip to content

Commit

Permalink
Merge pull request #14 from tmconsulting/fix-tests
Browse files Browse the repository at this point in the history
1. Fix error creating based Aanda error messages
  • Loading branch information
roquie authored Jul 6, 2018
2 parents fe83dd4 + e559492 commit 4eba3b6
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 68 deletions.
152 changes: 132 additions & 20 deletions aanda-sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -119,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
Expand All @@ -139,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
Expand All @@ -157,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
Expand All @@ -174,8 +198,14 @@ func (self *Api) HotelDescriptionRequest(hotelCode int) (HotelDescriptionRespons
jsonData := HotelDescriptionResponse{}

err := json.Unmarshal(body, &jsonData)
if err != nil {
return HotelDescriptionResponse{}, parseError(body)
if err != nil || jsonData.Status != nil {
respErr := parseError(body)

if respErr == nil {
return HotelDescriptionResponse{}, err
} else {
return HotelDescriptionResponse{}, respErr
}
}

return jsonData, nil
Expand All @@ -191,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
Expand All @@ -207,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
Expand All @@ -223,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
Expand All @@ -239,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
Expand All @@ -266,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
Expand Down Expand Up @@ -326,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
Expand Down Expand Up @@ -355,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
Expand All @@ -372,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
Expand All @@ -390,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
Expand All @@ -417,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
Expand All @@ -433,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
Expand All @@ -449,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
Expand All @@ -465,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
Expand Down
47 changes: 41 additions & 6 deletions error.go
Original file line number Diff line number Diff line change
@@ -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 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 == "" || o.Code == "0") && o.Type == "" && o.Note == ""
}

func (o *AandaError) ToError() error {
var 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)
return fmt.Sprintf("[%s] %s - %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
}
Loading

0 comments on commit 4eba3b6

Please sign in to comment.