Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Виталий Левченко – Go 2: ошибки, их логирование...

Виталий Левченко – Go 2: ошибки, их логирование, и как нам с этим жить

Avatar for GolangMoscow

GolangMoscow

August 29, 2019
Tweet

More Decks by GolangMoscow

Other Decks in Programming

Transcript

  1. Обо мне • 11 лет в индустрии • Go разработчик

    с 2012 • Тимлид • CTO • Product owner @ Uteka • Лидер Go, SRE, Teamlead сообществ Петербурга
  2. Go 2 • https://go.googlesource.com/proposal/+/master/design/ go2draft-error-printing.md • https://go.googlesource.com/proposal/+/master/design/ 32437-try-builtin.md • https://go.googlesource.com/proposal/+/master/design/

    go2draft-error-inspection.md • https://go.googlesource.com/proposal/+/master/design/ go2draft-error-handling-overview.md • https://go.googlesource.com/proposal/+/master/design/ go2draft-error-handling.md
  3. Постановка задачи • Avoid return • Log error • Debug

    error • Log trace • Handle error • Create error
  4. Try / handle func() { defer fmt.HandleErrorf(&err, “open file %s",

    path)
 file := try(os.Open(path)) defer file.Close() }
  5. Try / handle func() { defer fmt.HandleErrorf(&err, “open file %s",

    path)
 file := try(os.Open(path)) defer file.Close() } Abandoned
  6. Постановка задачи • Avoid return (ждём нового try) • Log

    error • Debug error • Log trace • Handle error • Create error
  7. Log error type Formatter interface { Format(p Printer) (next error)

    } type Printer interface { Print(args ...interface{}) Printf(format string, args ...interface{}) Detail() bool }
  8. Log error type Formatter interface { Format(p Printer) (next error)

    } type Printer interface { Print(args ...interface{}) Printf(format string, args ...interface{}) Detail() bool } Abandoned
  9. Постановка задачи • Avoid return (ждём нового try) • Log

    error (use xerror and wait for go1.13) • Debug error • Log trace • Handle error • Create error
  10. Постановка задачи • Avoid return (ждём нового try) • Log

    error (use xerror and wait for go1.13) • Debug error (use explicitly) • Log trace • Handle error • Create error
  11. Постановка задачи • Avoid return (ждём нового try) • Log

    error (use xerror and wait for go1.13) • Debug error (use explicitly) • Log trace (custom wrappers) • Handle error • Create error
  12. Handle error if errors.Is(err, os.ErrExist) { ... } var perr

    *os.PathError if errors.As(err, &perr) { fmt.Println(perr.Path) }
  13. Постановка задачи • Avoid return (ждём нового try) • Log

    error (use xerror and wait for go1.13) • Debug error (use explicitly) • Log trace (custom wrappers and pain) • Handle error (use xerror and wait for go1.13) • Create error
  14. Постановка задачи • Avoid return • Log error (lib +

    custom wrapper) • Debug error (custom) • Log trace (libs and custom) • Handle error (custom) • Create error (custom)
  15. Постановка задачи • Avoid return (or custom panics) • Log

    error (lib + custom wrapper) • Debug error (custom) • Log trace (libs and custom) • Handle error (custom) • Create error (custom)
  16. Custom error type Custom struct { message string error error

    stack xerrors.Frame type string fields []zap.Field }
  17. Custom error func New(e error, msg, type string, fields []zap.Field)

    error interface { Error() string Is(error) bool Unwrap() error AddFields([]zap.Field) Custom EncodeError() []byte }
  18. Custom error type Custom struct { message string error error

    stack xerrors.Frame type string fields []zap.Field } Or use:
 • https://github.com/joomcode/errorx