1 Star 0 Fork 0

王南北丶/direwolf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
error.go 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
王南北丶 提交于 2019-12-27 17:34 . add response method: Json, JsonGet
package direwolf
import (
"errors"
"fmt"
"runtime"
"strconv"
"time"
)
var (
ErrRequestBody = errors.New("request body can`t coexists with PostForm")
ErrTimeout = errors.New("reqeust timeout")
)
type RedirectError struct {
RedirectNum int
}
func (e *RedirectError) Error() string {
return "exceeded the maximum number of redirects: " + strconv.Itoa(e.RedirectNum)
}
type Error struct {
// wrapped error
err error
msg string
// file path and name
file string
fileLine int
time string
}
func (e *Error) Error() string {
_, ok := e.err.(interface {
Unwrap() error
})
if ok {
return fmt.Sprintf("%s - %s:%d\n%s\n%s", e.time, e.file, e.fileLine, e.msg, e.err.Error())
}
return fmt.Sprintf("%s - %s:%d\n%s\n\n%s\n", e.time, e.file, e.fileLine, e.msg, e.err.Error())
}
func (e *Error) Unwrap() error {
if e.err != nil {
return e.err
}
return nil
}
// WrapErr will wrap a error with some information: filename, line, time and some message.
func WrapErr(err error, msg string) error {
_, file, line, _ := runtime.Caller(1)
return &Error{
err: err,
msg: msg,
file: file,
fileLine: line,
time: time.Now().Format("2006-01-02 15:04:05"),
}
}
// WrapErr will wrap a error with some information: filename, line, time and some message.
// You can format message of error.
func WrapErrf(err error, format string, args ...interface{}) error {
msg := fmt.Sprintf(format, args...)
_, file, line, _ := runtime.Caller(1)
return &Error{
err: err,
msg: msg,
file: file,
fileLine: line,
time: time.Now().Format("2006-01-02 15:04:05"),
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/wnanbei/direwolf.git
git@gitee.com:wnanbei/direwolf.git
wnanbei
direwolf
direwolf
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385