Skip to content

Commit 0b681eb

Browse files
author
Sam Stenvall
committed
Document error handling properly (closes #27)
1 parent 97c99f3 commit 0b681eb

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,37 @@ if err != nil {
5858
}
5959
```
6060

61+
### Error handling
62+
63+
All `Service` methods return a result and an error object. You can differentiate between generic connection errors
64+
(like the API not being reachable) and service errors, which are errors returned in the response body by the API. This
65+
is useful if you want to gracefully recover from certain types of errors.
66+
67+
```go
68+
username := "completely"
69+
password := "invalid"
70+
71+
svc := service.New(client.New(username, password))
72+
73+
_, err := svc.GetAccount()
74+
75+
// Handle errors in general
76+
if (err != nil) {
77+
// Handle service errors specifically
78+
if serviceError, ok := err.(*upcloud.Error); ok {
79+
fmt.Println(serviceError.ErrorCode)
80+
fmt.Println(serviceError.ErrorMessage)
81+
}
82+
}
83+
````
84+
85+
This snippet would print the following:
86+
87+
```
88+
AUTHENTICATION_FAILED
89+
Authentication failed using the given username and password.
90+
```
91+
6192
The rest of these examples assume you already have a service object configured and named `svc`.
6293
6394
### Retrieving a list of servers

0 commit comments

Comments
 (0)