summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Crawshaw <crawshaw@tailscale.com>2020-09-17 09:56:12 -0400
committerDavid Crawshaw <crawshaw@tailscale.com>2020-09-17 09:57:36 -0400
commitad10cd71a626b005d14bc781e037df18be8128fe (patch)
tree8f3f348be3bab458d3434b87a7c623b21b38be9a
parentdea3ef0597d39616276495bab40366cfafc22925 (diff)
downloadtailscale-crawshaw/jsonhandler.tar.xz
tailscale-crawshaw/jsonhandler.zip
tsweb: when unwrapping HTTPError, record the user-facing message also in the logcrawshaw/jsonhandler
There's often some useful piece of information in there not already repeated in the internal error. Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
-rw-r--r--tsweb/jsonhandler.go3
-rw-r--r--tsweb/tsweb.go7
-rw-r--r--tsweb/tsweb_test.go5
3 files changed, 12 insertions, 3 deletions
diff --git a/tsweb/jsonhandler.go b/tsweb/jsonhandler.go
index 56704da7c..3199138e6 100644
--- a/tsweb/jsonhandler.go
+++ b/tsweb/jsonhandler.go
@@ -58,6 +58,9 @@ func (fn JSONHandlerFunc) ServeHTTPReturn(w http.ResponseWriter, r *http.Request
// the client in this handler. We don't want the wrapping
// ReturnHandler to do it too.
err = werr.Err
+ if werr.Msg != "" {
+ err = fmt.Errorf("%s: %w", werr.Msg, err)
+ }
} else {
resp = &response{
Status: "error",
diff --git a/tsweb/tsweb.go b/tsweb/tsweb.go
index 1c8ff01bc..649abdfa0 100644
--- a/tsweb/tsweb.go
+++ b/tsweb/tsweb.go
@@ -236,8 +236,13 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case hErrOK:
// Handler asked us to send an error. Do so, if we haven't
// already sent a response.
+ msg.Err = hErr.Msg
if hErr.Err != nil {
- msg.Err = hErr.Err.Error()
+ if msg.Err == "" {
+ msg.Err = hErr.Err.Error()
+ } else {
+ msg.Err = msg.Err + ": " + hErr.Err.Error()
+ }
}
if lw.code != 0 {
h.logf("[unexpected] handler returned HTTPError %v, but already sent a response with code %d", hErr, lw.code)
diff --git a/tsweb/tsweb_test.go b/tsweb/tsweb_test.go
index 6b4dcaf28..2e94ef752 100644
--- a/tsweb/tsweb_test.go
+++ b/tsweb/tsweb_test.go
@@ -122,7 +122,7 @@ func TestStdHandler(t *testing.T) {
Host: "example.com",
Method: "GET",
RequestURI: "/foo",
- Err: testErr.Error(),
+ Err: "not found: " + testErr.Error(),
Code: 404,
},
},
@@ -139,6 +139,7 @@ func TestStdHandler(t *testing.T) {
Host: "example.com",
Method: "GET",
RequestURI: "/foo",
+ Err: "not found",
Code: 404,
},
},
@@ -189,7 +190,7 @@ func TestStdHandler(t *testing.T) {
Host: "example.com",
Method: "GET",
RequestURI: "/foo",
- Err: testErr.Error(),
+ Err: "not found: " + testErr.Error(),
Code: 200,
},
},