Skip to content
Myr

errors.go

This file

Path

errors.go

174 lines · 3.9 KiB · Go

Last changed by

test support go1.18 (#2990)thinkeroubefore adoption2bde107

History of this file

Take it

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

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

lines 1–36why, 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 "fmt"9 "reflect"10 "strings"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 ErrorType

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 37why, one step back
37 Meta any

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

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

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 51why, one step back
51func (msg *Error) SetMeta(data any) *Error {

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

lines 52–56why, one step back
52 msg.Meta = data
53 return msg54}5556// JSON creates a properly formatted JSON

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 57why, one step back
57func (msg *Error) JSON() any {

Rename some variables (#2393)bestgopherbefore adoptionc9b8535

line 58why, one step back
58 jsonData := H{}

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

lines 59–65why, one step back
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() {

Rename some variables (#2393)bestgopherbefore adoptionc9b8535

line 66why, one step back
66 jsonData[key.String()] = value.MapIndex(key).Interface()

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

lines 67–68why, one step back
67 }
68 default:

Rename some variables (#2393)bestgopherbefore adoptionc9b8535

line 69why, one step back
69 jsonData["meta"] = msg.Meta

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

lines 70–71why, one step back
70 }
71 }

Rename some variables (#2393)bestgopherbefore adoptionc9b8535

lines 72–73why, one step back
72 if _, ok := jsonData["error"]; !ok {
73 jsonData["error"] = msg.Error()

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

line 74why, one step back
74 }

Rename some variables (#2393)bestgopherbefore adoptionc9b8535

line 75why, one step back
75 return jsonData

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

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

implement ".Unwrap() error" on Error type (#2525) (#2526)Georges Varouchasbefore adoptionf969bfa

lines 93–97why, one step back
93// Unwrap returns the wrapped error, to allow interoperability with errors.Is(), errors.As() and errors.Unwrap()
94func (msg *Error) Unwrap() error {95 return msg.Err96}97

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

lines 98–124why, one step back
98// ByType returns a readonly copy filtered the byte.
99// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic.100func (a errorMsgs) ByType(typ ErrorType) errorMsgs {101 if len(a) == 0 {102 return nil103 }104 if typ == ErrorTypeAny {105 return a106 }107 var result errorMsgs108 for _, msg := range a {109 if msg.IsType(typ) {110 result = append(result, msg)111 }112 }113 return result114}115116// Last returns the last error in the slice. It returns nil if the array is empty.117// Shortcut for errors[len(errors)-1].118func (a errorMsgs) Last() *Error {119 if length := len(a); length > 0 {120 return a[length-1]121 }122 return nil123}124

Fix typo (#2926)Tommy Chubefore adoptiond4e72a1

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

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

lines 126–141why, one step back
126// Example:
127// c.Error(errors.New("first"))128// c.Error(errors.New("second"))129// c.Error(errors.New("third"))130// c.Errors.Errors() // == []string{"first", "second", "third"}131func (a errorMsgs) Errors() []string {132 if len(a) == 0 {133 return nil134 }135 errorStrings := make([]string, len(a))136 for i, err := range a {137 errorStrings[i] = err.Error()138 }139 return errorStrings140}141

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 142why, one step back
142func (a errorMsgs) JSON() any {

Rename some variables (#2393)bestgopherbefore adoptionc9b8535

line 143why, one step back
143 switch length := len(a); length {

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

lines 144–148why, one step back
144 case 0:
145 return nil146 case 1:147 return a.Last().JSON()148 default:

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 149why, one step back
149 jsonData := make([]any, length)

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

line 150why, one step back
150 for i, err := range a {

Rename some variables (#2393)bestgopherbefore adoptionc9b8535

line 151why, one step back
151 jsonData[i] = err.JSON()

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

line 152why, one step back
152 }

Rename some variables (#2393)bestgopherbefore adoptionc9b8535

line 153why, one step back
153 return jsonData

fix(tree): reassign fullpath when register new node which the same current node (#2366)vinhha96before adoptiona6e8665

lines 154–174why, one step back
154 }
155}156157// MarshalJSON implements the json.Marshaller interface.158func (a errorMsgs) MarshalJSON() ([]byte, error) {159 return json.Marshal(a.JSON())160}161162func (a errorMsgs) String() string {163 if len(a) == 0 {164 return ""165 }166 var buffer strings.Builder167 for i, msg := range a {168 fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)169 if msg.Meta != nil {170 fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)171 }172 }173 return buffer.String()174}