UNPKG

@wordpress/upload-media

Version:
8 lines (7 loc) 6.05 kB
{ "version": 3, "sources": ["../src/error-messages.ts"], "sourcesContent": ["/**\n * User-friendly error messages for upload failures.\n *\n * Provides localized, human-readable messages for all error codes\n * with actionable guidance for users.\n */\n\n/**\n * WordPress dependencies\n */\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { ErrorCode } from './upload-error';\n\n/**\n * Configuration for an error message.\n */\nexport interface ErrorMessageConfig {\n\t/** Short title describing the error type. */\n\ttitle: string;\n\t/** Detailed description of what happened. */\n\tdescription: string;\n\t/** Optional actionable guidance for the user. */\n\taction?: string;\n}\n\n/**\n * Gets a user-friendly error message configuration for an error code.\n *\n * @param code The error code from UploadError.\n * @param fileName The name of the file that failed to upload.\n * @return Error message configuration with title, description, and action.\n */\nexport function getErrorMessage(\n\tcode: ErrorCode | string,\n\tfileName: string\n): ErrorMessageConfig {\n\tconst messages: Record< string, ErrorMessageConfig > = {\n\t\t[ ErrorCode.EMPTY_FILE ]: {\n\t\t\ttitle: __( 'Empty file' ),\n\t\t\tdescription: sprintf(\n\t\t\t\t/* translators: %s: file name */\n\t\t\t\t__( '\"%s\" is empty.' ),\n\t\t\t\tfileName\n\t\t\t),\n\t\t\taction: __( 'Please choose a different file.' ),\n\t\t},\n\t\t[ ErrorCode.SIZE_ABOVE_LIMIT ]: {\n\t\t\ttitle: __( 'File too large' ),\n\t\t\tdescription: sprintf(\n\t\t\t\t/* translators: %s: file name */\n\t\t\t\t__( '\"%s\" exceeds the maximum upload size.' ),\n\t\t\t\tfileName\n\t\t\t),\n\t\t\taction: __( 'Please reduce the file size and try again.' ),\n\t\t},\n\t\t[ ErrorCode.MIME_TYPE_NOT_SUPPORTED ]: {\n\t\t\ttitle: __( 'Unsupported file type' ),\n\t\t\tdescription: sprintf(\n\t\t\t\t/* translators: %s: file name */\n\t\t\t\t__( '\"%s\" is not a supported file type.' ),\n\t\t\t\tfileName\n\t\t\t),\n\t\t\taction: __( 'Please upload a different file format.' ),\n\t\t},\n\t\t[ ErrorCode.MIME_TYPE_NOT_ALLOWED_FOR_USER ]: {\n\t\t\ttitle: __( 'File type not allowed' ),\n\t\t\tdescription: sprintf(\n\t\t\t\t/* translators: %s: file name */\n\t\t\t\t__( 'You are not allowed to upload \"%s\".' ),\n\t\t\t\tfileName\n\t\t\t),\n\t\t\taction: __( 'Please contact your site administrator.' ),\n\t\t},\n\t\t[ ErrorCode.HEIC_DECODE_ERROR ]: {\n\t\t\ttitle: __( 'HEIC decode failed' ),\n\t\t\tdescription: sprintf(\n\t\t\t\t/* translators: %s: file name */\n\t\t\t\t__( 'Failed to decode HEIC file \"%s\".' ),\n\t\t\t\tfileName\n\t\t\t),\n\t\t\taction: __( 'Try converting the image to JPEG or PNG first.' ),\n\t\t},\n\t\t[ ErrorCode.IMAGE_TRANSCODING_ERROR ]: {\n\t\t\ttitle: __( 'Image processing failed' ),\n\t\t\tdescription: sprintf(\n\t\t\t\t/* translators: %s: file name */\n\t\t\t\t__( 'Failed to process \"%s\".' ),\n\t\t\t\tfileName\n\t\t\t),\n\t\t\taction: __( 'The image may be corrupted. Try a different file.' ),\n\t\t},\n\t\t[ ErrorCode.IMAGE_ROTATION_ERROR ]: {\n\t\t\ttitle: __( 'Image rotation failed' ),\n\t\t\tdescription: sprintf(\n\t\t\t\t/* translators: %s: file name */\n\t\t\t\t__( 'Failed to rotate \"%s\".' ),\n\t\t\t\tfileName\n\t\t\t),\n\t\t\taction: __( 'The image may be corrupted. Try a different file.' ),\n\t\t},\n\t\t[ ErrorCode.MEDIA_TRANSCODING_ERROR ]: {\n\t\t\ttitle: __( 'Media processing failed' ),\n\t\t\tdescription: sprintf(\n\t\t\t\t/* translators: %s: file name */\n\t\t\t\t__( 'Failed to convert \"%s\" to the target format.' ),\n\t\t\t\tfileName\n\t\t\t),\n\t\t\taction: __( 'The file may be corrupted. Try a different file.' ),\n\t\t},\n\t\t[ ErrorCode.GENERAL ]: {\n\t\t\ttitle: __( 'Upload failed' ),\n\t\t\tdescription: sprintf(\n\t\t\t\t/* translators: %s: file name */\n\t\t\t\t__( 'Failed to upload \"%s\".' ),\n\t\t\t\tfileName\n\t\t\t),\n\t\t\taction: __( 'Please try again.' ),\n\t\t},\n\t};\n\n\treturn (\n\t\tmessages[ code ] || {\n\t\t\ttitle: __( 'Upload failed' ),\n\t\t\tdescription: sprintf(\n\t\t\t\t/* translators: %s: file name */\n\t\t\t\t__( 'Failed to upload \"%s\".' ),\n\t\t\t\tfileName\n\t\t\t),\n\t\t\taction: __( 'Please try again.' ),\n\t\t}\n\t);\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,kBAA4B;AAK5B,0BAA0B;AAqBnB,SAAS,gBACf,MACA,UACqB;AACrB,QAAM,WAAiD;AAAA,IACtD,CAAE,8BAAU,UAAW,GAAG;AAAA,MACzB,WAAO,gBAAI,YAAa;AAAA,MACxB,iBAAa;AAAA;AAAA,YAEZ,gBAAI,gBAAiB;AAAA,QACrB;AAAA,MACD;AAAA,MACA,YAAQ,gBAAI,iCAAkC;AAAA,IAC/C;AAAA,IACA,CAAE,8BAAU,gBAAiB,GAAG;AAAA,MAC/B,WAAO,gBAAI,gBAAiB;AAAA,MAC5B,iBAAa;AAAA;AAAA,YAEZ,gBAAI,uCAAwC;AAAA,QAC5C;AAAA,MACD;AAAA,MACA,YAAQ,gBAAI,4CAA6C;AAAA,IAC1D;AAAA,IACA,CAAE,8BAAU,uBAAwB,GAAG;AAAA,MACtC,WAAO,gBAAI,uBAAwB;AAAA,MACnC,iBAAa;AAAA;AAAA,YAEZ,gBAAI,oCAAqC;AAAA,QACzC;AAAA,MACD;AAAA,MACA,YAAQ,gBAAI,wCAAyC;AAAA,IACtD;AAAA,IACA,CAAE,8BAAU,8BAA+B,GAAG;AAAA,MAC7C,WAAO,gBAAI,uBAAwB;AAAA,MACnC,iBAAa;AAAA;AAAA,YAEZ,gBAAI,qCA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