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

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/json"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)2627type Error struct {28	Err  error29	Type ErrorType30	Meta interface{}31}3233type errorMsgs []*Error3435var _ 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}6768// MarshalJSON implements the json.Marshaller interface.69func (msg *Error) MarshalJSON() ([]byte, error) {70	return json.Marshal(msg.JSON())71}7273// Error implements the error interface74func (msg Error) Error() string {75	return msg.Err.Error()76}7778func (msg *Error) IsType(flags ErrorType) bool {79	return (msg.Type & flags) > 080}8182// ByType returns a readonly copy filtered the byte.83// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic.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}99100// Last returns the last error in the slice. It returns nil if the array is empty.101// Shortcut for errors[len(errors)-1].102func (a errorMsgs) Last() *Error {103	if length := len(a); length > 0 {104		return a[length-1]105	}106	return nil107}108109// Errors returns an array will all the error messages.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 {151		fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)152		if msg.Meta != nil {153			fmt.Fprintf(&buffer, "     Meta: %v\n", msg.Meta)154		}155	}156	return buffer.String()157}