fix: handle invalid or unreadable saved settings without panicking#222
Open
MorphyKutay wants to merge 1 commit intoCachyOS:developfrom
Open
fix: handle invalid or unreadable saved settings without panicking#222MorphyKutay wants to merge 1 commit intoCachyOS:developfrom
MorphyKutay wants to merge 1 commit intoCachyOS:developfrom
Conversation
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
vnepogodin
requested changes
Apr 22, 2026
| 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)?; |
Member
There was a problem hiding this comment.
Suggested change
| let data = fs::read_to_string(buf)?; | |
| let data = fs::read_to_string(buf).context("Unable to read file")?; |
| 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)?) |
Member
There was a problem hiding this comment.
Suggested change
| Ok(serde_json::from_str(&data)?) | |
| Ok(serde_json::from_str(&data).context("Unable to parse")?) |
| 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))?; |
Member
There was a problem hiding this comment.
Suggested change
| let output = File::create(fix_path(path))?; | |
| let output = File::create(fix_path(path)).context("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))?; | ||
| serde_json::to_writer(output, content)?; |
Member
There was a problem hiding this comment.
Suggested change
| serde_json::to_writer(output, content)?; | |
| serde_json::to_writer(output, content).context("Unable to write json to file")?; |
| 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}"); |
Member
There was a problem hiding this comment.
Suggested change
| error!("Could not save settings: {e}"); | |
| error!("Could not save settings: {e:?}"); |
| match read_json(save_path.as_str()) { | ||
| Ok(v) => v, | ||
| Err(e) => { | ||
| error!("Could not load saved settings from {save_path}: {e}"); |
Member
There was a problem hiding this comment.
Suggested change
| error!("Could not load saved settings from {save_path}: {e}"); | |
| error!("Could not load saved settings from {save_path}: {e:?}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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