Skip to content

Commit fe43b7b

Browse files
committed
Handle errors on export boards
1 parent 0f0b059 commit fe43b7b

File tree

2 files changed

+75
-37
lines changed

2 files changed

+75
-37
lines changed

src/components/Settings/Export/Export.container.js

+47-31
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,56 @@ export class ExportContainer extends PureComponent {
3333

3434
const { boards, intl, activeBoardId, showNotification } = this.props;
3535
// TODO: Make this block easier to follow.
36-
if (type === 'openboard' && singleBoard) {
37-
await EXPORT_HELPERS.openboardExportAdapter(singleBoard, intl);
38-
} else if (type === 'cboard') {
39-
await EXPORT_HELPERS.cboardExportAdapter(boards, singleBoard);
40-
} else if (type === 'picsee_pdf') {
41-
if (singleBoard) {
42-
await EXPORT_HELPERS[exportConfig.callback]([singleBoard], intl, true);
36+
try {
37+
if (type === 'openboard' && singleBoard) {
38+
await EXPORT_HELPERS.openboardExportAdapter(singleBoard, intl);
39+
} else if (type === 'cboard') {
40+
await EXPORT_HELPERS.cboardExportAdapter(boards, singleBoard);
41+
} else if (type === 'picsee_pdf') {
42+
if (singleBoard) {
43+
await EXPORT_HELPERS[exportConfig.callback](
44+
[singleBoard],
45+
intl,
46+
true
47+
);
48+
} else {
49+
const currentBoard = boards.filter(
50+
board => board.id === activeBoardId
51+
);
52+
await EXPORT_HELPERS[exportConfig.callback](currentBoard, intl, true);
53+
}
54+
} else if (type !== 'pdf' && !singleBoard) {
55+
await EXPORT_HELPERS[exportConfig.callback](boards, intl);
4356
} else {
44-
const currentBoard = boards.filter(board => board.id === activeBoardId);
45-
await EXPORT_HELPERS[exportConfig.callback](currentBoard, intl, true);
57+
if (singleBoard) {
58+
await EXPORT_HELPERS[exportConfig.callback]([singleBoard], intl);
59+
} else {
60+
const currentBoard = boards.filter(
61+
board => board.id === activeBoardId
62+
);
63+
await EXPORT_HELPERS[exportConfig.callback](currentBoard, intl);
64+
}
4665
}
47-
} else if (type !== 'pdf' && !singleBoard) {
48-
await EXPORT_HELPERS[exportConfig.callback](boards, intl);
49-
} else {
50-
if (singleBoard) {
51-
await EXPORT_HELPERS[exportConfig.callback]([singleBoard], intl);
52-
} else {
53-
const currentBoard = boards.filter(board => board.id === activeBoardId);
54-
await EXPORT_HELPERS[exportConfig.callback](currentBoard, intl);
55-
}
56-
}
57-
const showBoardDowloadedNotification = () => {
58-
if (isAndroid())
59-
return showNotification(
60-
intl.formatMessage(messages.boardDownloadedCva)
61-
);
62-
if (isIOS())
63-
return showNotification(
64-
intl.formatMessage(messages.boardDownloadedCvaIOS)
65-
);
66-
return showNotification(intl.formatMessage(messages.boardDownloaded));
67-
};
66+
const showBoardDowloadedNotification = () => {
67+
if (isAndroid())
68+
return showNotification(
69+
intl.formatMessage(messages.boardDownloadedCva)
70+
);
71+
if (isIOS())
72+
return showNotification(
73+
intl.formatMessage(messages.boardDownloadedCvaIOS)
74+
);
75+
return showNotification(intl.formatMessage(messages.boardDownloaded));
76+
};
6877

69-
showBoardDowloadedNotification();
78+
showBoardDowloadedNotification();
79+
} catch (e) {
80+
console.error(e);
81+
const message = e.reason?.message?.startsWith('Failed to fetch')
82+
? messages.downloadNoConnectionError
83+
: messages.boardDownloadError;
84+
showNotification(intl.formatMessage(message));
85+
}
7086
doneCallback();
7187
};
7288

src/components/Settings/Export/Export.helpers.js

+28-6
Original file line numberDiff line numberDiff line change
@@ -945,18 +945,40 @@ export async function pdfExportAdapter(boards = [], intl, picsee = false) {
945945
}
946946
if (isAndroid() || isIOS()) {
947947
requestCvaWritePermissions();
948-
pdfObj.getBuffer(buffer => {
949-
var blob = new Blob([buffer], { type: 'application/pdf' });
950-
const name = 'Download/' + prefix + EXPORT_CONFIG_BY_TYPE.pdf.filename;
951-
writeCvaFile(name, blob);
952-
});
948+
const getBuffer = callback => {
949+
pdfObj.getBuffer(buffer => {
950+
var blob = new Blob([buffer], { type: 'application/pdf' });
951+
const name =
952+
'Download/' + prefix + EXPORT_CONFIG_BY_TYPE.pdf.filename;
953+
writeCvaFile(name, blob);
954+
callback();
955+
});
956+
};
957+
await generatePDF(getBuffer);
953958
} else {
954959
// On a browser simply use download!
955-
pdfObj.download(prefix + EXPORT_CONFIG_BY_TYPE.pdf.filename);
960+
const dowloadPDF = callback =>
961+
pdfObj.download(prefix + EXPORT_CONFIG_BY_TYPE.pdf.filename, callback);
962+
await generatePDF(dowloadPDF);
956963
}
957964
}
958965
}
959966

967+
//To handle PDF generation errors
968+
function generatePDF(callback) {
969+
return new Promise((resolve, reject) => {
970+
function unhandled(e) {
971+
reject(e);
972+
}
973+
setTimeout(() => {
974+
window.removeEventListener('unhandledrejection', unhandled);
975+
reject(new Error('timeout'));
976+
}, 20000);
977+
window.addEventListener('unhandledrejection', unhandled);
978+
callback(resolve);
979+
});
980+
}
981+
960982
function definePDFfont(intl) {
961983
const pdfFonts = { Roboto: FONTS['Roboto'] };
962984
// change font according to locale

0 commit comments

Comments
 (0)