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
The walk stopped after 200 commits: lines older than ad08765 point at it.
Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 1–11why, one step back1// 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"11add internal package which includes json package (#1504)田欧before adoption72db8ac
line 12why, one step back12 "github.com/gin-gonic/gin/internal/json"Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 13–14why, one step back13)14chore: add some annotations (#1550)田欧before adoption6db092f
line 15why, one step back15// ErrorType is an unsigned 64-bit error code as defined in the gin spec.Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 16–18why, one step back16type ErrorType uint641718const (chore: add some annotations (#1550)田欧before adoption6db092f
lines 19–23why, one step back19 // 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.Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
line 24why, one step back24 ErrorTypePrivate ErrorType = 1 << 0chore: add some annotations (#1550)田欧before adoption6db092f
lines 25–26why, one step back25 // ErrorTypePublic indicates a public error.26 ErrorTypePublic ErrorType = 1 << 1Added some comments to avoid having golint warnings (#1619)root@andrea:~#before adoption8fb21a8
line 27why, one step back27 // ErrorTypeAny indicates any other error.Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
line 28why, one step back28 ErrorTypeAny ErrorType = 1<<64 - 1Added some comments to avoid having golint warnings (#1619)root@andrea:~#before adoption8fb21a8
lines 29–30why, one step back29 // ErrorTypeNu indicates any other error.30 ErrorTypeNu = 2Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 31–32why, one step back31)32chore: add some annotations (#1550)田欧before adoption6db092f
line 33why, one step back33// Error represents a error's specification.Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 34–43why, one step back34type Error struct {35 Err error36 Type ErrorType37 Meta interface{}38}3940type errorMsgs []*Error4142var _ error = &Error{}43chore: add some annotations (#1550)田欧before adoption6db092f
line 44why, one step back44// SetType sets the error's type.Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 45–49why, one step back45func (msg *Error) SetType(flags ErrorType) *Error {46 msg.Type = flags47 return msg48}49chore: add some annotations (#1550)田欧before adoption6db092f
line 50why, one step back50// SetMeta sets the error's meta data.Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 51–55why, one step back51func (msg *Error) SetMeta(data interface{}) *Error {52 msg.Meta = data53 return msg54}55Added some comments to avoid having golint warnings (#1619)root@andrea:~#before adoption8fb21a8
line 56why, one step back56// JSON creates a properly formated JSONMerge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 57–77why, one step back57func (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}77update comment (#1057)田欧before adoptiona8fa424
line 78why, one step back78// MarshalJSON implements the json.Marshaller interface.Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 79–82why, one step back79func (msg *Error) MarshalJSON() ([]byte, error) {80 return json.Marshal(msg.JSON())81}82chore: add some annotations (#1550)田欧before adoption6db092f
line 83why, one step back83// Error implements the error interface.Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 84–87why, one step back84func (msg Error) Error() string {85 return msg.Err.Error()86}87chore: add some annotations (#1550)田欧before adoption6db092f
line 88why, one step back88// IsType judges one error.Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 89–93why, one step back89func (msg *Error) IsType(flags ErrorType) bool {90 return (msg.Type & flags) > 091}9293// ByType returns a readonly copy filtered the byte.update comment (#1057)田欧before adoptiona8fa424
line 94why, one step back94// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic.Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 95–111why, one step back95func (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.update comment (#1057)田欧before adoptiona8fa424
line 112why, one step back112// Shortcut for errors[len(errors)-1].Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 113–151why, one step back113func (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}151chore: add some annotations (#1550)田欧before adoption6db092f
line 152why, one step back152// MarshalJSON implements the json.Marshaller interface.Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 153–162why, one step back153func (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 {Linting and optimizing struct memory signature. (#1184)Boris Borshevskybefore adoption6f94fd0
line 163why, one step back163 fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765
lines 164–169why, one step back164 if msg.Meta != nil {165 fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)166 }167 }168 return buffer.String()169}