drpzr64329 2014-02-19 07:20
浏览 42
已采纳

如何正确重构错误处理?

I'm just starting with Go so I'm still not used to its patterns.

I have a web server that serves as a proxy to other remote services. I'm using mux to map routes to handlers, the code it's using App Engine.

// imports ommited.

func init() {
  m = mux.NewRouter()
  m.HandleFunc("/ponies", listPonies)
  m.HandleFunc("/rainbows", listRainbows)
  http.Handle("/", m)
}

func listPonies(w http.ResponseWriter, r *http.Request) {
  ponies, err := ponyService.getAll()

  if err != nil {
      w.write(err.Error())
      return;
  }

  w.write(string(ponies))
}

func listRainbows(w http.ResponseWriter, r *http.Request) {
  rainbows, err := rainbowService.getAll()

  if err != nil {
      w.write(err.Error())
      return;
  }

  w.write(string(rainbows))
}

I would like to refactor the common code (error handling, converting to string and writing the response) into a single function.

My first attempt was simply defining a common function to call:

func handleErrorAndWriteResponse(w http.ResponseWriter, obj Stringer, err error) {
  if err != nil {
      w.write(err.Error())
      return;
  }

  w.write(string(obj))
}

And call it like this

func listPonies(w http.ResponseWriter, r *http.Request) {
  handleErrorAndWriteResponse(w, ponyService.getAll())       
}

func listRainbows(w http.ResponseWriter, r *http.Request) {
  handleErrorAndWriteResponse(w, rainbowService.getAll())
}

But

  1. It doesn't work. I get an insufficient arguments error. It probably has to do with mixing the multiple response values from the services that don't translate directly into arguments of the function called.
  2. I don't like the idea of passing the error arguments around. Maybe that's fine, it just looks dirty to me. Then again, I don't know much about Go yet.

What's the "right way" (or the Go way) to do this?

  • 写回答

1条回答 默认 最新

  • dongxie3352 2014-02-19 07:53
    关注

    There is an blog post on golang.org about error handling that specifically talks about error handling in an AppEngine application. Specifically, check out the "Simplifying repetitive error handling" section.

    The way to do error handling is basically to let your handle functions return an error in case they fail and then wrap the call to them with the common error handler:

    type appHandler func(http.ResponseWriter, *http.Request) error
    
    func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        if err := fn(w, r); err != nil {
            http.Error(w, err.Error(), 500)
        }
    }
    
    func listPonies(w http.ResponseWriter, r *http.Request) error {
        ponies, err := ponyService.getAll()
    
        if err != nil {
            return err;
        }
    
        w.write(string(ponies))
    }
    

    You would also need to register your handlers differently:

    func init() {
        http.Handle("/view", appHandler(listPonies))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现