Share
Embed
Transcript
36 slides
-
func GetProduct(id int) (Product, error) { product, err := db.Select(...)
return product, err } -
func GetProduct(id int) (Product, error) { product, err := db.Select(...)
return product, err } func ContrivedMethod(id int) error { product, err := GetProduct(id) if err != nil { return err } ... } -
func GetProduct(id int) (Product, error) { product, err := db.Select(...)
return product, err } func ContrivedMethod(id int) error { product, err := GetProduct(id) if err != nil { return err } ... } func HandleRequest(id int, w http.ResponseWriter, r *http.Request) { err := ContrivedMethod(id) if err != nil { http.Error(w, err.Error(),...) } ... } -
strconv.ParseInt: parsing "adsf": invalid syntax pq: duplicate key value violates
unique constraint Product not found -
func GetProduct(id int) (Product, error) { product, err := db.Select(...)
return product, err } func ContrivedMethod(id int) error { product, err := GetProduct(id) if err != nil { return err } ... } func HandleRequest(id int, w http.ResponseWriter, r *http.Request) { err := ContrivedMethod(id) if err != nil { http.Error(w, err.Error(),...) } ... } Lost Context -
func GetProduct(id int) (Product, *SpringError) { product, err := db.Select(...)
return product, wrap(err) } -
No product found main.GetProduct(0x1, 0x1704, 0x2b7a0, ...) /tmp/sandbox244941649/main.go:25 +0xc0 main.ContrivedMethod(0x1,
0x931a0, 0x0, 0x0) /tmp/sandbox244941649/main.go:30 +0x60 main.main() /tmp/sandbox244941649/main.go:34 +0x40 -
func GetProduct(id int) (Product, error) { product, err := db.Select(...)
return product, err } func ContrivedMethod(id int) error { product, err := GetProduct(id) if err != nil { return err } ... } func HandleRequest(id int, w http.ResponseWriter, r *http.Request) { err := ContrivedMethod(id) if err != nil { http.Error(w, err.Error(),...) } ... } Wrapped? Wrapped? -
func GetProduct(id int) (Product, *SpringError) { product, err := db.Select(...)
return product, wrap(err) } func ContrivedMethod(id int) *SpringError { product, err := GetProduct(id) if err != nil { return err } ... } func HandleRequest(id int, w http.ResponseWriter, r *http.Request) { err := ContrivedMethod(id) if err != nil { http.Error(w, err.String(),...) } ... } errors get wrapped Already Wrapped! -
func GetProduct(id int) (Product, *SpringError) { product, err := db.Select(...)
return product, wrap(err) } func ContrivedMethod(id int) *SpringError { product, err := GetProduct(id) if err != nil { return err } ... } func HandleRequest(id int, w http.ResponseWriter, r *http.Request) { err := ContrivedMethod(id) if err != nil { http.Error(w, err.String(),...) } ... } -
func GetProduct(id int) (Product, *SpringError) { product, err := db.Select(...)
return product, wrap(err) } func ContrivedMethod(id int) *SpringError { product, err := GetProduct(id) if err != nil { return err } ... } func HandleRequest(id int, w http.ResponseWriter, r *http.Request) { err := ContrivedMethod(id) if err != nil { http.Error(w, err.String(),...) } ... } -
func GetProduct(id int) (Product, *SpringError) { product, err := db.Select(...)
return product, wrap(err) } func ContrivedMethod(id int) *SpringError { product, err := GetProduct(id) if err != nil { return err } ... } func HandleRequest(id int, w http.ResponseWriter, r *http.Request) { err := ContrivedMethod(id) if err != nil { http.Error(w, err.String(),...) } ... } -
func GetProduct(id int) (Product, *SpringError) { product, err := db.Select(...)
return product, wrap(err) } func ContrivedMethod(id int) *SpringError { product, err := GetProduct(id) if err != nil { return err } ... } func HandleRequest(id int, w http.ResponseWriter, r *http.Request) { err := ContrivedMethod(id) if err != nil { http.Error(w, err.String(),...) } ... } -
func GetProduct(id int) (product Product, err *SpringError) { defer annotateError(&err,
“fetching product with id %d”, id) product, err := db.Select(...) return product, wrap(err) } -
func HandleRequest(id int, w http.ResponseWriter, r *http.Request) { err :=
ContrivedMethod(id) if err != nil { err.Annotate(“request for %s”, request.URL.String()) http.Error(w, err.String(),...) } ... } -
Product not found in request for /product/1124 fetching product with
id 1124 main.GetProduct(0x1, 0x1704, 0x2b7a0, ...) /tmp/sandbox244941649/main.go:25 +0xc0 main.ContrivedMethod(0x1, 0x931a0, 0x0, 0x0) /tmp/sandbox244941649/main.go:30 +0x60 main.main() /tmp/sandbox244941649/main.go:34 +0x40 -
Recap • We used to see hard to understand errors
in production. • Now we don’t. • Add context to your errors. • Use the type system to enforce that every error is addressed. • Make it easy to add extra context where it is helpful