aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ipc
diff options
context:
space:
mode:
authorJason Cox <me@jasoncarloscox.com>2024-02-22 20:25:14 -0500
committerRobin Jarry <robin@jarry.cc>2024-04-13 21:46:33 +0200
commit8312b46f144cc1bcf5b6838854df2a9b167c1c3a (patch)
tree9a1955f37f5ed3f540ee00419d75a727e2ae299e /lib/ipc
parent941e2e9e7de058f9e6e10efab1ece8be4188468e (diff)
downloadaerc-8312b46f144cc1bcf5b6838854df2a9b167c1c3a.tar.gz
ipc: improve error handling
Detect error responses in addition to errors connecting, sending the request, and receiving the response. If an error occurs when retrying the IPC call, keep the new aerc instance running and simply show the error. We already know (due to the initial failed IPC call) that no other aerc instance is running. Keeping the new instance open also improves the visibility of the error in some cases, such as when clicking on a malformed mailto link causes a new aerc instance to open. Signed-off-by: Jason Cox <me@jasoncarloscox.com> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/ipc')
-rw-r--r--lib/ipc/receive.go2
-rw-r--r--lib/ipc/send.go19
2 files changed, 8 insertions, 13 deletions
diff --git a/lib/ipc/receive.go b/lib/ipc/receive.go
index 79618f50..4f6258ad 100644
--- a/lib/ipc/receive.go
+++ b/lib/ipc/receive.go
@@ -27,7 +27,7 @@ type AercServer struct {
func StartServer(handler Handler, startup context.Context) (*AercServer, error) {
sockpath := xdg.RuntimePath("aerc.sock")
// remove the socket if it is not connected to a session
- if err := ConnectAndExec(nil); err != nil {
+ if _, err := ConnectAndExec(nil); err != nil {
os.Remove(sockpath)
}
log.Debugf("Starting Unix server: %s", sockpath)
diff --git a/lib/ipc/send.go b/lib/ipc/send.go
index fbe67413..d5138bf8 100644
--- a/lib/ipc/send.go
+++ b/lib/ipc/send.go
@@ -9,36 +9,31 @@ import (
"git.sr.ht/~rjarry/aerc/lib/xdg"
)
-func ConnectAndExec(args []string) error {
+func ConnectAndExec(args []string) (*Response, error) {
sockpath := xdg.RuntimePath("aerc.sock")
conn, err := net.Dial("unix", sockpath)
if err != nil {
- return err
+ return nil, err
}
defer conn.Close()
req, err := (&Request{Arguments: args}).Encode()
if err != nil {
- return fmt.Errorf("failed to encode request: %w", err)
+ return nil, fmt.Errorf("failed to encode request: %w", err)
}
_, err = conn.Write(append(req, '\n'))
if err != nil {
- return fmt.Errorf("failed to send message: %w", err)
+ return nil, fmt.Errorf("failed to send message: %w", err)
}
scanner := bufio.NewScanner(conn)
if !scanner.Scan() {
- return errors.New("No response from server")
+ return nil, errors.New("No response from server")
}
resp, err := DecodeResponse(scanner.Bytes())
if err != nil {
- return err
+ return nil, err
}
- // TODO: handle this in a more elegant manner
- if resp.Error != "" {
- fmt.Println("result: ", resp.Error)
- }
-
- return nil
+ return resp, nil
}