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 Markdown在VScode编辑器下插入视频
  • ¥15 stm32c8t6工程,使用hal库
  • ¥100 有偿求易语言word文档取doc和docx页数方法或模块
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低
  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡