Skip to content

Latest commit

 

History

History
56 lines (42 loc) · 4.82 KB

File metadata and controls

56 lines (42 loc) · 4.82 KB

Error Handling Design

Error handling is critical for making your programs reliable, trustworthy and respectful to those who depend on them. A proper error value is both specific and informative. It must allow the caller to make an informed decision about the error that has occurred. There are several ways in Go to create error values. This depends on the amount of context that needs to be provided.

Notes

  • Use the default error value for static and simple formatted messages.
  • Create and return error variables to help the caller identify specific errors.
  • Create custom error types when the context of the error is more complex.
  • Error Values in Go aren't special, they are just values like any other, and so you have the entire language at your disposal.

Quotes

Systems cannot be developed assuming that human beings will be able to write millions of lines of code without making mistakes, and debugging alone is not an efficient way to develop reliable systems. - Al Aho (inventor of AWK)

Links

Error handling and Go
Error Handling In Go, Part I - William Kennedy
Error Handling In Go, Part II - William Kennedy
Design Philosophy On Logging - William Kennedy
Bugs are a failure of prediction - Matt Sherman
Inspecting errors - Dave Cheney
Don’t just check errors, handle them gracefully - Dave Cheney
Stack traces and the errors package - Dave Cheney
Errors are handled in return values - Russ Cox
Error handling in Upspin - Rob Pike
Go 1.13: xerrors - David Crawshaw
Why Go's Error Handling is Awesome - Raul Jordan

Code Review

Default Error Values (Go Playground)
Error Variables (Go Playground)
Type As Context (Go Playground)
Behavior As Context (Go Playground)
Find The Bug (Go Playground) | The Reason (Go Playground)
Wrapping Errors With pkg/errors (Go Playground)
Wrapping Errors With stdlib (Go Playground)

Exercises

Exercise 1

Create two error variables, one called ErrInvalidValue and the other called ErrAmountTooLarge. Provide the static message for each variable. Then write a function called checkAmount that accepts a float64 type value and returns an error value. Check the value for zero and if it is, return the ErrInvalidValue. Check the value for greater than $1,000 and if it is, return the ErrAmountTooLarge. Write a main function to call the checkAmount function and check the return error value. Display a proper message to the screen.

Template (Go Playground) | Answer (Go Playground)

Exercise 2

Create a custom error type called appError that contains three fields, err error, message string and code int. Implement the error interface providing your own message using these three fields. Implement a second method named temporary that returns false when the value of the code field is 9. Write a function called checkFlag that accepts a bool value. If the value is false, return a pointer of your custom error type initialized as you like. If the value is true, return a default error. Write a main function to call the checkFlag function and check the error using the temporary interface.

Template (Go Playground) | Answer (Go Playground)


All material is licensed under the Apache License Version 2.0, January 2004.