Skip to content

Commit

Permalink
feat: Reset compose state if error is recoverable
Browse files Browse the repository at this point in the history
Previously if anything went wrong in the messaging host, we only showed
a notification to the user and the user was stuck with a greyed out
ExtEditorR button.

However in some cases, the error may be fixed by updating ExtEditorR's
and/or the editor's configurations, e.g.

- Host cannot create the temporary EML file
- Host cannot write to the temporary EML file
- Host cannot start the editor

Now when such an error arises, we re-enable ExtEditorR's button and
clear out all received messages for the compose window (which should be
empty in the first place).

For other errors, we keep the button greyed out to avoid overwriting any
data. And there are also errors like version mismatch where users have
to reload ExtEditorR.
  • Loading branch information
Frederick888 committed Jan 26, 2023
1 parent c929318 commit 34ac6cd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ async function nativeMessagingListener(response) {
console.debug(`${manifest.short_name} received: `, response)
if (response.title && response.message) {
await createBasicNotification('', response.title, response.message)
if (response.reset === true) {
delete receivedPerTab[response.tab.id] // maybe do a safety check? but how can we recover?
await messenger.composeAction.enable(response.tab.id)
}
} else {
response.composeDetails.attachments = []

Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ fn handle(request: Exchange, temp_filename: &Path) -> Result<(), messaging::Erro
} else {
return Err(messaging::Error{
tab: request.tab.clone(),
reset: false, // users may want to enable bypass_version_check *and* reload
// ExtEditorR to recover
title: "ExtEditorR version mismatch!".to_owned(),
message: format!(
"Thunderbird extension is {} while native messaging host is {}. The request has been discarded.",
Expand All @@ -38,13 +40,15 @@ fn handle(request: Exchange, temp_filename: &Path) -> Result<(), messaging::Erro
{
let mut temp_file = fs::File::create(temp_filename).map_err(|e| messaging::Error {
tab: request.tab.clone(),
reset: true,
title: "ExtEditorR failed to create temporary file".to_owned(),
message: e.to_string(),
})?;
request
.to_eml(&mut temp_file)
.map_err(|e| messaging::Error {
tab: request.tab.clone(),
reset: true,
title: "ExtEditorR failed to write to temporary file".to_owned(),
message: e.to_string(),
})?;
Expand All @@ -71,6 +75,7 @@ fn handle(request: Exchange, temp_filename: &Path) -> Result<(), messaging::Erro
.output()
.map_err(|e| messaging::Error {
tab: request.tab.clone(),
reset: true,
title: "ExtEditorR failed to start editor".to_owned(),
message: e.to_string(),
})?;
Expand All @@ -80,6 +85,7 @@ fn handle(request: Exchange, temp_filename: &Path) -> Result<(), messaging::Erro
.to_string();
return Err(messaging::Error {
tab: request.tab,
reset: false,
title: "ExtEditorR encountered error from external editor".to_owned(),
message: util::error_message_with_path(stderr, temp_filename),
});
Expand All @@ -90,6 +96,7 @@ fn handle(request: Exchange, temp_filename: &Path) -> Result<(), messaging::Erro
{
let temp_file = fs::File::open(temp_filename).map_err(|e| messaging::Error {
tab: response.tab.clone(),
reset: false,
title: "ExtEditorR failed to read from temporary file".to_owned(),
message: util::error_message_with_path(e, temp_filename),
})?;
Expand All @@ -99,6 +106,7 @@ fn handle(request: Exchange, temp_filename: &Path) -> Result<(), messaging::Erro
.merge_from_eml(&mut reader, messaging::MAX_BODY_LENGTH)
.map_err(|e| messaging::Error {
tab: response.tab.clone(),
reset: false,
title: "ExtEditorR failed to process temporary file".to_owned(),
message: util::error_message_with_path(e, temp_filename),
})?;
Expand Down
1 change: 1 addition & 0 deletions src/model/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ impl Exchange {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Error {
pub tab: Tab,
pub reset: bool,
pub title: String,
pub message: String,
}
Expand Down

0 comments on commit 34ac6cd

Please sign in to comment.