From 8312b46f144cc1bcf5b6838854df2a9b167c1c3a Mon Sep 17 00:00:00 2001 From: Jason Cox Date: Thu, 22 Feb 2024 20:25:14 -0500 Subject: 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 Acked-by: Robin Jarry --- main.go | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 85538311..f73ac1e9 100644 --- a/main.go +++ b/main.go @@ -158,14 +158,16 @@ func main() { if err != nil { die("%s", err) } - retryExec := false + if len(opts.Command) > 0 { - err := ipc.ConnectAndExec(opts.Command) + response, err := ipc.ConnectAndExec(opts.Command) if err == nil { + if response.Error != "" { + fmt.Printf("response: %s\n", response.Error) + } return // other aerc instance takes over } // continue with setting up a new aerc instance and retry after init - retryExec = true } err = config.LoadConfigFromFile( @@ -212,18 +214,25 @@ func main() { enableIpc := func() { startupDone() - if !retryExec { + if len(opts.Command) == 0 { return } // retry execution - err := ipc.ConnectAndExec(opts.Command) + response, err := ipc.ConnectAndExec(opts.Command) + var errMsg string if err != nil { - fmt.Fprintf(os.Stderr, "Failed to communicate to aerc: %v\n", err) - err = app.CloseBackends() - if err != nil { - log.Warnf("failed to close backends: %v", err) - } - os.Exit(1) + errMsg = err.Error() + } else { + errMsg = response.Error + } + + if errMsg != "" { + // no other aerc instance is running, so let + // this one stay running but show the error + errMsg = fmt.Sprintf("Startup command (%s) failed: %s\n", + strings.Join(opts.Command, " "), errMsg) + log.Errorf(errMsg) + app.PushError(errMsg) } } -- cgit