Skip to content
Myr

errors.go

This file

Path

errors.go

157 lines · 3.2 KiB · Go

Last changed by

Linting and optimizing struct memory signature. (#1184)Boris Borshevskybefore adoption6f94fd0

History of this file

Take it

The walk stopped after 200 commits: lines older than e2212d4 point at it.

v1.1.4Javier Provecho Fernandezbefore adoptione2212d4

lines 1–10why, one step back
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"

Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765

line 11why, one step back
11

refactor(json): make jsonite optional with build tags (#1026)Bo-Yi Wubefore adoptionce670a6

line 12why, one step back
12 "github.com/gin-gonic/gin/json"

v1.1.4Javier Provecho Fernandezbefore adoptione2212d4

lines 13–26why, one step back
13)
1415type ErrorType uint641617const (18 ErrorTypeBind ErrorType = 1 << 63 // used when c.Bind() fails19 ErrorTypeRender ErrorType = 1 << 62 // used when c.Render() fails20 ErrorTypePrivate ErrorType = 1 << 021 ErrorTypePublic ErrorType = 1 << 12223 ErrorTypeAny ErrorType = 1<<64 - 124 ErrorTypeNu = 225)26

separate type define (#975)田欧before adoptiond535fcd

lines 27–31why, one step back
27type Error struct {
28 Err error29 Type ErrorType30 Meta interface{}31}

v1.1.4Javier Provecho Fernandezbefore adoptione2212d4

line 32why, one step back
32

separate type define (#975)田欧before adoptiond535fcd

line 33why, one step back
33type errorMsgs []*Error

v1.1.4Javier Provecho Fernandezbefore adoptione2212d4

lines 34–67why, one step back
34
35var _ error = &Error{}3637func (msg *Error) SetType(flags ErrorType) *Error {38 msg.Type = flags39 return msg40}4142func (msg *Error) SetMeta(data interface{}) *Error {43 msg.Meta = data44 return msg45}4647func (msg *Error) JSON() interface{} {48 json := H{}49 if msg.Meta != nil {50 value := reflect.ValueOf(msg.Meta)51 switch value.Kind() {52 case reflect.Struct:53 return msg.Meta54 case reflect.Map:55 for _, key := range value.MapKeys() {56 json[key.String()] = value.MapIndex(key).Interface()57 }58 default:59 json["meta"] = msg.Meta60 }61 }62 if _, ok := json["error"]; !ok {63 json["error"] = msg.Error()64 }65 return json66}67

update comment (#1057)田欧before adoptiona8fa424

line 68why, one step back
68// MarshalJSON implements the json.Marshaller interface.

v1.1.4Javier Provecho Fernandezbefore adoptione2212d4

lines 69–72why, one step back
69func (msg *Error) MarshalJSON() ([]byte, error) {
70 return json.Marshal(msg.JSON())71}72

update func comment (#981)田欧before adoptionb985857

line 73why, one step back
73// Error implements the error interface

Merge pull request #871 from gin-gonic/developJavier Provecho Fernandezbefore adoptiond459835

line 74why, one step back
74func (msg Error) Error() string {

v1.1.4Javier Provecho Fernandezbefore adoptione2212d4

lines 75–81why, one step back
75 return msg.Err.Error()
76}7778func (msg *Error) IsType(flags ErrorType) bool {79 return (msg.Type & flags) > 080}81

update func comment (#981)田欧before adoptionb985857

line 82why, one step back
82// ByType returns a readonly copy filtered the byte.

update comment (#1057)田欧before adoptiona8fa424

line 83why, one step back
83// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic.

v1.1.4Javier Provecho Fernandezbefore adoptione2212d4

lines 84–99why, one step back
84func (a errorMsgs) ByType(typ ErrorType) errorMsgs {
85 if len(a) == 0 {86 return nil87 }88 if typ == ErrorTypeAny {89 return a90 }91 var result errorMsgs92 for _, msg := range a {93 if msg.IsType(typ) {94 result = append(result, msg)95 }96 }97 return result98}99

update func comment (#981)田欧before adoptionb985857

line 100why, one step back
100// Last returns the last error in the slice. It returns nil if the array is empty.

update comment (#1057)田欧before adoptiona8fa424

line 101why, one step back
101// Shortcut for errors[len(errors)-1].

v1.1.4Javier Provecho Fernandezbefore adoptione2212d4

line 102why, one step back
102func (a errorMsgs) Last() *Error {

update func comment (#981)田欧before adoptionb985857

line 103why, one step back
103 if length := len(a); length > 0 {

v1.1.4Javier Provecho Fernandezbefore adoptione2212d4

lines 104–108why, one step back
104 return a[length-1]
105 }106 return nil107}108

update func comment (#981)田欧before adoptionb985857

line 109why, one step back
109// Errors returns an array will all the error messages.

v1.1.4Javier Provecho Fernandezbefore adoptione2212d4

lines 110–150why, one step back
110// Example:
111// c.Error(errors.New("first"))112// c.Error(errors.New("second"))113// c.Error(errors.New("third"))114// c.Errors.Errors() // == []string{"first", "second", "third"}115func (a errorMsgs) Errors() []string {116 if len(a) == 0 {117 return nil118 }119 errorStrings := make([]string, len(a))120 for i, err := range a {121 errorStrings[i] = err.Error()122 }123 return errorStrings124}125126func (a errorMsgs) JSON() interface{} {127 switch len(a) {128 case 0:129 return nil130 case 1:131 return a.Last().JSON()132 default:133 json := make([]interface{}, len(a))134 for i, err := range a {135 json[i] = err.JSON()136 }137 return json138 }139}140141func (a errorMsgs) MarshalJSON() ([]byte, error) {142 return json.Marshal(a.JSON())143}144145func (a errorMsgs) String() string {146 if len(a) == 0 {147 return ""148 }149 var buffer bytes.Buffer150 for i, msg := range a {

Linting and optimizing struct memory signature. (#1184)Boris Borshevskybefore adoption6f94fd0

line 151why, one step back
151 fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)

v1.1.4Javier Provecho Fernandezbefore adoptione2212d4

lines 152–157why, one step back
152 if msg.Meta != nil {
153 fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)154 }155 }156 return buffer.String()157}