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

Error Handling at Spring

Wisdom
February 18, 2015
50

Error Handling at Spring

nycgolang meetup

Wisdom

February 18, 2015

Transcript

36 slides
  1. 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 } ... }
  2. 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(),...) } ... }
  3. 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
  4. 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
  5. 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?
  6. 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!
  7. 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(),...) } ... }
  8. 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(),...) } ... }
  9. 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(),...) } ... }
  10. 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(),...) } ... }
  11. 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) }
  12. 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(),...) } ... }
  13. 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
  14. 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