1
0
Fork 0
mirror of https://git.sr.ht/~rjarry/aerc synced 2025-02-23 07:53:59 +01:00
aerc/lib/ipc/send.go
Jason Cox 8312b46f14 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>
2024-04-13 21:46:33 +02:00

39 lines
761 B
Go

package ipc
import (
"bufio"
"errors"
"fmt"
"net"
"git.sr.ht/~rjarry/aerc/lib/xdg"
)
func ConnectAndExec(args []string) (*Response, error) {
sockpath := xdg.RuntimePath("aerc.sock")
conn, err := net.Dial("unix", sockpath)
if err != nil {
return nil, err
}
defer conn.Close()
req, err := (&Request{Arguments: args}).Encode()
if err != nil {
return nil, fmt.Errorf("failed to encode request: %w", err)
}
_, err = conn.Write(append(req, '\n'))
if err != nil {
return nil, fmt.Errorf("failed to send message: %w", err)
}
scanner := bufio.NewScanner(conn)
if !scanner.Scan() {
return nil, errors.New("No response from server")
}
resp, err := DecodeResponse(scanner.Bytes())
if err != nil {
return nil, err
}
return resp, nil
}