Skip to content
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

fix: don't panic on missing shorebird.yaml #18

Merged
merged 6 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions library/src/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,10 @@ fn log_on_error<F, R>(f: F, context: &str, error_result: R) -> R
where
F: FnOnce() -> Result<R, anyhow::Error>,
{
let result = f();
match result {
Ok(r) => r,
Err(e) => {
error!("Error {}: {:?}", context, e);
error_result
}
}
f().unwrap_or_else(|e| {
error!("Error {}: {:?}", context, e);
error_result
})
}

/// Configures updater. First parameter is a struct containing configuration
Expand Down Expand Up @@ -119,7 +115,7 @@ pub extern "C" fn shorebird_init(
pub extern "C" fn shorebird_next_boot_patch_number() -> *mut c_char {
log_on_error(
|| {
let patch = updater::next_boot_patch().context("failed to fetch patch info")?;
let patch = updater::next_boot_patch()?.context("failed to fetch patch info")?;
bryanoltman marked this conversation as resolved.
Show resolved Hide resolved
allocate_c_string(&patch.number.to_string())
},
"fetching next_boot_patch_number",
Expand All @@ -133,7 +129,7 @@ pub extern "C" fn shorebird_next_boot_patch_number() -> *mut c_char {
pub extern "C" fn shorebird_next_boot_patch_path() -> *mut c_char {
log_on_error(
|| {
let patch = updater::next_boot_patch().context("failed to fetch patch info")?;
let patch = updater::next_boot_patch()?.context("failed to fetch patch info")?;
allocate_c_string(&patch.path)
},
"fetching next_boot_patch_path",
Expand All @@ -155,13 +151,20 @@ pub extern "C" fn shorebird_free_string(c_string: *mut c_char) {
/// Check for an update. Returns true if an update is available.
#[no_mangle]
pub extern "C" fn shorebird_check_for_update() -> bool {
return updater::check_for_update();
log_on_error(updater::check_for_update, "checking for update", false)
}

/// Synchronously download an update if one is available.
#[no_mangle]
pub extern "C" fn shorebird_update() {
updater::update();
log_on_error(
|| {
updater::update()?;
bryanoltman marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
},
"downloading update",
(),
);
}

/// Start a thread to download an update if one is available.
Expand All @@ -177,11 +180,7 @@ pub extern "C" fn shorebird_start_update_thread() {
/// shorebird_report_launch_success or shorebird_report_launch_failure.
#[no_mangle]
pub extern "C" fn shorebird_report_launch_start() {
log_on_error(
|| updater::report_launch_start(),
"reporting launch start",
(),
);
log_on_error(updater::report_launch_start, "reporting launch start", ());
}

/// Report that the app failed to launch. This will cause the updater to
Expand All @@ -190,10 +189,7 @@ pub extern "C" fn shorebird_report_launch_start() {
#[no_mangle]
pub extern "C" fn shorebird_report_launch_failure() {
log_on_error(
|| {
updater::report_launch_failure()?;
Ok(())
},
updater::report_launch_failure,
"reporting launch failure",
(),
);
Expand All @@ -209,10 +205,7 @@ pub extern "C" fn shorebird_report_launch_failure() {
#[no_mangle]
pub extern "C" fn shorebird_report_launch_success() {
log_on_error(
|| {
updater::report_launch_success()?;
Ok(())
},
updater::report_launch_success,
"reporting launch success",
(),
);
Expand Down
12 changes: 7 additions & 5 deletions library/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file handles the global config for the updater library.
use anyhow::bail;
bryanoltman marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(test)]
use crate::network::{DownloadFileFn, PatchCheckRequestFn};
Expand All @@ -7,6 +8,7 @@ use std::cell::RefCell;

use crate::updater::AppConfig;
use crate::yaml::YamlConfig;
use crate::UpdateError;

#[cfg(not(test))]
use once_cell::sync::OnceCell;
Expand Down Expand Up @@ -60,14 +62,14 @@ pub fn testing_reset_config() {
});
}

pub fn check_initialized_and_call<F, R>(f: F, config: &ResolvedConfig) -> R
pub fn check_initialized_and_call<F, R>(f: F, config: &ResolvedConfig) -> anyhow::Result<R>
where
F: FnOnce(&ResolvedConfig) -> R,
bryanoltman marked this conversation as resolved.
Show resolved Hide resolved
{
if !config.is_initialized {
panic!("Must call shorebird_init() before using the updater.");
bail!(UpdateError::ConfigNotInitialized);
}
return f(&config);
return Ok(f(&config));
}

#[cfg(test)]
Expand All @@ -93,7 +95,7 @@ where
}

#[cfg(not(test))]
pub fn with_config<F, R>(f: F) -> R
pub fn with_config<F, R>(f: F) -> anyhow::Result<R>
where
F: FnOnce(&ResolvedConfig) -> R,
{
Expand All @@ -106,7 +108,7 @@ where
}

#[cfg(test)]
pub fn with_config<F, R>(f: F) -> R
pub fn with_config<F, R>(f: F) -> anyhow::Result<R>
where
F: FnOnce(&ResolvedConfig) -> R,
{
Expand Down
2 changes: 1 addition & 1 deletion library/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// C doesn't care about the namespaces, but Rust does.
pub mod c_api;

// Declare other .rs file/module exists, but make them public.
// Declare other .rs file/module exists, but make them private.
mod cache;
mod config;
mod logging;
Expand Down
Loading