Created
October 31, 2017 23:59
-
-
Save travisjeffery/93cd7c79cebe7a9e0266967d1c8e58cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// transport.go | |
r.Methods("GET").Handle("/users/{id}", httptransport.NewServer( | |
endpoints.GetUserEndpoint, | |
decodeGetUserRequest, | |
encodeResponse, | |
options..., | |
)) | |
type errorer interface { | |
GetError() *example.Error | |
} | |
func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { | |
if e, ok := response.(errorer); ok { | |
if err := e.GetError(); err != nil { | |
w.WriteHeader(int(err.Code)) | |
} | |
} | |
w.Header().Set("Content-Type", "application/json; charset=utf-8") | |
return json.NewEncoder(w).Encode(response) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment