Skip to content

fix: handle invalid or unreadable saved settings without panicking#222

Open
MorphyKutay wants to merge 1 commit intoCachyOS:developfrom
MorphyKutay:fix/183-saved-json-error-handling
Open

fix: handle invalid or unreadable saved settings without panicking#222
MorphyKutay wants to merge 1 commit intoCachyOS:developfrom
MorphyKutay:fix/183-saved-json-error-handling

Conversation

@MorphyKutay
Copy link
Copy Markdown

Return anyhow::Result from read_json and write_json. On load failure, log and fall back to default saved JSON; on write failure log when closing the window.

Fixes #183

Return anyhow::Result from read_json and write_json. On load failure,
log and fall back to default saved JSON; on write failure log when
closing the window.

Fixes CachyOS#183

Made-with: Cursor
Comment thread src/utils.rs
let buf = fix_path(path);
let data = fs::read_to_string(buf).expect("Unable to read file");
serde_json::from_str(&data).expect("Unable to parse")
let data = fs::read_to_string(buf)?;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let data = fs::read_to_string(buf)?;
let data = fs::read_to_string(buf).context("Unable to read file")?;

Comment thread src/utils.rs
let data = fs::read_to_string(buf).expect("Unable to read file");
serde_json::from_str(&data).expect("Unable to parse")
let data = fs::read_to_string(buf)?;
Ok(serde_json::from_str(&data)?)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Ok(serde_json::from_str(&data)?)
Ok(serde_json::from_str(&data).context("Unable to parse")?)

Comment thread src/utils.rs
let output = File::create(fix_path(path)).expect("Unable to open file for writing");
serde_json::to_writer(output, content).expect("Unable to write json to file");
pub fn write_json(path: &str, content: &serde_json::Value) -> anyhow::Result<()> {
let output = File::create(fix_path(path))?;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let output = File::create(fix_path(path))?;
let output = File::create(fix_path(path)).context("Unable to open file for writing")?;

Comment thread src/utils.rs
serde_json::to_writer(output, content).expect("Unable to write json to file");
pub fn write_json(path: &str, content: &serde_json::Value) -> anyhow::Result<()> {
let output = File::create(fix_path(path))?;
serde_json::to_writer(output, content)?;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
serde_json::to_writer(output, content)?;
serde_json::to_writer(output, content).context("Unable to write json to file")?;

Comment thread src/main.rs
let preferences = unsafe { G_HELLO_WINDOW.as_ref().unwrap().get_preferences("save_path") };
write_json(preferences.as_str().unwrap(), saved_json);
if let Err(e) = write_json(preferences.as_str().unwrap(), saved_json) {
error!("Could not save settings: {e}");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error!("Could not save settings: {e}");
error!("Could not save settings: {e:?}");

Comment thread src/main.rs
match read_json(save_path.as_str()) {
Ok(v) => v,
Err(e) => {
error!("Could not load saved settings from {save_path}: {e}");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error!("Could not load saved settings from {save_path}: {e}");
error!("Could not load saved settings from {save_path}: {e:?}");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

thread panicked at src/utils.rs:32:33

2 participants