Skip to content

Commit 4c4c04a

Browse files
committed
Show error toasts on mobile share failures for easy debugging
Instead of requiring console access, surface all share failures as toasts: - No canShare API available - canShare test fails - File sharing not supported - Share API errors with error name Makes debugging mobile issues visible on phone screen
1 parent 06d8780 commit 4c4c04a

1 file changed

Lines changed: 31 additions & 18 deletions

File tree

src/lib/utils/download.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,36 @@ export async function downloadOrShare(fileId: string, filename?: string): Promis
1616
// Desktop or no Share API support → traditional download
1717
const isMobile = isMobileDevice();
1818
const hasCanShare = typeof navigator.canShare === 'function';
19-
const canShare = isMobile && hasCanShare && navigator.canShare({ files: [new File([], 'test')] });
2019

21-
console.log('[downloadOrShare]', {
22-
fileId,
23-
filename,
24-
isMobile,
25-
hasCanShare,
26-
canShare,
27-
userAgent: navigator.userAgent
28-
});
20+
console.log('[downloadOrShare] Detection:', { isMobile, hasCanShare });
2921

30-
if (!canShare) {
31-
console.log('[downloadOrShare] Using window.open fallback');
22+
if (!isMobile) {
23+
console.log('[downloadOrShare] Desktop detected, using direct download');
24+
window.open(fileUrl, '_blank');
25+
return;
26+
}
27+
28+
if (!hasCanShare) {
29+
addToast('error', 'Web Share API not available on this browser');
30+
console.log('[downloadOrShare] No canShare API, falling back');
31+
window.open(fileUrl, '_blank');
32+
return;
33+
}
34+
35+
// Test if canShare works with files
36+
let canShareFiles = false;
37+
try {
38+
canShareFiles = navigator.canShare({ files: [new File([], 'test')] });
39+
console.log('[downloadOrShare] canShare test result:', canShareFiles);
40+
} catch (e: any) {
41+
console.error('[downloadOrShare] canShare test threw:', e);
42+
addToast('error', `Share test failed: ${e.message || 'Unknown error'}`);
43+
window.open(fileUrl, '_blank');
44+
return;
45+
}
46+
47+
if (!canShareFiles) {
48+
addToast('error', 'File sharing not supported by this browser');
3249
window.open(fileUrl, '_blank');
3350
return;
3451
}
@@ -44,11 +61,7 @@ export async function downloadOrShare(fileId: string, filename?: string): Promis
4461
const blob = await response.blob();
4562
const file = new File([blob], filename || 'download', { type: blob.type });
4663

47-
console.log('[downloadOrShare] Calling navigator.share with file:', {
48-
name: file.name,
49-
type: file.type,
50-
size: file.size
51-
});
64+
console.log('[downloadOrShare] Sharing file:', file.name, file.type, file.size);
5265

5366
await navigator.share({ files: [file] });
5467
console.log('[downloadOrShare] Share succeeded');
@@ -58,13 +71,13 @@ export async function downloadOrShare(fileId: string, filename?: string): Promis
5871

5972
// User cancelled the share sheet → AbortError (don't show error)
6073
if (error.name === 'AbortError') {
61-
console.log('[downloadOrShare] User cancelled share sheet');
74+
console.log('[downloadOrShare] User cancelled');
6275
return;
6376
}
6477

6578
// Actual error → show toast with details
6679
const message = error.message || 'Unknown error';
67-
addToast('error', `Failed to share file: ${message}`);
80+
addToast('error', `Share failed: ${message} (${error.name || 'no error name'})`);
6881

6982
// Fallback: try traditional download
7083
console.log('[downloadOrShare] Falling back to window.open');

0 commit comments

Comments
 (0)