19963: Dedup interface type.
authorTom Clegg <tom@curii.com>
Wed, 22 Feb 2023 15:56:52 +0000 (10:56 -0500)
committerTom Clegg <tom@curii.com>
Wed, 22 Feb 2023 15:56:52 +0000 (10:56 -0500)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

services/keep-web/cache.go
services/keep-web/handler.go

index db06d635092a3ee36daed2d3b72fe1b1b738a909..c73191103e01e0916a08f1314e19d02f53cb61b3 100644 (file)
@@ -5,6 +5,7 @@
 package keepweb
 
 import (
+       "errors"
        "net/http"
        "sync"
        "sync/atomic"
@@ -168,7 +169,7 @@ func (c *cache) GetSession(token string) (arvados.CustomFileSystem, *cachedSessi
        if user == nil || expired {
                user = new(arvados.User)
                err := sess.client.RequestAndDecode(user, "GET", "/arvados/v1/users/current", nil, nil)
-               if statusErr, ok := err.(interface{ HTTPStatus() int }); ok && statusErr.HTTPStatus() == http.StatusForbidden {
+               if he := errorWithHTTPStatus(nil); errors.As(err, &he) && he.HTTPStatus() == http.StatusForbidden {
                        // token is OK, but "get user id" api is out
                        // of scope -- return nil, signifying unknown
                        // user
index afaa51b0ff67c037708ba39a560d7b952239e971..c16f01a1c4facf4426256c8c229b0a479f35ea17 100644 (file)
@@ -67,6 +67,10 @@ func (h *handler) serveStatus(w http.ResponseWriter, r *http.Request) {
        json.NewEncoder(w).Encode(struct{ Version string }{cmd.Version.String()})
 }
 
+type errorWithHTTPStatus interface {
+       HTTPStatus() int
+}
+
 // updateOnSuccess wraps httpserver.ResponseWriter. If the handler
 // sends an HTTP header indicating success, updateOnSuccess first
 // calls the provided update func. If the update func fails, an error
@@ -97,8 +101,7 @@ func (uos *updateOnSuccess) WriteHeader(code int) {
                if code >= 200 && code < 400 {
                        if uos.err = uos.update(); uos.err != nil {
                                code := http.StatusInternalServerError
-                               var he interface{ HTTPStatus() int }
-                               if errors.As(uos.err, &he) {
+                               if he := errorWithHTTPStatus(nil); errors.As(uos.err, &he) {
                                        code = he.HTTPStatus()
                                }
                                uos.logger.WithError(uos.err).Errorf("update() returned %T error, changing response to HTTP %d", uos.err, code)
@@ -381,7 +384,7 @@ func (h *handler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
        var session *cachedSession
        var collectionDir arvados.File
        for _, token = range tokens {
-               var statusErr interface{ HTTPStatus() int }
+               var statusErr errorWithHTTPStatus
                fs, sess, user, err := h.Cache.GetSession(token)
                if errors.As(err, &statusErr) && statusErr.HTTPStatus() == http.StatusUnauthorized {
                        // bad token
@@ -413,9 +416,8 @@ func (h *handler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
        if forceReload && collectionDir != nil {
                err := collectionDir.Sync()
                if err != nil {
-                       var statusErr interface{ HTTPStatus() int }
-                       if errors.As(err, &statusErr) {
-                               http.Error(w, err.Error(), statusErr.HTTPStatus())
+                       if he := errorWithHTTPStatus(nil); errors.As(err, &he) {
+                               http.Error(w, err.Error(), he.HTTPStatus())
                        } else {
                                http.Error(w, err.Error(), http.StatusInternalServerError)
                        }