如果需要自定义api请求返回的http状态码和错误内容,可以通过 httpx.SetErrorHandler 来设置。示例如下:
// 返回的结构体,json格式的body
type Message struct {
    Code int    `json:"code"`
    Desc string `json:"desc"`
}
// 定义错误处理函数
func errorHandler(err error) (int, interface{}) {
    return http.StatusConflict, Message{
        Code: -1,
        Desc: err.Error(),
    }
}
func main() {
    // 初始化,忽略了部分代码
    ctx := svc.NewServiceContext(c)
    server := rest.MustNewServer(c.RestConf)
    defer server.Stop()
    // 设置错误处理函数
    httpx.SetErrorHandler(errorHandler)
    handler.RegisterHandlers(server, ctx)
    server.Start()
}这样自定义之后, httpx.Error(...) 调用会先经过自定义的 errorHandler 处理再返回。
- errorHandler返回的- int作为- http status code返回客户端
- 如果 errorHandler返回的interface{}是error类型的话,那么会直接用err.Error()的内容以非json的格式返回客户端,不是error的话,那么会marshal成json再返回
 
  
 