Using log.Panic instead

This commit is contained in:
Manu Mtz-Almeida 2015-03-23 05:50:10 +01:00
parent 34b1d0262e
commit 3e3ced70d4
6 changed files with 15 additions and 12 deletions

View File

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

View File

@ -8,6 +8,7 @@ import (
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
"errors" "errors"
"log"
"net/http" "net/http"
"reflect" "reflect"
"strconv" "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 // https://github.com/codegangsta/martini-contrib/pull/34#issuecomment-29683659
func ensureNotPointer(obj interface{}) { func ensureNotPointer(obj interface{}) {
if reflect.TypeOf(obj).Kind() == reflect.Ptr { if reflect.TypeOf(obj).Kind() == reflect.Ptr {
panic("Pointers are not accepted as binding models") log.Panic("Pointers are not accepted as binding models")
} }
} }

View File

@ -195,7 +195,7 @@ func (c *Context) Get(key string) (interface{}, error) {
func (c *Context) MustGet(key string) interface{} { func (c *Context) MustGet(key string) interface{} {
value, err := c.Get(key) value, err := c.Get(key)
if err != nil { if err != nil {
log.Panicf(err.Error()) log.Panic(err.Error())
} }
return value return value
} }
@ -208,7 +208,7 @@ func ipInMasks(ip net.IP, masks []interface{}) bool {
switch t := proxy.(type) { switch t := proxy.(type) {
case string: case string:
if _, mask, err = net.ParseCIDR(t); err != nil { if _, mask, err = net.ParseCIDR(t); err != nil {
panic(err) log.Panic(err)
} }
case net.IP: case net.IP:
mask = &net.IPNet{IP: t, Mask: net.CIDRMask(len(t)*8, len(t)*8)} 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: case MIMEHTML:
data := chooseData(config.HTMLData, config.Data) data := chooseData(config.HTMLData, config.Data)
if len(config.HTMLPath) == 0 { 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) 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 { func (c *Context) NegotiateFormat(offered ...string) string {
if len(offered) == 0 { 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 { if c.accepted == nil {
c.accepted = parseAccept(c.Request.Header.Get("Accept")) c.accepted = parseAccept(c.Request.Header.Get("Accept"))

View File

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

View File

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

View File

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