Skip to content

Commit

Permalink
feat(sdk): re-use captured errors in retrieveLaunchParams. Make error…
Browse files Browse the repository at this point in the history
… more clear
  • Loading branch information
heyqbnk committed Jul 15, 2024
1 parent 9675700 commit 7f423e4
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/sdk/src/launch-params/retrieveLaunchParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import type { LaunchParams } from './types.js';
* @throws Error if extraction was unsuccessful.
*/
export function retrieveLaunchParams(): LaunchParams {
const errors: unknown[] = [];
const errors: string[] = [];

for (const retrieve of [
// Try to retrieve launch parameters from the current location. This method can return
// nothing in case, location was changed and then page was reloaded.
// nothing in case, location was changed, and then the page was reloaded.
retrieveFromLocation,
// Then, try using the lower level API - window.performance.
retrieveFromPerformance,
Expand All @@ -25,9 +25,15 @@ export function retrieveLaunchParams(): LaunchParams {
saveToStorage(lp);
return lp;
} catch (e) {
errors.push(e);
errors.push(e instanceof Error ? e.message : JSON.stringify(e));
}
}

throw new Error('Unable to retrieve launch parameters from any known source. Perhaps, you have opened your app outside of Telegram?\n\n📖 Refer to docs for more information:\nhttps://docs.telegram-mini-apps.com/packages/telegram-apps-sdk/environment');
throw new Error([
'Unable to retrieve launch parameters from any known source. Perhaps, you have opened your app outside Telegram?\n',
'📖 Refer to docs for more information:',
'https://docs.telegram-mini-apps.com/packages/telegram-apps-sdk/environment\n',
'Collected errors:',
errors.map(e => `— ${e}`),
].join('\n'));
}

0 comments on commit 7f423e4

Please sign in to comment.