diff options
| author | Christine Dodrill <me@christine.website> | 2019-01-29 03:21:52 -0800 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2019-01-29 03:21:52 -0800 |
| commit | b395841f422fd9c6e50b4be979da0f308f9499a9 (patch) | |
| tree | 0b6ad736ec9cfd33e5678a8d437a30c444072455 /web | |
| parent | 75c6b7060169f84f88e27c277edbcbfa739d7f54 (diff) | |
| download | x-b395841f422fd9c6e50b4be979da0f308f9499a9.tar.xz x-b395841f422fd9c6e50b4be979da0f308f9499a9.zip | |
web: try using exp/errors, error type for API calls
Diffstat (limited to 'web')
| -rw-r--r-- | web/discordwebhook/webhook.go | 18 | ||||
| -rw-r--r-- | web/error.go | 33 |
2 files changed, 49 insertions, 2 deletions
diff --git a/web/discordwebhook/webhook.go b/web/discordwebhook/webhook.go index 152ec74..a5dc1fc 100644 --- a/web/discordwebhook/webhook.go +++ b/web/discordwebhook/webhook.go @@ -1,11 +1,13 @@ +// Package discordwebhook is a simple low-level HTTP client wrapper around Discord webhooks. package discordwebhook import ( "bytes" "encoding/json" - "fmt" "io/ioutil" "net/http" + + "github.com/Xe/x/web" ) // Webhook is the parent structure fired off to Discord. @@ -64,7 +66,19 @@ func Validate(resp *http.Response) error { return err } resp.Body.Close() - return fmt.Errorf("status code was %v: %s", resp.StatusCode, string(data)) + + loc, err := resp.Location() + if err != nil { + return err + } + + return &web.Error{ + WantStatus: 200, + GotStatus: resp.StatusCode, + URL: loc, + Method: resp.Request.Method, + ResponseBody: string(data), + } } return nil diff --git a/web/error.go b/web/error.go new file mode 100644 index 0000000..e4ffcab --- /dev/null +++ b/web/error.go @@ -0,0 +1,33 @@ +// Package web is a simple collection of high-level error and transport types +// that I end up using over and over. +package web + +import ( + "fmt" + "net/url" + + "within.website/ln" +) + +// Error is an API error. +type Error struct { + WantStatus, GotStatus int + URL *url.URL + Method string + ResponseBody string +} + +func (e Error) Error() string { + return fmt.Sprintf("%s %s: wanted status code %d, got: %d: %v", e.Method, e.URL, e.WantStatus, e.GotStatus, e.ResponseBody) +} + +// F ields for logging. +func (e Error) F() ln.F { + return ln.F{ + "err_want_status": e.WantStatus, + "err_got_status": e.GotStatus, + "err_url": e.URL, + "err_method": e.Method, + "err_response_body": e.ResponseBody, + } +} |
