duanhuan8983 2018-05-05 05:42
浏览 24
已采纳

如何避免在连续的错误处理中重复自己

I have the following handling of consecutive errors:

nodes, err := model.AllNodes()
if err != nil {                           // This error handling  
    pr := progressRes{                    // is Just a copy of
        Type:        progressResponse,    // error handling
        Message:     err.Error(),         // for the next error,
        MessageType: errorMessage,        // what is the
        Progress:    100,                 // best practice
    }                                     // to avoid
    go response(ws, pr.json())            // repeating myself here
    return                                // without making the code
}                                         // complicated
links, err := model.AllLinks()
if err != nil {
    pr := progressRes{
        Type:        progressResponse,
        Message:     err.Error(),
        MessageType: errorMessage,
        Progress:    100,
    }
    go response(ws, pr.json())
    return
}

What is the best practice to avoid repeating myself in the above code without making the code complex? I can think of adding a new func, however I just thought there might be a better idea which I'm not aware of.

  • 写回答

3条回答 默认 最新

  • douxiang3978 2018-05-05 06:55
    关注

    If you are repeating a number of steps in multiple places the correct approach would be to abstract away those steps into a procedure, which is what programming is about. This applies to error handling just as well as to any other parts of your program.

    One option:

    func allNodesAndLinks() ([]*Node, []*Link, error) {
        nodes, err := model.AllNodes()
        if err != nil {
            return nil, nil, err
        }
        links, err := model.AllLinks()
        if err != nil {
            return nil, nil, err
        }
        return nodes, links, nil
    }
    
    // ...
    
    nodes, links, err := allNodesAndLinks()
    if err != nil {
        pr := progressRes{
            Type:        progressResponse,
            Message:     err.Error(),
            MessageType: errorMessage,
            Progress:    100,
        }
        go response(ws, pr.json())
        return
    }
    

    Another option:

    func respondWithError(ws io.Writer, err error) {
        pr := progressRes{
            Type:        progressResponse,
            Message:     err.Error(),
            MessageType: errorMessage,
            Progress:    100,
        }
        response(ws, pr.json())
    }
    
    // ...
    
    nodes, err := model.AllNodes()
    if err != nil {
        go respondWithError(ws, err)
        return
    }
    links, err := model.AllLinks()
    if err != nil {
        go respondWithError(ws, err)
        return
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码