The following interface fetches articles for an id.
interface NetworkApi {
/*
* Fetches details of an article of parameter [id] from the network.
*/
fun article(id: String) : Single<Article>
}
The following interface fetches articles for an id.
interface NetworkApi {
/*
* Fetches details of an article of parameter [id] from the network.
*/
fun article(id: String) : Single<Article>
}
| func networkRequest() { | |
| body, err := makeRequest("url", "GET") | |
| if err != nil { | |
| switch err := err.(type) { | |
| case *NetworkError: | |
| if err.IsAccessDenied() { | |
| // handle access denied here. | |
| } else if err.IsAuthError() { | |
| // handle auth error here. |
| // Network error, when the status code from a network request is > 400 | |
| type NetworkError struct { | |
| statusCode int | |
| Body string | |
| } | |
| func (n *NetworkError) IsAuthError() bool { | |
| return n.statusCode == 401 | |
| } |
| n, err := io.ReadAtLeast(r, buf, min) | |
| if err != nil { | |
| switch err { | |
| case io.ErrShortBuffer: | |
| // handle short buffer error | |
| break | |
| case io.ErrUnexpectedEOF: | |
| // handle short buffer error | |
| break | |
| default: |
| type User struct { | |
| Name string | |
| Email string | |
| } | |
| func main() { | |
| var u *User | |
| u, err := fetchStruct() | |
| if err == nil { | |
| modifyEmail(u) |
| package main | |
| import ( | |
| "net/http" | |
| "github.com/pkg/errors" | |
| ) | |
| type User struct { | |
| Name string |
| func fetchStruct() (*User, error) { | |
| var u User | |
| resp, err := http.Get("url") | |
| if err != nil { | |
| return &u, errors.Wrap(err, "network read error") | |
| } | |
| u = User{ | |
| Name: "some data from resp", | |
| Email: "some data from resp", | |
| } |
| package user | |
| import ( | |
| "net/http" | |
| "github.com/pkg/errors" | |
| ) | |
| type User struct { | |
| Name string |
| package user | |
| import ( | |
| "io" | |
| ) | |
| type User struct { | |
| Name string | |
| Email string | |
| } |
| package user | |
| import ( | |
| "io" | |
| ) | |
| type User struct { | |
| Name string | |
| Email string | |
| } |