gin/errors.go

51 lines
1021 B
Go
Raw Normal View History

2015-03-26 16:10:46 +03: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"
"fmt"
)
const (
ErrorTypeInternal = 1 << iota
ErrorTypeExternal = 1 << iota
ErrorTypeAll = 0xffffffff
)
// Used internally to collect errors that occurred during an http request.
type errorMsg struct {
Err string `json:"error"`
2015-04-08 03:58:35 +03:00
Type int `json:"-"`
2015-03-26 16:10:46 +03:00
Meta interface{} `json:"meta"`
}
type errorMsgs []errorMsg
2015-04-08 03:58:35 +03:00
func (a errorMsgs) ByType(typ int) errorMsgs {
2015-03-26 16:10:46 +03:00
if len(a) == 0 {
return a
}
result := make(errorMsgs, 0, len(a))
for _, msg := range a {
if msg.Type&typ > 0 {
result = append(result, msg)
}
}
return result
}
func (a errorMsgs) String() string {
if len(a) == 0 {
return ""
}
var buffer bytes.Buffer
for i, msg := range a {
2015-04-09 13:15:02 +03:00
text := fmt.Sprintf("Error #%02d: %s\n Meta: %v\n", (i + 1), msg.Err, msg.Meta)
2015-03-26 16:10:46 +03:00
buffer.WriteString(text)
}
return buffer.String()
}