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

Improve error handling #53

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
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
33 changes: 25 additions & 8 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ async function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
async function waitFor(f) {
while (!f()) await sleep(200);
return f();
let retries = 50;
while (!f() && --retries > 0) {
await sleep(200);
}
if (retries <= 0) {
ssoLogError("timeout while waiting for native messaging host");
return false;
}
return true;
}

/*
Expand All @@ -58,7 +65,7 @@ function is_operational() {
* Update the UI according to the current state
*/
function update_ui() {
if (state_active && accounts.active) {
if (broker_online && state_active && accounts.active) {
chrome.action.enable();
let imgdata = accounts.active.avatar_imgdata;
if (imgdata) {
Expand Down Expand Up @@ -163,13 +170,15 @@ function notify_state_change() {

async function load_accounts() {
port_native.postMessage({ command: "getAccounts" });
await waitFor(() => {
let success = await waitFor(() => {
if (accounts.queried) {
return true;
}
return false;
});
if (accounts.registered.length == 0) {
if (!success) {
return;
} else if (accounts.registered.length == 0) {
ssoLog("no accounts registered");
return;
}
Expand All @@ -185,10 +194,12 @@ async function load_accounts() {
command: "acquireTokenSilently",
account: accounts.active,
});
await waitFor(() => {
let success = await waitFor(() => {
return graph_api_token !== null;
});
if ("error" in graph_api_token) {
if (!success) {
return;
} else if ("error" in graph_api_token) {
ssoLog(
"couldn't aquire API token for avatar: " +
graph_api_token.error,
Expand Down Expand Up @@ -244,12 +255,15 @@ async function get_or_request_prt(ssoUrl) {
account: accounts.active,
ssoUrl: ssoUrl,
});
await waitFor(() => {
let success = await waitFor(() => {
if (prt_sso_cookie.hasData) {
return true;
}
return false;
});
if (!success) {
return { error: "timeout while waiting for native messaging host" };
}
prt_sso_cookie.hasData = false;
const data = prt_sso_cookie.data;
if ("error" in data) {
Expand Down Expand Up @@ -359,10 +373,13 @@ async function on_message_native(response) {
if (host_versions.native === null) {
await load_accounts();
port_native.postMessage({ command: "getVersion" });
} else {
update_ui();
}
} else {
ssoLog("lost connection to broker");
broker_online = false;
update_ui();
}
} else {
ssoLog("unknown command: " + response.command);
Expand Down
Loading