respond
respond copied to clipboard
respond.Before unable to find original request with Go1.7+ native Context
Due to the changes in Go1.7+ and the implementation of a native Context, the respond.Before operation fails due to the original request being duplicated within WithContext and not available to the Before handler.
Example:
func (r *Request) WithContext(ctx context.Context) *Request {
if ctx == nil {
panic("nil context")
}
r2 := new(Request)
*r2 = *r
r2.ctx = ctx
return r2
}
This sample code will not be able to access the original data object to resolve the .Public interface:
responder.Before = func(w http.ResponseWriter, r *http.Request, status int, data interface{}) (int, interface{}) {
if err, ok := data.(error); ok {
return status, map[string]interface{}{"error": err.Error()}
}
if obj, ok := data.(interface {
Public() interface{}
}); ok {
return status, obj.Public()
}
return status, data
}