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 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢