Skip to content

Commit

Permalink
Fixes errors
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed May 12, 2015
1 parent 421793b commit 99694bb
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 42 deletions.
30 changes: 17 additions & 13 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ type Params []Param

// ByName returns the value of the first Param which key matches the given name.
// If no matching Param is found, an empty string is returned.
func (ps Params) ByName(name string) string {
func (ps Params) Get(name string) (string, bool) {
for _, entry := range ps {
if entry.Key == name {
return entry.Value
return entry.Value, true
}
}
return ""
return "", false
}

func (ps Params) ByName(name string) (va string) {
va, _ = ps.Get(name)
return
}

// Context is the most important part of gin. It allows us to pass variables between middleware,
Expand Down Expand Up @@ -138,9 +143,9 @@ func (c *Context) Fail(code int, err error) {

func (c *Context) ErrorTyped(err error, typ int, meta interface{}) {
c.Errors = append(c.Errors, errorMsg{
Err: err.Error(),
Type: typ,
Meta: meta,
Error: err,
Flags: typ,
Meta: meta,
})
}

Expand All @@ -154,7 +159,7 @@ func (c *Context) Error(err error, meta interface{}) {
func (c *Context) LastError() error {
nuErrors := len(c.Errors)
if nuErrors > 0 {
return errors.New(c.Errors[nuErrors-1].Err)
return c.Errors[nuErrors-1].Error
}
return nil
}
Expand Down Expand Up @@ -203,8 +208,7 @@ func (c *Context) DefaultParamValue(key, defaultValue string) string {
}

func (c *Context) paramValue(key string) (string, bool) {
va := c.Params.ByName(key)
return va, len(va) > 0
return c.Params.Get(key)
}

func (c *Context) formValue(key string) (string, bool) {
Expand All @@ -231,17 +235,17 @@ func (c *Context) postFormValue(key string) (string, bool) {

// Sets a new pair key/value just for the specified context.
// It also lazy initializes the hashmap.
func (c *Context) Set(key string, item interface{}) {
func (c *Context) Set(key string, value interface{}) {
if c.Keys == nil {
c.Keys = make(map[string]interface{})
}
c.Keys[key] = item
c.Keys[key] = value
}

// Get returns the value for the given key or an error if the key does not exist.
func (c *Context) Get(key string) (value interface{}, ok bool) {
func (c *Context) Get(key string) (value interface{}, exists bool) {
if c.Keys != nil {
value, ok = c.Keys[key]
value, exists = c.Keys[key]
}
return
}
Expand Down
12 changes: 6 additions & 6 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ func TestContextError(t *testing.T) {
assert.Equal(t, c.Errors.String(), "Error #01: first error\n Meta: some data\n"+
"Error #02: second error\n Meta: some data 2\n")

assert.Equal(t, c.Errors[0].Err, "first error")
assert.Equal(t, c.Errors[0].Error, errors.New("first error"))
assert.Equal(t, c.Errors[0].Meta, "some data")
assert.Equal(t, c.Errors[0].Type, ErrorTypeExternal)
assert.Equal(t, c.Errors[0].Flags, ErrorTypeExternal)

assert.Equal(t, c.Errors[1].Err, "second error")
assert.Equal(t, c.Errors[1].Error, errors.New("second error"))
assert.Equal(t, c.Errors[1].Meta, "some data 2")
assert.Equal(t, c.Errors[1].Type, ErrorTypeExternal)
assert.Equal(t, c.Errors[1].Flags, ErrorTypeExternal)
}

func TestContextTypedError(t *testing.T) {
Expand All @@ -337,11 +337,11 @@ func TestContextTypedError(t *testing.T) {
c.ErrorTyped(errors.New("interno 2"), ErrorTypeInternal, nil)

for _, err := range c.Errors.ByType(ErrorTypeExternal) {
assert.Equal(t, err.Type, ErrorTypeExternal)
assert.Equal(t, err.Flags, ErrorTypeExternal)
}

for _, err := range c.Errors.ByType(ErrorTypeInternal) {
assert.Equal(t, err.Type, ErrorTypeInternal)
assert.Equal(t, err.Flags, ErrorTypeInternal)
}
}

Expand Down
19 changes: 8 additions & 11 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ import (
)

const (
ErrorTypePrivate = 1 << iota
ErrorTypePublic = 1 << iota
)

const (
ErrorMaskAny = 0xffffffff
ErrorTypeInternal = 1 << iota
ErrorTypeExternal = 1 << iota
ErrorTypeAny = 0xffffffff
)

// Used internally to collect errors that occurred during an http request.
type errorMsg struct {
Error error `json:"error"`
Type int `json:"-"`
Flags int `json:"-"`
Meta interface{} `json:"meta"`
}

Expand All @@ -33,7 +30,7 @@ func (a errorMsgs) ByType(typ int) errorMsgs {
}
result := make(errorMsgs, 0, len(a))
for _, msg := range a {
if msg.Type&typ > 0 {
if msg.Flags&typ > 0 {
result = append(result, msg)
}
}
Expand All @@ -44,11 +41,11 @@ func (a errorMsgs) Errors() []string {
if len(a) == 0 {
return []string{}
}
errors := make([]string, len(a))
errorStrings := make([]string, len(a))
for i, err := range a {
errors[i] = err.Error.Error()
errorStrings[i] = err.Error.Error()
}
return errors
return errorStrings
}

func (a errorMsgs) String() string {
Expand Down
8 changes: 4 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
)

func ErrorLogger() HandlerFunc {
return ErrorLoggerT(ErrorTypeAll)
return ErrorLoggerT(ErrorTypeAny)
}

func ErrorLoggerT(typ int) HandlerFunc {
Expand All @@ -31,17 +31,17 @@ func ErrorLoggerT(typ int) HandlerFunc {

if !c.Writer.Written() {
if errs := c.Errors.ByType(typ); len(errs) > 0 {
c.JSON(-1, errs)
c.JSON(-1, errs.Errors())
}
}
}
}

func Logger() HandlerFunc {
return LoggerWithFile(DefaultWriter)
return LoggerWithWriter(DefaultWriter)
}

func LoggerWithFile(out io.Writer) HandlerFunc {
func LoggerWithWriter(out io.Writer) HandlerFunc {
return func(c *Context) {
// Start timer
start := time.Now()
Expand Down
2 changes: 1 addition & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {
func TestLogger(t *testing.T) {
buffer := new(bytes.Buffer)
router := New()
router.Use(LoggerWithFile(buffer))
router.Use(LoggerWithWriter(buffer))
router.GET("/example", func(c *Context) {})

performRequest(router, "GET", "/example")
Expand Down
4 changes: 2 additions & 2 deletions recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ var (
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
// While Gin is in development mode, Recovery will also output the panic as HTML.
func Recovery() HandlerFunc {
return RecoveryWithFile(DefaultWriter)
return RecoveryWithWriter(DefaultWriter)
}

func RecoveryWithFile(out io.Writer) HandlerFunc {
func RecoveryWithWriter(out io.Writer) HandlerFunc {
var logger *log.Logger
if out != nil {
logger = log.New(out, "", log.LstdFlags)
Expand Down
4 changes: 2 additions & 2 deletions recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestPanicInHandler(t *testing.T) {
buffer := new(bytes.Buffer)
router := New()
router.Use(RecoveryWithFile(buffer))
router.Use(RecoveryWithWriter(buffer))
router.GET("/recovery", func(_ *Context) {
panic("Oupps, Houston, we have a problem")
})
Expand All @@ -30,7 +30,7 @@ func TestPanicInHandler(t *testing.T) {
// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.
func TestPanicWithAbort(t *testing.T) {
router := New()
router.Use(RecoveryWithFile(nil))
router.Use(RecoveryWithWriter(nil))
router.GET("/recovery", func(c *Context) {
c.AbortWithStatus(400)
panic("Oupps, Houston, we have a problem")
Expand Down
3 changes: 1 addition & 2 deletions response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package gin

import (
"bufio"
"log"
"net"
"net/http"
)
Expand Down Expand Up @@ -46,7 +45,7 @@ func (w *responseWriter) WriteHeader(code int) {
if code > 0 {
w.status = code
if w.Written() {
log.Println("[GIN] WARNING. Headers were already written!")
debugPrint("[WARNING] Headers were already written")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestFunctionName(t *testing.T) {
}

func somefunction() {

// this empty function is used by TestFunctionName()
}

func TestJoinPaths(t *testing.T) {
Expand Down

0 comments on commit 99694bb

Please sign in to comment.