errors.go
This file
Path
errors.go
169 lines · 3.7 KiB · Go
Last changed by
Added some comments to avoid having golint warnings (#1619)root@andrea:~#before adoption8fb21a8
History of this fileTake it
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package gin67import (8 "bytes"9 "fmt"10 "reflect"1112 "github.com/gin-gonic/gin/internal/json"13)1415// ErrorType is an unsigned 64-bit error code as defined in the gin spec.16type ErrorType uint641718const (19 // ErrorTypeBind is used when Context.Bind() fails.20 ErrorTypeBind ErrorType = 1 << 6321 // ErrorTypeRender is used when Context.Render() fails.22 ErrorTypeRender ErrorType = 1 << 6223 // ErrorTypePrivate indicates a private error.24 ErrorTypePrivate ErrorType = 1 << 025 // ErrorTypePublic indicates a public error.26 ErrorTypePublic ErrorType = 1 << 127 // ErrorTypeAny indicates any other error.28 ErrorTypeAny ErrorType = 1<<64 - 129 // ErrorTypeNu indicates any other error.30 ErrorTypeNu = 231)3233// Error represents a error's specification.34type Error struct {35 Err error36 Type ErrorType37 Meta interface{}38}3940type errorMsgs []*Error4142var _ error = &Error{}4344// SetType sets the error's type.45func (msg *Error) SetType(flags ErrorType) *Error {46 msg.Type = flags47 return msg48}4950// SetMeta sets the error's meta data.51func (msg *Error) SetMeta(data interface{}) *Error {52 msg.Meta = data53 return msg54}5556// JSON creates a properly formated JSON57func (msg *Error) JSON() interface{} {58 json := H{}59 if msg.Meta != nil {60 value := reflect.ValueOf(msg.Meta)61 switch value.Kind() {62 case reflect.Struct:63 return msg.Meta64 case reflect.Map:65 for _, key := range value.MapKeys() {66 json[key.String()] = value.MapIndex(key).Interface()67 }68 default:69 json["meta"] = msg.Meta70 }71 }72 if _, ok := json["error"]; !ok {73 json["error"] = msg.Error()74 }75 return json76}7778// MarshalJSON implements the json.Marshaller interface.79func (msg *Error) MarshalJSON() ([]byte, error) {80 return json.Marshal(msg.JSON())81}8283// Error implements the error interface.84func (msg Error) Error() string {85 return msg.Err.Error()86}8788// IsType judges one error.89func (msg *Error) IsType(flags ErrorType) bool {90 return (msg.Type & flags) > 091}9293// ByType returns a readonly copy filtered the byte.94// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic.95func (a errorMsgs) ByType(typ ErrorType) errorMsgs {96 if len(a) == 0 {97 return nil98 }99 if typ == ErrorTypeAny {100 return a101 }102 var result errorMsgs103 for _, msg := range a {104 if msg.IsType(typ) {105 result = append(result, msg)106 }107 }108 return result109}110111// Last returns the last error in the slice. It returns nil if the array is empty.112// Shortcut for errors[len(errors)-1].113func (a errorMsgs) Last() *Error {114 if length := len(a); length > 0 {115 return a[length-1]116 }117 return nil118}119120// Errors returns an array will all the error messages.121// Example:122// c.Error(errors.New("first"))123// c.Error(errors.New("second"))124// c.Error(errors.New("third"))125// c.Errors.Errors() // == []string{"first", "second", "third"}126func (a errorMsgs) Errors() []string {127 if len(a) == 0 {128 return nil129 }130 errorStrings := make([]string, len(a))131 for i, err := range a {132 errorStrings[i] = err.Error()133 }134 return errorStrings135}136137func (a errorMsgs) JSON() interface{} {138 switch len(a) {139 case 0:140 return nil141 case 1:142 return a.Last().JSON()143 default:144 json := make([]interface{}, len(a))145 for i, err := range a {146 json[i] = err.JSON()147 }148 return json149 }150}151152// MarshalJSON implements the json.Marshaller interface.153func (a errorMsgs) MarshalJSON() ([]byte, error) {154 return json.Marshal(a.JSON())155}156157func (a errorMsgs) String() string {158 if len(a) == 0 {159 return ""160 }161 var buffer bytes.Buffer162 for i, msg := range a {163 fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)164 if msg.Meta != nil {165 fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)166 }167 }168 return buffer.String()169}