owned this note changed 4 years ago
Linked with GitHub

議程3 - Survive under the crap Go error system - 郭學聰

tags: GopherCon TW 2020 Agenda

歡迎來到 https://hackmd.io/@Golang-Taipei/GopherCon2020 共筆

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

點擊本頁上方的 開始用 Markdown 一起寫筆記!
手機版請點選上方 按鈕展開議程列表。

[投影片連結]

https://hackmd.io/@fieliapm/SJYqZtpuv#/

Golang Error Handling Overview

f, err := os.Open("...") if err != nil { return "QQ", err }

Error 5Ws

  • Wh
    • title
  • Why
    • description
  • Which
    • type
  • When
    • line number
  • Where
    • call stack

For end users:

  • What & why

For package importer

  • Which

For package devs

Golang error system

原生只有 title, description
1.13 前是「黑暗期」
官方 'errors' package

  • Wrap and unwrap
    • fmt.Errorf
    • unwrap
  • golang.org/x/xerrors
    • Frame
      • New & Wrap
        • Attach current function & line number
      • Inspect
        • Print current function & line number
        • 可用 "%+v" format string 得到 call stack
  • github.com/pkg/errors

Advice

  • Wrapping: Wrap as early as possible
  • Inspection: Collect and log error at entry function or middleware
  • Displaying: Only show the most outer error message to enduser

Fail Fast

  • When error occurs:
    • Stop immediately
    • Report the error as early as possible
Select a repo