|
| 1 | +Error handling with Go |
| 2 | + |
| 3 | +8 May 2018 |
| 4 | + |
| 5 | + |
| 6 | +Frank Kleine |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +@bovigo |
| 11 | + |
| 12 | +* Errors, what are they? |
| 13 | + |
| 14 | +Errors can happen in each stage of the creation and execution of a program. |
| 15 | + |
| 16 | + Error handling refers to the anticipation, detection, and resolution of |
| 17 | + programming, application, and communications errors. |
| 18 | + |
| 19 | +From [[https://searchsoftwarequality.techtarget.com/definition/error-handling][What is error handling?]], Margaret Rouse |
| 20 | + |
| 21 | +: Wikipedia offers "Exception handling only" - I don't like that. |
| 22 | + |
| 23 | +* Errors in Go |
| 24 | + |
| 25 | +From the os package, os.Open: |
| 26 | +.code open.go |
| 27 | + |
| 28 | +And in usage it looks something like this: |
| 29 | +.code open_usage.go |
| 30 | + |
| 31 | +Standard library is full of interesting examples. It's always worth to dive into it. |
| 32 | + |
| 33 | +: Built-in error type. Golang.org blog article, a recommended read. |
| 34 | + |
| 35 | +* Your errors |
| 36 | + |
| 37 | +Always add context to an error before returning it. |
| 38 | +.code error_nocontext.go /START OMIT/,/END OMIT/ |
| 39 | +vs. |
| 40 | +.code error_context.go /START OMIT/,/END OMIT/ |
| 41 | + |
| 42 | +* Errors in non end user functions |
| 43 | + |
| 44 | +- E.g. functions that don't provide direct output to a user of a program. |
| 45 | +- Your backend functions, e.g. repositories, dealing with other services, etc. |
| 46 | +- Functions that most likely don't know how to deal with an error and must return immediately. |
| 47 | + |
| 48 | +* Example: repository function |
| 49 | + |
| 50 | +.code delete_repo.go /START OMIT/,/END OMIT/ |
| 51 | + |
| 52 | +* Errors in end user functions |
| 53 | + |
| 54 | +- Functions that must provide direct output to a user |
| 55 | +- E.g. HTTP handlers, main func of a CLI program |
| 56 | +- Function must continue after the error happened to render some helpful output for the user (and hopefully log the error) |
| 57 | + |
| 58 | +* Example: HTTP handler |
| 59 | + |
| 60 | +- Default HTTP handler doesn't offer to return an error |
| 61 | +.code httphandler.go |
| 62 | +- Leads to repetitive code |
| 63 | +- Prevent this by specifying your own HTTP handler |
| 64 | +.code userhttphandler.go /START OMIT/,/END OMIT/ |
| 65 | +- And a handler which handles the error |
| 66 | +.code errorhttphandler.go /START OMIT/,/END OMIT/ |
| 67 | + |
| 68 | +* User defined handler |
| 69 | + |
| 70 | +.code userhandler.go /START OMIT/,/END OMIT/ |
| 71 | + |
| 72 | +* What about pkg/errors? |
| 73 | + |
| 74 | +- A package by Dave Cheney, provides utility functions for working with errors. |
| 75 | +- Adds stack traces to errors |
| 76 | +- Uses chaining of errors, each error can have a cause |
| 77 | + |
| 78 | +It's a great and interesting package. However, I never found the need in my code to add the weight of more code and dependencies for a perceived little gain. |
| 79 | + |
| 80 | +(Disclaimer: Your mileage will vary.) |
| 81 | + |
| 82 | +(Provoking opinion: if you need stack traces your code base in a single artefact is too large. Except when you ship a binary to a customer. Than you want stack traces.) |
| 83 | + |
| 84 | + |
| 85 | +* Links |
| 86 | + |
| 87 | +- [[https://blog.golang.org/error-handling-and-go][Golang.org article about Go error handling]] |
| 88 | +- [[https://github.com/pkg/errors][pkg/errors]] |
0 commit comments