Better errors in Context.Negotiation

This commit is contained in:
Manu Mtz-Almeida 2014-08-31 18:41:11 +02:00
parent 275bdc194e
commit 012c935a46

View File

@ -292,8 +292,7 @@ type Negotiate struct {
} }
func (c *Context) Negotiate(code int, config Negotiate) { func (c *Context) Negotiate(code int, config Negotiate) {
result := c.NegotiateFormat(config.Offered...) switch c.NegotiateFormat(config.Offered...) {
switch result {
case MIMEJSON: case MIMEJSON:
data := chooseData(config.JSONData, config.Data) data := chooseData(config.JSONData, config.Data)
c.JSON(code, data) c.JSON(code, data)
@ -310,11 +309,14 @@ func (c *Context) Negotiate(code int, config Negotiate) {
c.XML(code, data) c.XML(code, data)
default: default:
c.Fail(400, errors.New("m")) c.Fail(http.StatusNotAcceptable, errors.New("the accepted formats are not offered by the server"))
} }
} }
func (c *Context) NegotiateFormat(offered ...string) string { func (c *Context) NegotiateFormat(offered ...string) string {
if len(offered) == 0 {
panic("you must provide at least one offer")
}
if c.accepted == nil { if c.accepted == nil {
c.accepted = parseAccept(c.Request.Header.Get("Accept")) c.accepted = parseAccept(c.Request.Header.Get("Accept"))
} }