Skip to content

Commit 3c8a86a

Browse files
tutman96Nevexo
authored andcommitted
Handle error conditions better and detect support methods automatically
1 parent 2297d46 commit 3c8a86a

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

internal/plugin/install.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ func (p *PluginInstall) GetStatus() (*PluginStatus, error) {
5353
Enabled: p.Enabled,
5454
}
5555

56-
if p.rpcServer != nil && p.rpcServer.status.Status != "disconnected" {
57-
log.Printf("Status from RPC: %v", p.rpcServer.status)
56+
// If the rpc server is connected and the plugin is reporting status, use that
57+
if p.rpcServer != nil &&
58+
p.rpcServer.status.Status != "disconnected" &&
59+
p.rpcServer.status.Status != "unknown" {
5860
status.Status = p.rpcServer.status.Status
5961
status.Message = p.rpcServer.status.Message
6062

@@ -66,7 +68,7 @@ func (p *PluginInstall) GetStatus() (*PluginStatus, error) {
6668
if p.processManager != nil {
6769
status.Status = "running"
6870
if p.processManager.LastError != nil {
69-
status.Status = "errored"
71+
status.Status = "error"
7072
status.Message = p.processManager.LastError.Error()
7173
}
7274
}

internal/plugin/rpc.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net"
1010
"os"
1111
"path"
12+
"slices"
1213
"time"
1314
)
1415

@@ -19,12 +20,17 @@ type PluginRpcStatus struct {
1920

2021
var (
2122
PluginRpcStatusDisconnected = PluginRpcStatus{"disconnected", ""}
23+
PluginRpcStatusUnknown = PluginRpcStatus{"unknown", ""}
2224
PluginRpcStatusLoading = PluginRpcStatus{"loading", ""}
2325
PluginRpcStatusPendingConfiguration = PluginRpcStatus{"pending-configuration", ""}
2426
PluginRpcStatusRunning = PluginRpcStatus{"running", ""}
2527
PluginRpcStatusError = PluginRpcStatus{"error", ""}
2628
)
2729

30+
type PluginRpcSupportedMethods struct {
31+
SupportedRpcMethods []string `json:"supported_rpc_methods"`
32+
}
33+
2834
type PluginRpcServer struct {
2935
install *PluginInstall
3036
workingDir string
@@ -103,10 +109,10 @@ func (s *PluginRpcServer) handleConnection(conn net.Conn) {
103109
// TODO: if read 65k bytes, then likey there is more data to read... figure out how to handle this
104110
n, err := conn.Read(buf)
105111
if err != nil {
106-
log.Printf("Failed to read message: %v", err)
107112
if errors.Is(err, net.ErrClosed) {
108113
s.status = PluginRpcStatusDisconnected
109114
} else {
115+
log.Printf("Failed to read message: %v", err)
110116
s.status = PluginRpcStatusError
111117
s.status.Message = fmt.Errorf("failed to read message: %v", err).Error()
112118
}
@@ -124,21 +130,23 @@ func (s *PluginRpcServer) handleConnection(conn net.Conn) {
124130
}
125131

126132
func (s *PluginRpcServer) handleRpcStatus(ctx context.Context, rpcserver *jsonrpc.JSONRPCServer) {
127-
// log.Printf("Plugin rpc server started. Getting supported methods...")
128-
// supportedMethodsResponse, err := rpcserver.Request("getPluginSupportedMethods", map[string]interface{}{})
129-
// if err != nil {
130-
// log.Printf("Failed to get supported methods: %v", err)
131-
// s.status = PluginRpcStatusError
132-
// s.status.Message = fmt.Errorf("error getting supported methods: %v", err).Error()
133-
// }
134-
135-
// if supportedMethodsResponse.Error != nil {
136-
// log.Printf("Failed to get supported methods: %v", supportedMethodsResponse.Error)
137-
// s.status = PluginRpcStatusError
138-
// s.status.Message = fmt.Errorf("error getting supported methods: %v", supportedMethodsResponse.Error).Error()
139-
// }
140-
141-
// log.Printf("Plugin has supported methods: %v", supportedMethodsResponse.Result)
133+
s.status = PluginRpcStatusUnknown
134+
135+
log.Printf("Plugin rpc server started. Getting supported methods...")
136+
var supportedMethodsResponse PluginRpcSupportedMethods
137+
err := rpcserver.Request("getPluginSupportedMethods", nil, &supportedMethodsResponse)
138+
if err != nil {
139+
log.Printf("Failed to get supported methods: %v", err)
140+
s.status = PluginRpcStatusError
141+
s.status.Message = fmt.Errorf("error getting supported methods: %v", err.Message).Error()
142+
}
143+
144+
log.Printf("Plugin has supported methods: %v", supportedMethodsResponse.SupportedRpcMethods)
145+
146+
if !slices.Contains(supportedMethodsResponse.SupportedRpcMethods, "getPluginStatus") {
147+
log.Printf("Plugin does not support getPluginStatus method")
148+
return
149+
}
142150

143151
ticker := time.NewTicker(1 * time.Second)
144152
for {
@@ -147,7 +155,7 @@ func (s *PluginRpcServer) handleRpcStatus(ctx context.Context, rpcserver *jsonrp
147155
return
148156
case <-ticker.C:
149157
var statusResponse PluginRpcStatus
150-
err := rpcserver.Request("getPluginStatus", map[string]interface{}{}, &statusResponse)
158+
err := rpcserver.Request("getPluginStatus", nil, &statusResponse)
151159
if err != nil {
152160
log.Printf("Failed to get status: %v", err)
153161
if err, ok := err.Data.(error); ok && errors.Is(err, net.ErrClosed) {

ui/src/components/PluginConfigureDialog.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function Dialog({ plugin, setOpen }: { plugin: PluginStatus | null, setOpen: (op
134134
</p>
135135
<Card className={cx(
136136
"text-gray-500 dark:text-gray-400 p-4 border",
137-
plugin.status === "errored" && "border-red-200 bg-red-50 text-red-800 dark:text-red-400",
137+
plugin.status === "error" && "border-red-200 bg-red-50 text-red-800 dark:text-red-400",
138138
)}>
139139
{plugin.message}
140140
</Card>

ui/src/components/PluginStatusIcon.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function PluginStatusIcon({ plugin }: { plugin: PluginStatus; }) {
77
classNames = "bg-green-500 border-green-600";
88
} else if (plugin.enabled && plugin.status === "pending-configuration") {
99
classNames = "bg-yellow-500 border-yellow-600";
10-
} else if (plugin.enabled && plugin.status === "errored") {
10+
} else if (plugin.enabled && plugin.status === "error") {
1111
classNames = "bg-red-500 border-red-600";
1212
}
1313

ui/src/hooks/stores.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ export interface PluginManifest {
561561

562562
export interface PluginStatus extends PluginManifest {
563563
enabled: boolean;
564-
status: "stopped" | "running" | "loading" | "pending-configuration" | "errored";
564+
status: "stopped" | "running" | "loading" | "pending-configuration" | "error";
565565
message?: string;
566566
}
567567

0 commit comments

Comments
 (0)