Skip to content

Commit

Permalink
ISSUE#405: customize the error return (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaporz authored and achew22 committed Jun 15, 2017
1 parent c6f7a5a commit d64f531
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 58 deletions.
61 changes: 61 additions & 0 deletions examples/clients/echo/EchoServiceApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,64 @@ func (a EchoServiceApi) EchoBody (body ExamplepbSimpleMessage) (ExamplepbSimpleM

return *successPayload, err
}
/**
* Echo method receives a simple message and returns it.
* The message posted as the id parameter will also be\nreturned.
* @param id
* @param num
* @return ExamplepbSimpleMessage
*/
//func (a EchoServiceApi) Echo_1 (id string, num string) (ExamplepbSimpleMessage, error) {
func (a EchoServiceApi) Echo_1 (id string, num string) (ExamplepbSimpleMessage, error) {

_sling := sling.New().Get(a.basePath)

// create path and map variables
path := "/v1/example/echo/{id}/{num}"
path = strings.Replace(path, "{" + "id" + "}", fmt.Sprintf("%v", id), -1)
path = strings.Replace(path, "{" + "num" + "}", fmt.Sprintf("%v", num), -1)

_sling = _sling.Path(path)

// accept header
accepts := []string { "application/json" }
for key := range accepts {
_sling = _sling.Set("Accept", accepts[key])
break // only use the first Accept
}


var successPayload = new(ExamplepbSimpleMessage)

// We use this map (below) so that any arbitrary error JSON can be handled.
// FIXME: This is in the absence of this Go generator honoring the non-2xx
// response (error) models, which needs to be implemented at some point.
var failurePayload map[string]interface{}

httpResponse, err := _sling.Receive(successPayload, &failurePayload)

if err == nil {
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
if failurePayload != nil {
// If the failurePayload is present, there likely was some kind of non-2xx status
// returned (and a JSON payload error present)
var str []byte
str, err = json.Marshal(failurePayload)
if err == nil { // For safety, check for an error marshalling... probably superfluous
// This will return the JSON error body as a string
err = errors.New(string(str))
}
} else {
// So, there was no network-type error, and nothing in the failure payload,
// but we should still check the status code
if httpResponse == nil {
// This should never happen...
err = errors.New("No HTTP Response received.")
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
}
}
}

return *successPayload, err
}
1 change: 1 addition & 0 deletions examples/clients/echo/ExamplepbSimpleMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import (

type ExamplepbSimpleMessage struct {
Id string `json:"id,omitempty"`
Num string `json:"num,omitempty"`

}
42 changes: 21 additions & 21 deletions examples/examplepb/a_bit_of_everything.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 21 additions & 11 deletions examples/examplepb/echo_service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d64f531

Please sign in to comment.