blob: b9f4a1c807e6afdc60af68d5611ccda88ad1e1f1 [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001# Errors
2
Dave Day0d6986a2014-12-10 15:02:18 +11003Errors are indicated by returning an `error` as an additional return value from a function. A `nil` value means that there was no error.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11004
Dave Day0d6986a2014-12-10 15:02:18 +11005` error `s can be turned into strings by calling `Error`, their only method. You can create an error from a string by calling `errors.New`:
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11006
Ondrej Fabryf2966632018-06-07 14:55:48 +02007```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11008if failure {
Dave Day0d6986a2014-12-10 15:02:18 +11009 return errors.New("inverse tachyon pulse failed")
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110010}
11```
12
Nika Jonesa0668e22018-10-09 01:12:43 -070013or by using `fmt.Errorf`:
14
15```go
16if failure {
17 return fmt.Errorf("inverse tachyon pulse failed")
18}
19```
20
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110021Error strings should not start with a capital letter because they'll often be prefixed before printing:
22
Ondrej Fabryf2966632018-06-07 14:55:48 +020023```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110024err := TryInverseTachyonPulse()
25if err != nil {
Dave Day0d6986a2014-12-10 15:02:18 +110026 fmt.Printf("failed to solve problem: %s\n", err)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110027}
28```
29
30If you expect calling code to be able to handle an error, you can distinguish classes of errors either by returning special values, or new types. You only need to distinguish differences that the calling code could be expected to handle in this way as the string allows one to communicate the details of the error.
31
Dave Day0d6986a2014-12-10 15:02:18 +110032`io.EOF` is a special value that signals the end of a stream. You can compare error values directly against io.EOF.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110033
34If you want to carry extra data with the error, you can use a new type:
35
Ondrej Fabryf2966632018-06-07 14:55:48 +020036```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110037type ParseError struct {
Dave Day0d6986a2014-12-10 15:02:18 +110038 Line, Col int
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110039}
40
41func (p ParseError) Error() string {
Dave Day0d6986a2014-12-10 15:02:18 +110042 return fmt.Sprintf("parse error on line %d, column %d", p.Line, p.Col)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110043}
44```
45
Nika Jonesa0668e22018-10-09 01:12:43 -070046If you want to create a constant string error, you can use a named type string:
47
48```go
49type errorConst string
50
51const ErrTooManyErrors errorConst = "too many errors found."
52```
53
Dave Day0d6986a2014-12-10 15:02:18 +110054Calling code would test for a special type of `error` by using a type switch:
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110055
Ondrej Fabryf2966632018-06-07 14:55:48 +020056```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110057switch err := err.(type) {
58case ParseError:
Dave Day0d6986a2014-12-10 15:02:18 +110059 PrintParseError(err)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110060}
61```
62
63## Naming
64
Dave Day0d6986a2014-12-10 15:02:18 +110065Error types end in `"Error"` and error variables start with `"Err"`:
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110066
Ondrej Fabryf2966632018-06-07 14:55:48 +020067```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110068package somepkg
69
70// ParseError is type of error returned when there's a parsing problem.
71type ParseError struct {
72 Line, Col int
73}
74
75var ErrBadAction = errors.New("somepkg: a bad action was performed")
76
77// -----
78
79package foo
80
81func foo() {
82 res, err := somepkgAction()
83 if err != nil {
84 if err == somepkg.ErrBadAction {
85 }
86 if pe, ok := err.(*somepkg.ParseError); ok {
87 line, col := pe.Line, pe.Col
88 // ....
89 }
90 }
91}
92```
93
94## References
95
96 * Errors (specification): http://golang.org/ref/spec#Errors
Dave Day0d6986a2014-12-10 15:02:18 +110097 * Package `errors`: http://golang.org/pkg/errors/
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110098 * Type switches: http://golang.org/doc/go_spec.html#TypeSwitchStmt