Merge branch '16997-sort-config-for-diff'
[arvados.git] / lib / controller / rpc / conn.go
index 69bb06c0fd83116a5cc4285d0c448a6ed4b16e3f..19e2d32d2cb01979317fff7dd74a16d3842bfe0e 100644 (file)
@@ -39,7 +39,9 @@ func PassthroughTokenProvider(ctx context.Context) ([]string, error) {
 }
 
 type Conn struct {
-       SendHeader    http.Header
+       SendHeader         http.Header
+       RedactHostInErrors bool
+
        clusterID     string
        httpClient    http.Client
        baseURL       url.URL
@@ -148,7 +150,21 @@ func (conn *Conn) requestAndDecode(ctx context.Context, dst interface{}, ep arva
                path = strings.Replace(path, "/{uuid}", "/"+uuid, 1)
                delete(params, "uuid")
        }
-       return aClient.RequestAndDecodeContext(ctx, dst, ep.Method, path, body, params)
+       err = aClient.RequestAndDecodeContext(ctx, dst, ep.Method, path, body, params)
+       if err != nil && conn.RedactHostInErrors {
+               redacted := strings.Replace(err.Error(), conn.baseURL.String(), "//railsapi.internal", -1)
+               if strings.HasPrefix(redacted, "request failed: ") {
+                       redacted = strings.Replace(redacted, "request failed: ", "", -1)
+               }
+               if redacted != err.Error() {
+                       if err, ok := err.(httpStatusError); ok {
+                               return wrapHTTPStatusError(err, redacted)
+                       } else {
+                               return errors.New(redacted)
+                       }
+               }
+       }
+       return err
 }
 
 func (conn *Conn) BaseURL() url.URL {
@@ -444,14 +460,14 @@ func (conn *Conn) GroupList(ctx context.Context, options arvados.ListOptions) (a
        return resp, err
 }
 
-func (conn *Conn) GroupContents(ctx context.Context, options arvados.ContentsOptions) (arvados.GroupList, error) {
+func (conn *Conn) GroupContents(ctx context.Context, options arvados.GroupContentsOptions) (arvados.ObjectList, error) {
        ep := arvados.EndpointGroupContents
-       var resp arvados.GroupList
+       var resp arvados.ObjectList
        err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
        return resp, err
 }
 
-func (conn *Conn) GroupShared(ctx context.Context, options arvados.SharedOptions) (arvados.GroupList, error) {
+func (conn *Conn) GroupShared(ctx context.Context, options arvados.ListOptions) (arvados.GroupList, error) {
        ep := arvados.EndpointGroupShared
        var resp arvados.GroupList
        err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
@@ -465,6 +481,13 @@ func (conn *Conn) GroupDelete(ctx context.Context, options arvados.DeleteOptions
        return resp, err
 }
 
+func (conn *Conn) GroupTrash(ctx context.Context, options arvados.DeleteOptions) (arvados.Group, error) {
+       ep := arvados.EndpointGroupTrash
+       var resp arvados.Group
+       err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
+       return resp, err
+}
+
 func (conn *Conn) GroupUntrash(ctx context.Context, options arvados.UntrashOptions) (arvados.Group, error) {
        ep := arvados.EndpointGroupUntrash
        var resp arvados.Group
@@ -622,3 +645,26 @@ func (conn *Conn) UserAuthenticate(ctx context.Context, options arvados.UserAuth
        err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
        return resp, err
 }
+
+// httpStatusError is an error with an HTTP status code that can be
+// propagated by lib/controller/router, etc.
+type httpStatusError interface {
+       error
+       HTTPStatus() int
+}
+
+// wrappedHTTPStatusError is used to augment/replace an error message
+// while preserving the HTTP status code indicated by the original
+// error.
+type wrappedHTTPStatusError struct {
+       httpStatusError
+       message string
+}
+
+func wrapHTTPStatusError(err httpStatusError, message string) httpStatusError {
+       return wrappedHTTPStatusError{err, message}
+}
+
+func (err wrappedHTTPStatusError) Error() string {
+       return err.message
+}