{"record":{"id":"22e80c285ac76080","repo":"wavetermdev/waveterm","slug":"error-getting-connection-name-from-context-v","errorCode":null,"errorMessage":"error getting connection name from context: %v","messagePattern":"error getting connection name from context: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/remote/connparse/connparse.go","lineNumber":73,"sourceCode":"}\n\nfunc (c *Connection) GetFullURI() string {\n\treturn c.Scheme + \"://\" + c.GetPathWithHost()\n}\n\nfunc (c *Connection) GetSchemeAndHost() string {\n\treturn c.Scheme + \"://\" + c.Host\n}\n\nfunc ParseURIAndReplaceCurrentHost(ctx context.Context, uri string) (*Connection, error) {\n\tconn, err := ParseURI(uri)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error parsing connection: %v\", err)\n\t}\n\tif conn.Host == ConnHostCurrent {\n\t\tsource, err := GetConnNameFromContext(ctx)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error getting connection name from context: %v\", err)\n\t\t}\n\n\t\t// RPC context connection is empty for local connections\n\t\tif source == \"\" {\n\t\t\tsource = wshrpc.LocalConnName\n\t\t}\n\t\tconn.Host = source\n\t}\n\treturn conn, nil\n}\n\nfunc GetConnNameFromContext(ctx context.Context) (string, error) {\n\thandler := wshutil.GetRpcResponseHandlerFromContext(ctx)\n\tif handler == nil {\n\t\treturn \"\", fmt.Errorf(\"error getting rpc response handler from context\")\n\t}\n\treturn handler.GetRpcContext().Conn, nil\n}","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/remote/connparse/connparse.go#L55-L91","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a context that carries an RPC response handler (the wshutil context middleware used for RPC requests) instead of a bare context.Background().","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.","In tests, construct a mock wshutil.RpcResponseHandler with a populated RpcContext.Conn and attach it to the context before calling.","Check the wrapped inner error (%v) to distinguish 'no handler in context' from other context failures."],"exampleFix":"// before\nconn, err := connparse.ParseURIAndReplaceCurrentHost(context.Background(), uri)\n// after\nctx := wshutil.ContextWithRpcResponseHandler(ctx, handler)\nconn, err := connparse.ParseURIAndReplaceCurrentHost(ctx, uri)","handlingStrategy":"validation","validationCode":"if wshutil.GetRpcResponseHandlerFromContext(ctx) == nil {\n    return fmt.Errorf(\"no rpc handler in context; cannot resolve 'current' host\")\n}","typeGuard":null,"tryCatchPattern":"conn, err := connparse.ParseURIAndReplaceCurrentHost(ctx, uri)\nif err != nil {\n    if strings.Contains(err.Error(), \"error getting connection name from context\") {\n        // fall back to explicit connection name\n        conn, err = connparse.ParseURIAndReplaceCurrentHost(ctx, strings.Replace(uri, \"current\", \"local\", 1))\n    }\n    if err != nil { return err }\n}","preventionTips":["Only call ParseURIAndReplaceCurrentHost within RPC request handlers where the handler-injected context is available.","Avoid the 'current' host placeholder in non-RPC code paths; use explicit connection names.","Write tests that mimic the real RPC context rather than context.Background().","Assert handler presence early at the entry point of your command chain."],"tags":["go","context","rpc","connection-parsing"],"backgroundTag":"missing-rpc-handler-in-context","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}