1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-18 08:26:45 +02:00
woodpecker/vendor/github.com/gin-gonic/gin/errors.go

162 lines
3.2 KiB
Go
Raw Normal View History

2015-05-22 20:37:40 +02:00
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gin
import (
"bytes"
2015-09-30 03:21:17 +02:00
"encoding/json"
2015-05-22 20:37:40 +02:00
"fmt"
2015-09-30 03:21:17 +02:00
"reflect"
2015-05-22 20:37:40 +02:00
)
2015-09-30 03:21:17 +02:00
type ErrorType uint64
2015-05-22 20:37:40 +02:00
const (
2015-09-30 03:21:17 +02:00
ErrorTypeBind ErrorType = 1 << 63 // used when c.Bind() fails
ErrorTypeRender ErrorType = 1 << 62 // used when c.Render() fails
ErrorTypePrivate ErrorType = 1 << 0
ErrorTypePublic ErrorType = 1 << 1
ErrorTypeAny ErrorType = 1<<64 - 1
ErrorTypeNu = 2
)
type (
Error struct {
Err error
Type ErrorType
Meta interface{}
}
errorMsgs []*Error
2015-05-22 20:37:40 +02:00
)
2015-09-30 03:21:17 +02:00
var _ error = &Error{}
func (msg *Error) SetType(flags ErrorType) *Error {
msg.Type = flags
return msg
}
func (msg *Error) SetMeta(data interface{}) *Error {
msg.Meta = data
return msg
}
func (msg *Error) JSON() interface{} {
json := H{}
if msg.Meta != nil {
value := reflect.ValueOf(msg.Meta)
switch value.Kind() {
case reflect.Struct:
return msg.Meta
case reflect.Map:
for _, key := range value.MapKeys() {
json[key.String()] = value.MapIndex(key).Interface()
}
default:
json["meta"] = msg.Meta
}
}
if _, ok := json["error"]; !ok {
json["error"] = msg.Error()
}
return json
}
// Implements the json.Marshaller interface
func (msg *Error) MarshalJSON() ([]byte, error) {
return json.Marshal(msg.JSON())
}
// Implements the error interface
func (msg *Error) Error() string {
return msg.Err.Error()
2015-05-22 20:37:40 +02:00
}
2015-09-30 03:21:17 +02:00
func (msg *Error) IsType(flags ErrorType) bool {
return (msg.Type & flags) > 0
}
2015-05-22 20:37:40 +02:00
2015-09-30 03:21:17 +02:00
// Returns a readonly copy filterd the byte.
// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic
func (a errorMsgs) ByType(typ ErrorType) errorMsgs {
2015-05-22 20:37:40 +02:00
if len(a) == 0 {
2015-09-30 03:21:17 +02:00
return nil
}
if typ == ErrorTypeAny {
2015-05-22 20:37:40 +02:00
return a
}
2015-09-30 03:21:17 +02:00
var result errorMsgs = nil
2015-05-22 20:37:40 +02:00
for _, msg := range a {
2015-09-30 03:21:17 +02:00
if msg.IsType(typ) {
2015-05-22 20:37:40 +02:00
result = append(result, msg)
}
}
return result
}
2015-09-30 03:21:17 +02:00
// Returns the last error in the slice. It returns nil if the array is empty.
// Shortcut for errors[len(errors)-1]
func (a errorMsgs) Last() *Error {
length := len(a)
if length > 0 {
return a[length-1]
}
return nil
}
// Returns an array will all the error messages.
// Example
// ```
// c.Error(errors.New("first"))
// c.Error(errors.New("second"))
// c.Error(errors.New("third"))
// c.Errors.Errors() // == []string{"first", "second", "third"}
// ``
2015-05-22 20:37:40 +02:00
func (a errorMsgs) Errors() []string {
if len(a) == 0 {
2015-09-30 03:21:17 +02:00
return nil
2015-05-22 20:37:40 +02:00
}
errorStrings := make([]string, len(a))
for i, err := range a {
2015-09-30 03:21:17 +02:00
errorStrings[i] = err.Error()
2015-05-22 20:37:40 +02:00
}
return errorStrings
}
2015-09-30 03:21:17 +02:00
func (a errorMsgs) JSON() interface{} {
switch len(a) {
case 0:
return nil
case 1:
return a.Last().JSON()
default:
json := make([]interface{}, len(a))
for i, err := range a {
json[i] = err.JSON()
}
return json
}
}
func (a errorMsgs) MarshalJSON() ([]byte, error) {
return json.Marshal(a.JSON())
}
2015-05-22 20:37:40 +02:00
func (a errorMsgs) String() string {
if len(a) == 0 {
return ""
}
var buffer bytes.Buffer
for i, msg := range a {
2015-09-30 03:21:17 +02:00
fmt.Fprintf(&buffer, "Error #%02d: %s\n", (i + 1), msg.Err)
if msg.Meta != nil {
fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
}
2015-05-22 20:37:40 +02:00
}
return buffer.String()
}