Skip to content
Myr

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 file

Take 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 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"11

add internal package which includes json package (#1504)田欧before adoption72db8ac

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

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

lines 13–14why, one step back
13)
14

chore: add some annotations (#1550)田欧before adoption6db092f

line 15why, one step back
15// 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 back
16type ErrorType uint64
1718const (

chore: add some annotations (#1550)田欧before adoption6db092f

lines 19–23why, one step back
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.

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

line 24why, one step back
24 ErrorTypePrivate ErrorType = 1 << 0

chore: add some annotations (#1550)田欧before adoption6db092f

lines 25–26why, one step back
25 // ErrorTypePublic indicates a public error.
26 ErrorTypePublic ErrorType = 1 << 1

Added some comments to avoid having golint warnings (#1619)root@andrea:~#before adoption8fb21a8

line 27why, one step back
27 // ErrorTypeAny indicates any other error.

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

line 28why, one step back
28 ErrorTypeAny ErrorType = 1<<64 - 1

Added some comments to avoid having golint warnings (#1619)root@andrea:~#before adoption8fb21a8

lines 29–30why, one step back
29 // ErrorTypeNu indicates any other error.
30 ErrorTypeNu = 2

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

lines 31–32why, one step back
31)
32

chore: add some annotations (#1550)田欧before adoption6db092f

line 33why, one step back
33// Error represents a error's specification.

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

lines 34–43why, one step back
34type Error struct {
35 Err error36 Type ErrorType37 Meta interface{}38}3940type errorMsgs []*Error4142var _ error = &Error{}43

chore: add some annotations (#1550)田欧before adoption6db092f

line 44why, one step back
44// SetType sets the error's type.

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

lines 45–49why, one step back
45func (msg *Error) SetType(flags ErrorType) *Error {
46 msg.Type = flags47 return msg48}49

chore: add some annotations (#1550)田欧before adoption6db092f

line 50why, one step back
50// SetMeta sets the error's meta data.

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

lines 51–55why, one step back
51func (msg *Error) SetMeta(data interface{}) *Error {
52 msg.Meta = data53 return msg54}55

Added some comments to avoid having golint warnings (#1619)root@andrea:~#before adoption8fb21a8

line 56why, one step back
56// JSON creates a properly formated JSON

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

lines 57–77why, one step back
57func (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}77

update comment (#1057)田欧before adoptiona8fa424

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

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

lines 79–82why, one step back
79func (msg *Error) MarshalJSON() ([]byte, error) {
80 return json.Marshal(msg.JSON())81}82

chore: add some annotations (#1550)田欧before adoption6db092f

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

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

lines 84–87why, one step back
84func (msg Error) Error() string {
85 return msg.Err.Error()86}87

chore: add some annotations (#1550)田欧before adoption6db092f

line 88why, one step back
88// IsType judges one error.

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

lines 89–93why, one step back
89func (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 back
94// 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 back
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.

update comment (#1057)田欧before adoptiona8fa424

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

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

lines 113–151why, one step back
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}151

chore: add some annotations (#1550)田欧before adoption6db092f

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

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

lines 153–162why, one step back
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 {

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

line 163why, one step back
163 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 back
164 if msg.Meta != nil {
165 fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)166 }167 }168 return buffer.String()169}