From 3285007fbb053066188b0fd26c839c583d0fc055 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Mon, 23 Mar 2015 04:39:53 +0100 Subject: [PATCH] Refactores context.go --- context.go | 13 +++++++------ context_test.go | 12 ++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/context.go b/context.go index 300b1e72..ffd02b8e 100644 --- a/context.go +++ b/context.go @@ -8,13 +8,14 @@ import ( "bytes" "errors" "fmt" - "github.com/gin-gonic/gin/binding" - "github.com/gin-gonic/gin/render" - "github.com/julienschmidt/httprouter" "log" "net" "net/http" "strings" + + "github.com/gin-gonic/gin/binding" + "github.com/gin-gonic/gin/render" + "github.com/julienschmidt/httprouter" ) const ( @@ -187,14 +188,14 @@ func (c *Context) Get(key string) (interface{}, error) { return value, nil } } - return nil, errors.New("Key does not exist.") + return nil, errors.New("Key %s does not exist") } // MustGet returns the value for the given key or panics if the value doesn't exist. func (c *Context) MustGet(key string) interface{} { value, err := c.Get(key) - if err != nil || value == nil { - log.Panicf("Key %s doesn't exist", value) + if err != nil { + log.Panicf(err.Error()) } return value } diff --git a/context_test.go b/context_test.go index 745e1cdc..b531e6d3 100644 --- a/context_test.go +++ b/context_test.go @@ -214,11 +214,11 @@ func TestHandlerFunc(t *testing.T) { r.ServeHTTP(w, req) if w.Code != 404 { - t.Errorf("Response code should be Not found, was: %s", w.Code) + t.Errorf("Response code should be Not found, was: %d", w.Code) } if stepsPassed != 2 { - t.Errorf("Falied to switch context in handler function: %s", stepsPassed) + t.Errorf("Falied to switch context in handler function: %d", stepsPassed) } } @@ -329,7 +329,7 @@ func TestBindingJSON(t *testing.T) { r.ServeHTTP(w, req) if w.Code != 200 { - t.Errorf("Response code should be Ok, was: %s", w.Code) + t.Errorf("Response code should be Ok, was: %d", w.Code) } if w.Body.String() != "{\"parsed\":\"bar\"}\n" { @@ -362,7 +362,7 @@ func TestBindingJSONEncoding(t *testing.T) { r.ServeHTTP(w, req) if w.Code != 200 { - t.Errorf("Response code should be Ok, was: %s", w.Code) + t.Errorf("Response code should be Ok, was: %d", w.Code) } if w.Body.String() != "{\"parsed\":\"嘉\"}\n" { @@ -395,7 +395,7 @@ func TestBindingJSONNoContentType(t *testing.T) { r.ServeHTTP(w, req) if w.Code != 400 { - t.Errorf("Response code should be Bad request, was: %s", w.Code) + t.Errorf("Response code should be Bad request, was: %d", w.Code) } if w.Body.String() == "{\"parsed\":\"bar\"}\n" { @@ -430,7 +430,7 @@ func TestBindingJSONMalformed(t *testing.T) { r.ServeHTTP(w, req) if w.Code != 400 { - t.Errorf("Response code should be Bad request, was: %s", w.Code) + t.Errorf("Response code should be Bad request, was: %d", w.Code) } if w.Body.String() == "{\"parsed\":\"bar\"}\n" { t.Errorf("Response should not be {\"parsed\":\"bar\"}, was: %s", w.Body.String())