-
Notifications
You must be signed in to change notification settings - Fork 68
fix: handle invalid or unreadable saved settings without panicking #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -55,7 +55,13 @@ fn get_saved_locale() -> Option<String> { | |||||
| fn get_saved_json(preferences: &serde_json::Value) -> serde_json::Value { | ||||||
| let save_path = fix_path(preferences["save_path"].as_str().unwrap()); | ||||||
| if Path::new(&save_path).exists() { | ||||||
| read_json(save_path.as_str()) | ||||||
| match read_json(save_path.as_str()) { | ||||||
| Ok(v) => v, | ||||||
| Err(e) => { | ||||||
| error!("Could not load saved settings from {save_path}: {e}"); | ||||||
| json!({"locale": ""}) | ||||||
| }, | ||||||
| } | ||||||
| } else { | ||||||
| json!({"locale": ""}) | ||||||
| } | ||||||
|
|
@@ -265,7 +271,9 @@ fn on_link1_clicked(param: &[glib::Value]) -> Option<glib::Value> { | |||||
| fn on_delete_window(_param: &[glib::Value]) -> Option<glib::Value> { | ||||||
| let saved_json = &*G_SAVE_JSON.lock().unwrap(); | ||||||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| Some(false.to_value()) | ||||||
| } | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -26,16 +26,17 @@ pub fn fix_path(path: &str) -> String { | |||||
| } | ||||||
|
|
||||||
| #[inline] | ||||||
| pub fn read_json(path: &str) -> serde_json::Value { | ||||||
| pub fn read_json(path: &str) -> anyhow::Result<serde_json::Value> { | ||||||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Ok(serde_json::from_str(&data)?) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| #[inline] | ||||||
| pub fn write_json(path: &str, content: &serde_json::Value) { | ||||||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| serde_json::to_writer(output, content)?; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Ok(()) | ||||||
| } | ||||||
|
|
||||||
| #[inline] | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.