Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return codes.InvalidArgument to rather return HTTP 400 instead of HTTP 500 #409

Merged
merged 1 commit into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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