Преглед на файлове

Using log.Panic instead

Manu Mtz-Almeida преди 10 години
родител
ревизия
3e3ced70d4
променени са 6 файла, в които са добавени 15 реда и са изтрити 12 реда
  1. 3 2
      auth.go
  2. 2 1
      binding/binding.go
  3. 4 4
      context.go
  4. 1 1
      mode.go
  5. 2 2
      recovery_test.go
  6. 3 2
      utils.go

+ 3 - 2
auth.go

@@ -9,6 +9,7 @@ import (
 	"encoding/base64"
 	"errors"
 	"fmt"
+	"log"
 	"sort"
 )
 
@@ -60,12 +61,12 @@ func BasicAuth(accounts Accounts) HandlerFunc {
 
 func processAccounts(accounts Accounts) authPairs {
 	if len(accounts) == 0 {
-		panic("Empty list of authorized credentials")
+		log.Panic("Empty list of authorized credentials")
 	}
 	pairs := make(authPairs, 0, len(accounts))
 	for user, password := range accounts {
 		if len(user) == 0 {
-			panic("User can not be empty")
+			log.Panic("User can not be empty")
 		}
 		base := user + ":" + password
 		value := "Basic " + base64.StdEncoding.EncodeToString([]byte(base))

+ 2 - 1
binding/binding.go

@@ -8,6 +8,7 @@ import (
 	"encoding/json"
 	"encoding/xml"
 	"errors"
+	"log"
 	"net/http"
 	"reflect"
 	"strconv"
@@ -203,7 +204,7 @@ func setWithProperType(valueKind reflect.Kind, val string, structField reflect.V
 // https://github.com/codegangsta/martini-contrib/pull/34#issuecomment-29683659
 func ensureNotPointer(obj interface{}) {
 	if reflect.TypeOf(obj).Kind() == reflect.Ptr {
-		panic("Pointers are not accepted as binding models")
+		log.Panic("Pointers are not accepted as binding models")
 	}
 }
 

+ 4 - 4
context.go

@@ -195,7 +195,7 @@ func (c *Context) Get(key string) (interface{}, error) {
 func (c *Context) MustGet(key string) interface{} {
 	value, err := c.Get(key)
 	if err != nil {
-		log.Panicf(err.Error())
+		log.Panic(err.Error())
 	}
 	return value
 }
@@ -208,7 +208,7 @@ func ipInMasks(ip net.IP, masks []interface{}) bool {
 		switch t := proxy.(type) {
 		case string:
 			if _, mask, err = net.ParseCIDR(t); err != nil {
-				panic(err)
+				log.Panic(err)
 			}
 		case net.IP:
 			mask = &net.IPNet{IP: t, Mask: net.CIDRMask(len(t)*8, len(t)*8)}
@@ -402,7 +402,7 @@ func (c *Context) Negotiate(code int, config Negotiate) {
 	case MIMEHTML:
 		data := chooseData(config.HTMLData, config.Data)
 		if len(config.HTMLPath) == 0 {
-			panic("negotiate config is wrong. html path is needed")
+			log.Panic("negotiate config is wrong. html path is needed")
 		}
 		c.HTML(code, config.HTMLPath, data)
 
@@ -417,7 +417,7 @@ func (c *Context) Negotiate(code int, config Negotiate) {
 
 func (c *Context) NegotiateFormat(offered ...string) string {
 	if len(offered) == 0 {
-		panic("you must provide at least one offer")
+		log.Panic("you must provide at least one offer")
 	}
 	if c.accepted == nil {
 		c.accepted = parseAccept(c.Request.Header.Get("Accept"))

+ 1 - 1
mode.go

@@ -43,7 +43,7 @@ func SetMode(value string) {
 	case TestMode:
 		gin_mode = testCode
 	default:
-		panic("gin mode unknown: " + value)
+		log.Panic("gin mode unknown: " + value)
 	}
 	mode_name = value
 }

+ 2 - 2
recovery_test.go

@@ -18,7 +18,7 @@ func TestPanicInHandler(t *testing.T) {
 	r := New()
 	r.Use(Recovery())
 	r.GET("/recovery", func(_ *Context) {
-		panic("Oupps, Houston, we have a problem")
+		log.Panic("Oupps, Houston, we have a problem")
 	})
 
 	// RUN
@@ -40,7 +40,7 @@ func TestPanicWithAbort(t *testing.T) {
 	r.Use(Recovery())
 	r.GET("/recovery", func(c *Context) {
 		c.AbortWithStatus(400)
-		panic("Oupps, Houston, we have a problem")
+		log.Panic("Oupps, Houston, we have a problem")
 	})
 
 	// RUN

+ 3 - 2
utils.go

@@ -6,6 +6,7 @@ package gin
 
 import (
 	"encoding/xml"
+	"log"
 	"reflect"
 	"runtime"
 	"strings"
@@ -49,7 +50,7 @@ func filterFlags(content string) string {
 func chooseData(custom, wildcard interface{}) interface{} {
 	if custom == nil {
 		if wildcard == nil {
-			panic("negotiation config is invalid")
+			log.Panic("negotiation config is invalid")
 		}
 		return wildcard
 	}
@@ -71,7 +72,7 @@ func parseAccept(acceptHeader string) (parts []string) {
 func lastChar(str string) uint8 {
 	size := len(str)
 	if size == 0 {
-		panic("The length of the string can't be 0")
+		log.Panic("The length of the string can't be 0")
 	}
 	return str[size-1]
 }