# Error Handling In Go, Part II

- Published: November 10, 2014
- Author: William Kennedy
- URL: https://www.ardanlabs.com/blog/2014/11/error-handling-in-go-part-ii.html

<span style="font-size: large;">Introduction</span><br />
In <a href="https://www.ardanlabs.com/blog/2014/10/error-handling-in-go-part-i.html" target="_blank">part I</a> of this post, we learned about the <span style="font-family: Courier New, Courier, monospace;">error</span> interface and how the standard library provides support for creating <span style="font-family: Courier New, Courier, monospace;">error</span> interface values via the <span style="font-family: Courier New, Courier, monospace;">errors</span> package. We also learned how to work with <span style="font-family: Courier New, Courier, monospace;">error</span> interface values and use them to identify when an error has occured. Finally, we saw how some packages in the standard library export <span style="font-family: Courier New, Courier, monospace;">error</span> interface variables to help us identify specific errors.

<br />
<br />
Knowing when to create and use custom error types in Go can sometimes be confusing. In most cases, the traditional <span style="font-family: Courier New, Courier, monospace;">error</span> interface value provided by the <span style="font-family: Courier New, Courier, monospace;">errors</span> package is enough for reporting and handling errors. However, sometimes the caller needs extra context in order to make a more informed error handling decision. For me, that is when custom error types make sense.<br />
<br />
In this post, we are going to learn about custom error types and look at two use cases from the standard library where they are used. Each use case provides an interesting perspective for when and how to implement a custom error type. Then we will learn how to identify the concrete type of the value or pointer stored within an <span style="font-family: Courier New, Courier, monospace;">error</span> interface value, and see how that can help us make more informed error handling decisions.<br />
<br />
<span style="font-size: large;">The net Package</span><br />
The <span style="font-family: Courier New, Courier, monospace;">net</span> package has declared a custom error type called <span style="font-family: Courier New, Courier, monospace;">OpError</span>. Pointers of this struct are used by many of the functions and methods inside the package as the concrete type stored within the returned <span style="font-family: Courier New, Courier, monospace;">error</span> interface value:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.1</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
http://golang.org/src/pkg/net/dial.go<br />
<br />
01 func Listen(net, laddr string) (Listener, error) {<br />
02 &nbsp; &nbsp; la, err := resolveAddr(&quot;listen&quot;, net, laddr, noDeadline)<br />
03 &nbsp; &nbsp; if err != nil {<br />
04 &nbsp; &nbsp; &nbsp; &nbsp; return nil, &amp;OpError{Op: &quot;listen&quot;, Net: net, Addr: nil, Err: err}<br />
05 &nbsp; &nbsp; }<br />
06 &nbsp; &nbsp; var l Listener<br />
07 &nbsp; &nbsp; switch la := la.toAddr().(type) {<br />
08 &nbsp; &nbsp; case &#42;TCPAddr:<br />
09 &nbsp; &nbsp; &nbsp; &nbsp; l, err = ListenTCP(net, la)<br />
10 &nbsp; &nbsp; case &#42;UnixAddr:<br />
11 &nbsp; &nbsp; &nbsp; &nbsp; l, err = ListenUnix(net, la)<br />
12 &nbsp; &nbsp; default:<br />
13 &nbsp; &nbsp; &nbsp; &nbsp; return nil, &amp;OpError{Op: &quot;listen&quot;, Net: net, Addr: la, Err: &amp;AddrError{Err: &quot;unexpected address type&quot;, Addr: laddr}}<br />
14 &nbsp; &nbsp; }<br />
15 &nbsp; &nbsp; if err != nil {<br />
16 &nbsp; &nbsp; &nbsp; &nbsp; return nil, err // l is non-nil interface containing nil pointer<br />
17 &nbsp; &nbsp; }<br />
18 &nbsp; &nbsp; return l, nil<br />
19 }
</div>
<br />
Listing 1.1 shows the implementation of the <span style="font-family: Courier New, Courier, monospace;">Listen</span> function from the <span style="font-family: Courier New, Courier, monospace;">net</span> package. We can see that on lines 04 and 13, pointers of the <span style="font-family: Courier New, Courier, monospace;">OpError</span> struct are created and passed in the return statement for the <span style="font-family: Courier New, Courier, monospace;">error</span> interface value. Since pointers of the <span style="font-family: Courier New, Courier, monospace;">OpError</span> struct implement the <span style="font-family: Courier New, Courier, monospace;">error</span> interface, the pointer can be stored in an <span style="font-family: Courier New, Courier, monospace;">error</span> interface value and returned. What you don’t see is that on lines 09 and 11, the <span style="font-family: Courier New, Courier, monospace;">ListenTCP</span> and <span style="font-family: Courier New, Courier, monospace;">ListenUnix</span> functions can also return pointers of the <span style="font-family: Courier New, Courier, monospace;">OpError</span> struct, which are stored in the returned <span style="font-family: Courier New, Courier, monospace;">error</span> interface value.<br />
<br />
Next, let’s look at the declaration of the <span style="font-family: Courier New, Courier, monospace;">OpError</span> struct:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.2</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
http://golang.org/pkg/net/#OpError<br />
<br />
01 // OpError is the error type usually returned by functions in the net<br />
02 // package. It describes the operation, network type, and address of<br />
03 // an error.<br />
04 type OpError struct {<br />
05 &nbsp; &nbsp; // Op is the operation which caused the error, such as<br />
06 &nbsp; &nbsp; // &quot;read&quot; or &quot;write&quot;.<br />
07 &nbsp; &nbsp; Op string<br />
08<br />
09 &nbsp; &nbsp; // Net is the network type on which this error occurred,<br />
10 &nbsp; &nbsp; // such as &quot;tcp&quot; or &quot;udp6&quot;.<br />
11 &nbsp; &nbsp; Net string<br />
12<br />
13 &nbsp; &nbsp; // Addr is the network address on which this error occurred.<br />
14 &nbsp; &nbsp; Addr Addr<br />
15<br />
16 &nbsp; &nbsp; // Err is the error that occurred during the operation.<br />
17 &nbsp; &nbsp; Err error<br />
18 }
</div>
<br />
Listing 1.2 shows the declaration of the <span style="font-family: Courier New, Courier, monospace;">OpError</span> struct. The first three fields on lines 07, 11 and 14 provide context about the network operation being performed when an error occurs. The fourth field on line 17 is declared as an <span style="font-family: Courier New, Courier, monospace;">error</span> interface type. This field will contain the actual error that occurred and the value of the concrete type in many cases will be a pointer of type <span style="font-family: Courier New, Courier, monospace;">errorString</span>.<br />
<br />
Another thing to note is the naming convention for custom error types. It is idiomatic in Go to postfix the name of a custom error type with the word <span style="font-family: Courier New, Courier, monospace;">Error</span>. We will see this naming convention used again in other packages.<br />
<br />
Next, let’s look at the implementation of the <span style="font-family: Courier New, Courier, monospace;">error</span> interface for the <span style="font-family: Courier New, Courier, monospace;">OpError</span> struct:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.3</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
http://golang.org/src/pkg/net/net.go<br />
<br />
01 func (e &#42;OpError) Error() string {<br />
02 &nbsp; &nbsp; if e == nil {<br />
03 &nbsp; &nbsp; &nbsp; &nbsp; return &quot;&lt;nil&gt;&quot;<br />
04 &nbsp; &nbsp; }<br />
05 &nbsp; &nbsp; s := e.Op<br />
06 &nbsp; &nbsp; if e.Net != &quot;&quot; {<br />
07 &nbsp; &nbsp; &nbsp; &nbsp; s += &quot; &quot; + e.Net<br />
08 &nbsp; &nbsp; }<br />
09 &nbsp; &nbsp; if e.Addr != nil {<br />
10 &nbsp; &nbsp; &nbsp; &nbsp; s += &quot; &quot; + e.Addr.String()<br />
11 &nbsp; &nbsp; }<br />
12 &nbsp; &nbsp; s += &quot;: &quot; + e.Err.Error()<br />
13 &nbsp; &nbsp; return s<br />
14 }
</div>
<br />
The implementation of the <span style="font-family: Courier New, Courier, monospace;">error</span> interface in listing 1.3 shows how the context associated with the error is used to generate a contextual error message. Binding context with the error provides extended error information and can help the caller make more informed decisions about how to handle the error.<br />
<br />
<span style="font-size: large;">The json Package</span><br />
The <span style="font-family: Courier New, Courier, monospace;">json</span> package performs the decoding of data from JSON to native Go types and vice versa. All possible errors that can be returned from the package are generated internally. Maintaining the context associated with an error is critical for this package or it wouldn’t be able to properly report what has happened. There are several custom error types in the json package and these types can be returned by the same functions and methods.<br />
<br />
Let’s look at one of these custom error types:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.4</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
http://golang.org/src/pkg/encoding/json/decode.go<br />
<br />
01 // An UnmarshalTypeError describes a JSON value that was<br />
02 // not appropriate for a value of a specific Go type.<br />
03 type UnmarshalTypeError struct {<br />
04 &nbsp; &nbsp; Value string       // description of JSON value<br />
05 &nbsp; &nbsp; Type  reflect.Type // type of Go value it could not be assigned to<br />
06 }<br />
07<br />
08 func (e &#42;UnmarshalTypeError) Error() string {<br />
09 &nbsp; &nbsp; return &quot;json: cannot unmarshal &quot; + e.Value + &quot; into Go value of type &quot; + e.Type.String()<br />
10 }
</div>
<br />
Listing 1.4 shows the declaration of the <span style="font-family: Courier New, Courier, monospace;">UnmarshalTypeError</span> struct and the implementation of the <span style="font-family: Courier New, Courier, monospace;">error</span> interface. This struct is used to report errors that occur when a value can’t be decoded into a specific Go type. The struct contains two fields, one called <span style="font-family: Courier New, Courier, monospace;">Value</span> on line 04 that contains the value attempted to be decoded and one called <span style="font-family: Courier New, Courier, monospace;">Type</span> on line 05 which contains the Go type that the value could not be converted to. The implementation of the <span style="font-family: Courier New, Courier, monospace;">error</span> interface on line 08 takes the context of the error and produces a proper error message.<br />
<br />
In this case, the type itself provides the context for the error. The name of this type is called <span style="font-family: Courier New, Courier, monospace;">UnmarshalTypeError</span> and that is the context in which it is used. When there are errors associated with unmarshaling types, pointers of this struct are stored within the returned <span style="font-family: Courier New, Courier, monospace;">error</span> interface value.<br />
<br />
When there are invalid arguments passed into an unmarshal call, a pointer of concrete type <span style="font-family: Courier New, Courier, monospace;">InvalidUnmarshalError</span> is stored within the <span style="font-family: Courier New, Courier, monospace;">error</span> interface value that is returned:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.5</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
http://golang.org/src/pkg/encoding/json/decode.go<br />
<br />
01 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.<br />
02 // (The argument to Unmarshal must be a non-nil pointer.)<br />
03 type InvalidUnmarshalError struct {<br />
04 &nbsp; &nbsp; Type reflect.Type<br />
05 }<br />
06<br />
07 func (e &#42;InvalidUnmarshalError) Error() string {<br />
08 &nbsp; &nbsp; if e.Type == nil {<br />
09 &nbsp; &nbsp; &nbsp; &nbsp; return &quot;json: Unmarshal(nil)&quot;<br />
10 &nbsp; &nbsp; }<br />
11<br />
12 &nbsp; &nbsp; if e.Type.Kind() != reflect.Ptr {<br />
13 &nbsp; &nbsp; &nbsp; &nbsp; return &quot;json: Unmarshal(non-pointer &quot; + e.Type.String() + &quot;)&quot;<br />
14 &nbsp; &nbsp; }<br />
15 &nbsp; &nbsp; return &quot;json: Unmarshal(nil &quot; + e.Type.String() + &quot;)&quot;<br />
16 }
</div>
<br />
Listing 1.5 shows the declaration of the <span style="font-family: Courier New, Courier, monospace;">InvalidUnmarshalError</span> struct and the implementation of the <span style="font-family: Courier New, Courier, monospace;">error</span> interface. Once again, the type itself provides the context for the error. The state being maintained helps to produce a proper error message and provides context to help the caller make more informed error handling decision.<br />
<br />
<span style="font-size: large;">Concrete Type Identification</span><br />
In the case of the <span style="font-family: Courier New, Courier, monospace;">Unmarshal</span> function from the <span style="font-family: Courier New, Courier, monospace;">json</span> package, there is the potential for a pointer of type <span style="font-family: Courier New, Courier, monospace;">UnmarshalTypeError</span>, <span style="font-family: Courier New, Courier, monospace;">InvalidUnmarshalError</span> or <span style="font-family: Courier New, Courier, monospace;">errorString</span> to be stored within the <span style="font-family: Courier New, Courier, monospace;">error</span> interface value that is returned:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.6</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
http://golang.org/src/pkg/encoding/json/decode.go<br />
<br />
01 func Unmarshal(data []byte, v interface{}) error {<br />
02 &nbsp; &nbsp; // Check for well-formedness.<br />
03 &nbsp; &nbsp; // Avoids filling out half a data structure<br />
04 &nbsp; &nbsp; // before discovering a JSON syntax error.<br />
05 &nbsp; &nbsp; var d decodeState<br />
06 &nbsp; &nbsp; err := checkValid(data, &amp;d.scan)<br />
07 &nbsp; &nbsp; if err != nil {<br />
08 &nbsp; &nbsp; &nbsp; &nbsp; return err<br />
09 &nbsp; &nbsp; }<br />
10<br />
11 &nbsp; &nbsp; d.init(data)<br />
12 &nbsp; &nbsp; return d.unmarshal(v)<br />
13 }<br />
14<br />
15 func (d &#42;decodeState) unmarshal(v interface{}) (err error) {<br />
16 &nbsp; &nbsp; defer func() {<br />
17 &nbsp; &nbsp; &nbsp; &nbsp; if r := recover(); r != nil {<br />
18 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if _, ok := r.(runtime.Error); ok {<br />
19 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(r)<br />
20 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
21 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; err = r.(error)<br />
22 &nbsp; &nbsp; &nbsp; &nbsp; }<br />
23 &nbsp; &nbsp; }()<br />
24<br />
25 &nbsp; &nbsp; rv := reflect.ValueOf(v)<br />
26 &nbsp; &nbsp; if rv.Kind() != reflect.Ptr || rv.IsNil() {<br />
27 &nbsp; &nbsp; &nbsp; &nbsp; return &amp;InvalidUnmarshalError{reflect.TypeOf(v)}<br />
28 &nbsp; &nbsp; }<br />
29<br />
30 &nbsp; &nbsp; d.scan.reset()<br />
31 &nbsp; &nbsp; // We decode rv not rv.Elem because the Unmarshaler interface<br />
32 &nbsp; &nbsp; // test must be applied at the top level of the value.<br />
33 &nbsp; &nbsp; d.value(rv)<br />
34 &nbsp; &nbsp; return d.savedError<br />
35 }
</div>
<br />
Listing 1.6 shows how the returned <span style="font-family: Courier New, Courier, monospace;">error</span> interface value for the <span style="font-family: Courier New, Courier, monospace;">Unmarshal</span> call can potentially store pointers of different concrete types. On line 27, the <span style="font-family: Courier New, Courier, monospace;">unmarshal</span> method returns a pointer of type <span style="font-family: Courier New, Courier, monospace;">InvalidUnmarshalError</span> and then on line 34, the value of the <span style="font-family: Courier New, Courier, monospace;">savedError</span> field from the <span style="font-family: Courier New, Courier, monospace;">decodeState</span> variable is returned. This value can be pointers of several different concrete types.<br />
<br />
Knowing that the <span style="font-family: Courier New, Courier, monospace;">json</span> package is using the custom error type as the context for the error, how can we identify the type of the concrete value to make a more informed decision about handling the error?<br />
<br />
Let’s start with a program that causes the <span style="font-family: Courier New, Courier, monospace;">Unmarshal</span> function to return an <span style="font-family: Courier New, Courier, monospace;">error</span> interface value with a concrete pointer type of <span style="font-family: Courier New, Courier, monospace;">UnmarshalTypeError</span>:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.7</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
http://play.golang.org/p/FVFo8mJLBV<br />
<br />
01 package main<br />
02<br />
03 import (<br />
04 &nbsp; &nbsp; &quot;encoding/json&quot;<br />
05 &nbsp; &nbsp; &quot;fmt&quot;<br />
06 &nbsp; &nbsp; &quot;log&quot;<br />
07 )<br />
08<br />
09 type user struct {<br />
10 &nbsp; &nbsp; Name int<br />
11 }<br />
12<br />
13 func main() {<br />
14 &nbsp; &nbsp; var u user<br />
15 &nbsp; &nbsp; err := json.Unmarshal([]byte(`{&quot;name&quot;:&quot;bill&quot;}`), &amp;u)<br />
16 &nbsp; &nbsp; if err != nil {<br />
17 &nbsp; &nbsp; &nbsp; &nbsp; log.Println(err)<br />
18 &nbsp; &nbsp; &nbsp; &nbsp; return<br />
19 &nbsp; &nbsp; }<br />
20<br />
21 &nbsp; &nbsp; fmt.Println(&quot;Name:&quot;, u.Name)<br />
22 }<br />
<br />
Output:<br />
2009/11/10 23:00:00 json: cannot unmarshal string into Go value of type int
</div>
<br />
Listing 1.7 shows a sample program that attempts to unmarshal a JSON document into a Go type. The JSON document on line 15 contains a field named <span style="font-family: Courier New, Courier, monospace;">name</span> with a string value of <span style="font-family: Courier New, Courier, monospace;">bill</span>. Since the <span style="font-family: Courier New, Courier, monospace;">Name</span> field in the <span style="font-family: Courier New, Courier, monospace;">user</span> type declared on line 09 is declared as an integer, the <span style="font-family: Courier New, Courier, monospace;">Unmarshal</span> function returns an <span style="font-family: Courier New, Courier, monospace;">error</span> interface value stored with a concrete pointer type of <span style="font-family: Courier New, Courier, monospace;">UnmarshalTypeError</span>.<br />
<br />
Now let’s make some changes to the program in listing 1.7 so the same <span style="font-family: Courier New, Courier, monospace;">Unmarshal</span> call returns an <span style="font-family: Courier New, Courier, monospace;">error&nbsp;</span>interface value that is storing a different concrete pointer type:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.8</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
http://play.golang.org/p/n8dQFeHYVp<br />
<br />
01 package main<br />
02<br />
03 import (<br />
04 &nbsp; &nbsp; &quot;encoding/json&quot;<br />
05 &nbsp; &nbsp; &quot;fmt&quot;<br />
06 &nbsp; &nbsp; &quot;log&quot;<br />
07 )<br />
08<br />
09 type user struct {<br />
10 &nbsp; &nbsp; Name int<br />
11 }<br />
12<br />
13 func main() {<br />
14 &nbsp; &nbsp; var u user<br />
15 &nbsp; &nbsp; err := json.Unmarshal([]byte(`{&quot;name&quot;:&quot;bill&quot;}`), u)<br />
16 &nbsp; &nbsp; if err != nil {<br />
17 &nbsp; &nbsp; &nbsp; &nbsp; switch e := err.(type) {<br />
18 &nbsp; &nbsp; &nbsp; &nbsp; case &#42;json.UnmarshalTypeError:<br />
19 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Printf(&quot;UnmarshalTypeError: Value[%s] Type[%v]\n&quot;, e.Value, e.Type)<br />
20 &nbsp; &nbsp; &nbsp; &nbsp; case &#42;json.InvalidUnmarshalError:<br />
21 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Printf(&quot;InvalidUnmarshalError: Type[%v]\n&quot;, e.Type)<br />
22 &nbsp; &nbsp; &nbsp; &nbsp; default:<br />
23 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println(err)<br />
24 &nbsp; &nbsp; &nbsp; &nbsp; }<br />
25 &nbsp; &nbsp; &nbsp; &nbsp; return<br />
26 &nbsp; &nbsp; }<br />
27<br />
28 &nbsp; &nbsp; fmt.Println(&quot;Name:&quot;, u.Name)<br />
29 }<br />
<br />
Output:<br />
2009/11/10 23:00:00 json: Unmarshal(non-pointer main.user)<br />
2009/11/10 23:00:00 InvalidUnmarshalError: Type[main.user]
</div>
<br />
The sample program in listing 1.8 has a few changes from listing 1.7. On line 15, we now pass the value of the variable <span style="font-family: Courier New, Courier, monospace;">u</span> instead of its address to the <span style="font-family: Courier New, Courier, monospace;">Unmarshal</span> function. This change causes the <span style="font-family: Courier New, Courier, monospace;">Unmarshal</span> function to return an <span style="font-family: Courier New, Courier, monospace;">error</span> interface value that is storing a concrete pointer type of <span style="font-family: Courier New, Courier, monospace;">InvalidUnmarshalError</span>.<br />
<br />
Then we do something interesting on lines 17 through 24:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.9</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
17 &nbsp; &nbsp; &nbsp; &nbsp; switch e := err.(type) {<br />
18 &nbsp; &nbsp; &nbsp; &nbsp; case &#42;json.UnmarshalTypeError:<br />
19 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Printf(&quot;UnmarshalTypeError: Value[%s] Type[%v]\n&quot;, e.Value, e.Type)<br />
20 &nbsp; &nbsp; &nbsp; &nbsp; case &#42;json.InvalidUnmarshalError:<br />
21 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Printf(&quot;InvalidUnmarshalError: Type[%v]\n&quot;, e.Type)<br />
22 &nbsp; &nbsp; &nbsp; &nbsp; default:<br />
23 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println(err)<br />
24 &nbsp; &nbsp; &nbsp; &nbsp; }</div>
<br />
A&nbsp;<span style="font-family: Courier New, Courier, monospace;">switch</span>&nbsp;statement on line 17 is added to identify the concrete type of the pointer that was stored inside the returned&nbsp;<span style="font-family: Courier New, Courier, monospace;">error</span>&nbsp;interface value. Notice how the keyword&nbsp;<span style="font-family: Courier New, Courier, monospace;">type</span>&nbsp;is used within the interface value conversion syntax. We are also able to extract the value of the concrete type at the same time and use it in each case statement.<br />
<br />
The <span style="font-family: Courier New, Courier, monospace;">case</span> statements on lines 18 and 20 check for the specific concrete types and perform logic associated with handling those errors. This is the idiomatic way in Go to identify the concrete type of the value or pointer stored within an <span style="font-family: Courier New, Courier, monospace;">error</span> interface value from a set of concrete types.<br />
<br />
<span style="font-size: large;">Conclusion</span><br />
The <span style="font-family: Courier New, Courier, monospace;">error</span> interface values we return should contain information about a specific negative condition that has occurred within the scope of a function or method that impacts the caller. It must provide enough information so the caller can act in an informed way. Usually a simple message is enough, but sometimes the caller needs more.<br />
<br />
We saw one case from the <span style="font-family: Courier New, Courier, monospace;">net</span> package where a custom error type was declared to wrap the original error and the context associated with that error. In the <span style="font-family: Courier New, Courier, monospace;">json</span> package, we saw the use of custom error types that provide both the context of the error and associated state. In both cases, the need to maintain context associated with an error was a deciding factor.<br />
<br />
When the traditional <span style="font-family: Courier New, Courier, monospace;">error</span> interface value created by the <span style="font-family: Courier New, Courier, monospace;">errors</span> package provides enough context for the error, use it. It is used throughout the standard library and is usually all you need. When extra context is required to help the caller make a more informed decision, take a cue from the standard library and build your own custom error types.
