1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

throw error

This commit is contained in:
sunnylqm 2024-07-24 17:06:49 +08:00
parent 3355751bd5
commit 454fc28c08
No known key found for this signature in database
3 changed files with 33 additions and 5 deletions

View File

@ -37,6 +37,7 @@ export class Pushy {
checkStrategy: 'both',
logger: noop,
debug: false,
throwError: false,
};
lastChecking?: number;
@ -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');
@ -300,6 +302,7 @@ export class Pushy {
});
succeeded = true;
} catch (e: any) {
lastError = e;
if (__DEV__) {
succeeded = true;
} else {
@ -317,6 +320,7 @@ export class Pushy {
});
succeeded = true;
} catch (e: any) {
lastError = e;
if (__DEV__) {
succeeded = true;
} else {
@ -334,6 +338,7 @@ export class Pushy {
});
succeeded = true;
} catch (e: any) {
lastError = e;
if (__DEV__) {
succeeded = true;
} else {
@ -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, {

View File

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

View File

@ -79,4 +79,5 @@ export interface PushyOptions {
autoMarkSuccess?: boolean;
dismissErrorAfter?: number;
debug?: boolean;
throwError?: boolean;
}