fix(editor): check temp close (#1339)

Md Mushfiqur Rahim and FromSi created

## What?

Checks `tmpFile.Close()` when preparing the temporary file for the
external editor.

If closing the temp file fails, the temp file is removed and an
`EditorFinishedMsg` with a contextual error is returned.

## Why?

Closes #728.

Ignoring `tmpFile.Close()` could hide failures that happen while
flushing the written email body before opening the external editor.
Returning the close error makes editor startup failures explicit and
prevents continuing with an unreliable temp file.

---------

Co-authored-by: FromSi <fromsi665@gmail.com>

Change summary

main.go | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)

Detailed changes

main.go 🔗

@@ -2680,19 +2680,32 @@ func openExternalEditor(body string) tea.Cmd {
 	tmpPath := tmpFile.Name()
 
 	if _, err := tmpFile.WriteString(body); err != nil {
-		tmpFile.Close()    //nolint:errcheck,gosec
-		os.Remove(tmpPath) //nolint:errcheck,gosec
+		writeErr := err
+		if err := tmpFile.Close(); err != nil {
+			_ = os.Remove(tmpPath)
+			return func() tea.Msg {
+				return tui.EditorFinishedMsg{Err: fmt.Errorf("closing temp file after write failure: %w", err)}
+			}
+		}
+		_ = os.Remove(tmpPath)
+		return func() tea.Msg {
+			return tui.EditorFinishedMsg{Err: fmt.Errorf("writing temp file: %w", writeErr)}
+		}
+	}
+	if err := tmpFile.Close(); err != nil {
+		_ = os.Remove(tmpPath)
 		return func() tea.Msg {
-			return tui.EditorFinishedMsg{Err: fmt.Errorf("writing temp file: %w", err)}
+			return tui.EditorFinishedMsg{Err: fmt.Errorf("closing temp file: %w", err)}
 		}
 	}
-	tmpFile.Close() //nolint:errcheck,gosec
 
 	parts := strings.Fields(editor)
 	args := append(parts[1:], tmpPath)   //nolint:gocritic
 	c := exec.Command(parts[0], args...) //nolint:gosec,noctx
 	return tea.ExecProcess(c, func(err error) tea.Msg {
-		defer os.Remove(tmpPath) //nolint:errcheck
+		defer func() {
+			_ = os.Remove(tmpPath)
+		}()
 		if err != nil {
 			return tui.EditorFinishedMsg{Err: err}
 		}