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', checkStrategy: 'both',
logger: noop, logger: noop,
debug: false, debug: false,
throwError: false,
}; };
lastChecking?: number; lastChecking?: number;
@ -289,6 +290,7 @@ export class Pushy {
} }
let succeeded = false; let succeeded = false;
this.report({ type: 'downloading' }); this.report({ type: 'downloading' });
let lastError: any;
const diffUrl = (await testUrls(diffUrls)) || _diffUrl; const diffUrl = (await testUrls(diffUrls)) || _diffUrl;
if (diffUrl) { if (diffUrl) {
log('downloading diff'); log('downloading diff');
@ -300,6 +302,7 @@ export class Pushy {
}); });
succeeded = true; succeeded = true;
} catch (e: any) { } catch (e: any) {
lastError = e;
if (__DEV__) { if (__DEV__) {
succeeded = true; succeeded = true;
} else { } else {
@ -317,6 +320,7 @@ export class Pushy {
}); });
succeeded = true; succeeded = true;
} catch (e: any) { } catch (e: any) {
lastError = e;
if (__DEV__) { if (__DEV__) {
succeeded = true; succeeded = true;
} else { } else {
@ -334,6 +338,7 @@ export class Pushy {
}); });
succeeded = true; succeeded = true;
} catch (e: any) { } catch (e: any) {
lastError = e;
if (__DEV__) { if (__DEV__) {
succeeded = true; succeeded = true;
} else { } else {
@ -349,10 +354,14 @@ export class Pushy {
return hash; return hash;
} }
if (!succeeded) { if (!succeeded) {
return this.report({ this.report({
type: 'errorUpdate', type: 'errorUpdate',
data: { newVersion: hash }, data: { newVersion: hash },
}); });
if (lastError) {
throw lastError;
}
return;
} }
log('downloaded hash:', hash); log('downloaded hash:', hash);
setLocalHashInfo(hash, { setLocalHashInfo(hash, {

View File

@ -37,6 +37,15 @@ export const PushyProvider = ({
const [lastError, setLastError] = useState<Error>(); const [lastError, setLastError] = useState<Error>();
const lastChecking = useRef(0); const lastChecking = useRef(0);
const throwErrorIfEnabled = useCallback(
(e: Error) => {
if (options.throwError) {
throw e;
}
},
[options.throwError],
);
const dismissError = useCallback(() => { const dismissError = useCallback(() => {
setLastError(undefined); setLastError(undefined);
}, []); }, []);
@ -115,9 +124,16 @@ export const PushyProvider = ({
} catch (e: any) { } catch (e: any) {
setLastError(e); setLastError(e);
alertError('更新失败', e.message); alertError('更新失败', e.message);
throwErrorIfEnabled(e);
} }
}, },
[alertError, client, options.updateStrategy, alertUpdate], [
client,
options.updateStrategy,
alertUpdate,
alertError,
throwErrorIfEnabled,
],
); );
const downloadAndInstallApk = useCallback( const downloadAndInstallApk = useCallback(
@ -141,6 +157,7 @@ export const PushyProvider = ({
} catch (e: any) { } catch (e: any) {
setLastError(e); setLastError(e);
alertError('更新检查失败', e.message); alertError('更新检查失败', e.message);
throwErrorIfEnabled(e);
return; return;
} }
if (!info) { if (!info) {
@ -195,12 +212,13 @@ export const PushyProvider = ({
); );
} }
}, [ }, [
alertError,
client, client,
downloadAndInstallApk, alertError,
downloadUpdate, throwErrorIfEnabled,
options.updateStrategy, options.updateStrategy,
alertUpdate, alertUpdate,
downloadAndInstallApk,
downloadUpdate,
]); ]);
const markSuccess = client.markSuccess; const markSuccess = client.markSuccess;

View File

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