1 import { c, msgid } from 'ttag';
3 import truncate from '@proton/utils/truncate';
7 MAX_FILENAME_CHARS_DISPLAY,
9 MAX_IMPORT_FILE_SIZE_STRING,
10 } from '../constants';
12 const getErrorMessage = (errorType: IMPORT_ERROR_TYPE, filename = '') => {
13 const formattedFilename = `"${truncate(filename, MAX_FILENAME_CHARS_DISPLAY)}"`;
14 if (errorType === IMPORT_ERROR_TYPE.NO_FILE_SELECTED) {
15 return c('Error importing calendar').t`An error occurred uploading your file. No file has been selected.`;
17 if (errorType === IMPORT_ERROR_TYPE.NO_ICS_FILE) {
18 return c('Error importing calendar')
19 .t`An error occurred uploading your file ${formattedFilename}. Only ICS file formats are allowed.`;
21 if (errorType === IMPORT_ERROR_TYPE.FILE_EMPTY) {
22 return c('Error importing calendar').t`Your file ${formattedFilename} is empty.`;
24 if (errorType === IMPORT_ERROR_TYPE.FILE_TOO_BIG) {
25 return c('Error importing calendar')
26 .t`An error occurred uploading your file ${formattedFilename}. Maximum file size is ${MAX_IMPORT_FILE_SIZE_STRING}.`;
28 if (errorType === IMPORT_ERROR_TYPE.INVALID_CALENDAR) {
29 return c('Error importing calendar').t`Your file ${formattedFilename} is not a calendar.`;
31 if (errorType === IMPORT_ERROR_TYPE.INVALID_METHOD) {
32 return c('Error importing calendar')
33 .t`Your file ${formattedFilename} has an invalid method and cannot be imported.`;
35 if (errorType === IMPORT_ERROR_TYPE.NO_EVENTS) {
36 return c('Error importing calendar').t`Your file ${formattedFilename} has no events to be imported.`;
38 if (errorType === IMPORT_ERROR_TYPE.TOO_MANY_EVENTS) {
39 return c('Error importing calendar').ngettext(
40 msgid`Your file ${formattedFilename} contains more than ${MAX_IMPORT_EVENTS} event.`,
41 `Your file ${formattedFilename} contains more than ${MAX_IMPORT_EVENTS} events.`,
45 if (errorType === IMPORT_ERROR_TYPE.FILE_CORRUPTED) {
46 return c('Error importing calendar')
47 .t`An error occurred reading your file ${formattedFilename}. Incorrect file format.`;
51 interface ImportFileErrorCause {
52 errorType: IMPORT_ERROR_TYPE;
53 filename: string | undefined;
56 export class ImportFileError extends Error {
57 cause: ImportFileErrorCause;
59 constructor(errorType: IMPORT_ERROR_TYPE, filename?: string) {
60 super(getErrorMessage(errorType, filename));
61 Object.setPrototypeOf(this, ImportFileError.prototype);
62 this.cause = { errorType, filename };