Skip to content

Commit

Permalink
throw error
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnylqm committed Jul 24, 2024
1 parent 3355751 commit 454fc28
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class Pushy {
checkStrategy: 'both',
logger: noop,
debug: false,
throwError: false,
};

lastChecking?: number;
Expand Down Expand Up @@ -289,6 +290,7 @@ export class Pushy {
}
let succeeded = false;
this.report({ type: 'downloading' });
let lastError: any;
const diffUrl = (await testUrls(diffUrls)) || _diffUrl;
if (diffUrl) {
log('downloading diff');
Expand All @@ -300,6 +302,7 @@ export class Pushy {
});
succeeded = true;
} catch (e: any) {
lastError = e;
if (__DEV__) {
succeeded = true;
} else {
Expand All @@ -317,6 +320,7 @@ export class Pushy {
});
succeeded = true;
} catch (e: any) {
lastError = e;
if (__DEV__) {
succeeded = true;
} else {
Expand All @@ -334,6 +338,7 @@ export class Pushy {
});
succeeded = true;
} catch (e: any) {
lastError = e;
if (__DEV__) {
succeeded = true;
} else {
Expand All @@ -349,10 +354,14 @@ export class Pushy {
return hash;
}
if (!succeeded) {
return this.report({
this.report({
type: 'errorUpdate',
data: { newVersion: hash },
});
if (lastError) {
throw lastError;
}
return;
}
log('downloaded hash:', hash);
setLocalHashInfo(hash, {
Expand Down
26 changes: 22 additions & 4 deletions src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ export const PushyProvider = ({
const [lastError, setLastError] = useState<Error>();
const lastChecking = useRef(0);

const throwErrorIfEnabled = useCallback(
(e: Error) => {
if (options.throwError) {
throw e;
}
},
[options.throwError],
);

const dismissError = useCallback(() => {
setLastError(undefined);
}, []);
Expand Down Expand Up @@ -115,9 +124,16 @@ export const PushyProvider = ({
} catch (e: any) {
setLastError(e);
alertError('更新失败', e.message);
throwErrorIfEnabled(e);
}
},
[alertError, client, options.updateStrategy, alertUpdate],
[
client,
options.updateStrategy,
alertUpdate,
alertError,
throwErrorIfEnabled,
],
);

const downloadAndInstallApk = useCallback(
Expand All @@ -141,6 +157,7 @@ export const PushyProvider = ({
} catch (e: any) {
setLastError(e);
alertError('更新检查失败', e.message);
throwErrorIfEnabled(e);
return;
}
if (!info) {
Expand Down Expand Up @@ -195,12 +212,13 @@ export const PushyProvider = ({
);
}
}, [
alertError,
client,
downloadAndInstallApk,
downloadUpdate,
alertError,
throwErrorIfEnabled,
options.updateStrategy,
alertUpdate,
downloadAndInstallApk,
downloadUpdate,
]);

const markSuccess = client.markSuccess;
Expand Down
1 change: 1 addition & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ export interface PushyOptions {
autoMarkSuccess?: boolean;
dismissErrorAfter?: number;
debug?: boolean;
throwError?: boolean;
}

0 comments on commit 454fc28

Please sign in to comment.