Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Виталий Левченко – Go 2: ошибки, их логирование...
Search
GolangMoscow
August 29, 2019
Programming
0
100
Виталий Левченко – Go 2: ошибки, их логирование, и как нам с этим жить
GolangMoscow
August 29, 2019
Tweet
Share
More Decks by GolangMoscow
See All by GolangMoscow
Антон Кильчик – Дженерики, которые мы заслужили
golangmoscow
0
540
Yegor Myskin – HOW WE MAKE GRPC
golangmoscow
0
120
Other Decks in Programming
See All in Programming
テストカバレッジ100%を10年続けて得られた学びと品質
mottyzzz
2
570
CJK and Unicode From a PHP Committer
youkidearitai
PRO
0
110
ユーザーも開発者も悩ませない TV アプリ開発 ~Compose の内部実装から学ぶフォーカス制御~
taked137
0
140
個人軟體時代
ethanhuang13
0
320
AIでLINEスタンプを作ってみた
eycjur
1
230
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
lovee
0
110
Design Foundational Data Engineering Observability
sucitw
3
190
そのAPI、誰のため? Androidライブラリ設計における利用者目線の実践テクニック
mkeeda
2
260
Updates on MLS on Ruby (and maybe more)
sylph01
1
180
🔨 小さなビルドシステムを作る
momeemt
3
670
Tool Catalog Agent for Bedrock AgentCore Gateway
licux
6
2.3k
2025 年のコーディングエージェントの現在地とエンジニアの仕事の変化について
azukiazusa1
22
12k
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
96
6.2k
Mobile First: as difficult as doing things right
swwweet
224
9.9k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
126
53k
Into the Great Unknown - MozCon
thekraken
40
2k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
111
20k
Statistics for Hackers
jakevdp
799
220k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.9k
Faster Mobile Websites
deanohume
309
31k
Code Review Best Practice
trishagee
70
19k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
The Pragmatic Product Professional
lauravandoore
36
6.9k
Automating Front-end Workflow
addyosmani
1370
200k
Transcript
Go 2: ошибки, их логирование, и как нам с этим
жить Виталий Левченко
Обо мне • 11 лет в индустрии • Go разработчик
с 2012 • Тимлид • CTO • Product owner @ Uteka • Лидер Go, SRE, Teamlead сообществ Петербурга
GO SPB t.me/goleningrad
None
None
None
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
Постановка задачи
Постановка задачи • Avoid return
Постановка задачи • Avoid return • Log error
Постановка задачи • Avoid return • Log error • Debug
error
Постановка задачи • Avoid return • Log error • Debug
error • Log trace
Постановка задачи • Avoid return • Log error • Debug
error • Log trace • Handle error
Постановка задачи • Avoid return • Log error • Debug
error • Log trace • Handle error • Create error
Try / handle
Try / handle func() { defer fmt.HandleErrorf(&err, “open file %s",
path) file := try(os.Open(path)) defer file.Close() }
Try / handle func() { defer fmt.HandleErrorf(&err, “open file %s",
path) file := try(os.Open(path)) defer file.Close() } Abandoned
Постановка задачи • Avoid return (ждём нового try) • Log
error • Debug error • Log trace • Handle error • Create error
Log error type Formatter interface { Format(p Printer) (next error)
} type Printer interface { Print(args ...interface{}) Printf(format string, args ...interface{}) Detail() bool }
Log error type Formatter interface { Format(p Printer) (next error)
} type Printer interface { Print(args ...interface{}) Printf(format string, args ...interface{}) Detail() bool } Abandoned
Log error • https://godoc.org/golang.org/x/xerrors • https://tip.golang.org/pkg/fmt/#Errorf • https://github.com/joomcode/errorx
Log error interface { Unwrap() error }
Log error err := fmt.Errorf("file open err: %w", err)
Log error • Add fields: []zap.Field or whatever
Log error
Постановка задачи • Avoid return (ждём нового try) • Log
error (use xerror and wait for go1.13) • Debug error • Log trace • Handle error • Create error
Постановка задачи • Avoid return (ждём нового try) • Log
error (use xerror and wait for go1.13) • Debug error (use explicitly) • Log trace • Handle error • Create error
Log errors
Log trace • https://github.com/pkg/errors • https://github.com/go-errors/errors • Etc…
Log trace • https://github.com/pkg/errors • https://github.com/go-errors/errors • Etc… • Or
custom
Log trace • https://github.com/pkg/errors • https://github.com/go-errors/errors • Etc… • Or
custom • Send to sentry or whatever
Log trace
Постановка задачи • Avoid return (ждём нового try) • Log
error (use xerror and wait for go1.13) • Debug error (use explicitly) • Log trace (custom wrappers) • Handle error • Create error
Handle error • https://godoc.org/golang.org/x/xerrors • https://tip.golang.org/pkg/fmt/#Errorf
Handle error if errors.Is(err, os.ErrExist) { ... }
Handle error if errors.Is(err, os.ErrExist) { ... } var perr
*os.PathError if errors.As(err, &perr) { fmt.Println(perr.Path) }
Handle error
Постановка задачи • 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
Постановка задачи • Avoid return • Log error (lib +
custom wrapper) • Debug error (custom) • Log trace (libs and custom) • Handle error (custom) • Create error (custom)
Постановка задачи • Avoid return (or custom panics) • Log
error (lib + custom wrapper) • Debug error (custom) • Log trace (libs and custom) • Handle error (custom) • Create error (custom)
Обработка ошибок в Go
Custom error type Custom struct { message string error error
stack xerrors.Frame type string fields []zap.Field }
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 }
Custom error type Custom struct { message string error error
stack xerrors.Frame type string fields []zap.Field } Or use: • https://github.com/joomcode/errorx
Official • https://github.com/golang/go/wiki/ Go2ErrorValuesFeedback • https://github.com/golang/go/wiki/ Go2ErrorHandlingFeedback • https://blog.golang.org/experiment
Official • https://github.com/golang/go/wiki/ Go2ErrorValuesFeedback • https://github.com/golang/go/wiki/ Go2ErrorHandlingFeedback • https://blog.golang.org/experiment
Спасибо!