wavetermdev/waveterm · error

error getting connection name from context: %v

Error message

error getting connection name from context: %v

What it means

ParseURIAndReplaceCurrentHost resolves the placeholder 'current' host in a connection URI by looking up the active connection name stored in the RPC context. This error is wrapped when GetConnNameFromContext fails because the context does not carry an RPC response handler, so the library cannot determine which connection 'current' refers to. It indicates the caller invoked the API outside a valid wsh RPC request context.

Source

Thrown at pkg/remote/connparse/connparse.go:73

}

func (c *Connection) GetFullURI() string {
	return c.Scheme + "://" + c.GetPathWithHost()
}

func (c *Connection) GetSchemeAndHost() string {
	return c.Scheme + "://" + c.Host
}

func ParseURIAndReplaceCurrentHost(ctx context.Context, uri string) (*Connection, error) {
	conn, err := ParseURI(uri)
	if err != nil {
		return nil, fmt.Errorf("error parsing connection: %v", err)
	}
	if conn.Host == ConnHostCurrent {
		source, err := GetConnNameFromContext(ctx)
		if err != nil {
			return nil, fmt.Errorf("error getting connection name from context: %v", err)
		}

		// RPC context connection is empty for local connections
		if source == "" {
			source = wshrpc.LocalConnName
		}
		conn.Host = source
	}
	return conn, nil
}

func GetConnNameFromContext(ctx context.Context) (string, error) {
	handler := wshutil.GetRpcResponseHandlerFromContext(ctx)
	if handler == nil {
		return "", fmt.Errorf("error getting rpc response handler from context")
	}
	return handler.GetRpcContext().Conn, nil
}

View on GitHub (pinned to a4447c1563)

Solutions

  1. Pass a context that carries an RPC response handler (the wshutil context middleware used for RPC requests) instead of a bare context.Background().
  2. If there is no active remote connection, explicitly use a concrete connection name or the local connection name in the URI instead of the 'current' placeholder.
  3. In tests, construct a mock wshutil.RpcResponseHandler with a populated RpcContext.Conn and attach it to the context before calling.
  4. Check the wrapped inner error (%v) to distinguish 'no handler in context' from other context failures.

Example fix

// before
conn, err := connparse.ParseURIAndReplaceCurrentHost(context.Background(), uri)
// after
ctx := wshutil.ContextWithRpcResponseHandler(ctx, handler)
conn, err := connparse.ParseURIAndReplaceCurrentHost(ctx, uri)
Defensive patterns

Strategy: validation

Validate before calling

if wshutil.GetRpcResponseHandlerFromContext(ctx) == nil {
    return fmt.Errorf("no rpc handler in context; cannot resolve 'current' host")
}

Try / catch

conn, err := connparse.ParseURIAndReplaceCurrentHost(ctx, uri)
if err != nil {
    if strings.Contains(err.Error(), "error getting connection name from context") {
        // fall back to explicit connection name
        conn, err = connparse.ParseURIAndReplaceCurrentHost(ctx, strings.Replace(uri, "current", "local", 1))
    }
    if err != nil { return err }
}

Prevention

When it happens

Trigger: Calling ParseURIAndReplaceCurrentHost (directly or via parseConnection, RemoteFileCopyCommand, or RemoteFileMoveCommand) with a URI whose host is ConnHostCurrent ('current') while the passed ctx has no RPC response handler attached (wshutil.GetRpcResponseHandlerFromContext returns nil), or where the handler's RpcContext is present but Conn resolution fails.

Common situations: Invoking remote file copy/move commands from a plain CLI or test path without the wsh RPC middleware that injects the connection handler into the context; unit tests calling ParseURIAndReplaceCurrentHost with context.Background(); server code paths executed outside a client RPC request.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/22e80c285ac76080. Report an issue: GitHub.