Documentation
¶
Overview ¶
Package errorhandler 提供自定义错误处理功能
Index ¶
- func Exit(status int)
- type ErrorHandler
- func (e *ErrorHandler) Add(f HandleFunc, status ...int) error
- func (e *ErrorHandler) New(next http.Handler) http.Handler
- func (e *ErrorHandler) NewFunc(next func(http.ResponseWriter, *http.Request)) http.Handler
- func (e *ErrorHandler) Recovery(errlog *log.Logger) recovery.RecoverFunc
- func (e *ErrorHandler) Render(w http.ResponseWriter, status int)
- func (e *ErrorHandler) Set(f HandleFunc, status ...int)
- type HandleFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ErrorHandler ¶
type ErrorHandler struct {
// contains filtered or unexported fields
}
ErrorHandler 错误处理函数的管理
net/http 包中对于错误的处理是通过 http.Error() 进行的, 我们无法直接修改该方法,实现自定义的错误处理功能。 只能对 http.ResponseWriter.WriteHeader() 进行自定义, 在指定的状态下,抛出异常,再通过 recover 实现错误处理。
func (*ErrorHandler) Add ¶
func (e *ErrorHandler) Add(f HandleFunc, status ...int) error
Add 添加针对特写状态码的错误处理函数
func (*ErrorHandler) New ¶
func (e *ErrorHandler) New(next http.Handler) http.Handler
New 构建一个可以捕获错误状态码的 Handler,要求在最外层。
func (*ErrorHandler) NewFunc ¶
func (e *ErrorHandler) NewFunc(next func(http.ResponseWriter, *http.Request)) http.Handler
NewFunc 构建一个可以捕获错误状态码的 Handler,要求在最外层。
func (*ErrorHandler) Recovery ¶
func (e *ErrorHandler) Recovery(errlog *log.Logger) recovery.RecoverFunc
Recovery 生成一个 recovery.RecoverFunc 函数,用于捕获由 panic 触发的事件。
errlog 表示输出调用堆栈信息到日志。可以为空,表示不输出信息。
func (*ErrorHandler) Render ¶
func (e *ErrorHandler) Render(w http.ResponseWriter, status int)
Render 向客户端输出指定状态码的错误内容。
func (*ErrorHandler) Set ¶
func (e *ErrorHandler) Set(f HandleFunc, status ...int)
Set 设置指定状态码对应的处理函数
有则修改,没有则添加
type HandleFunc ¶
type HandleFunc func(http.ResponseWriter, int)
HandleFunc 错误处理函数,对某一固定的状态码可以做专门的处理。
Click to show internal directories.
Click to hide internal directories.