From b395841f422fd9c6e50b4be979da0f308f9499a9 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Tue, 29 Jan 2019 03:21:52 -0800 Subject: web: try using exp/errors, error type for API calls --- web/error.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 web/error.go (limited to 'web/error.go') 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, + } +} -- cgit v1.2.3