Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media Utils: Don't convert error messages into an array #39448

Merged
merged 4 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ function CoverEdit( {
const isUploadingMedia = isTemporaryMedia( id, url );

const onUploadError = ( message ) => {
createErrorNotice( Array.isArray( message ) ? message[ 2 ] : message, {
type: 'snackbar',
} );
createErrorNotice( message, { type: 'snackbar' } );
};

const mediaElement = useRef();
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function PostFeaturedImageDisplay( {

const { createErrorNotice } = useDispatch( noticesStore );
const onUploadError = ( message ) => {
createErrorNotice( message[ 2 ], { type: 'snackbar' } );
createErrorNotice( message, { type: 'snackbar' } );
};

const controls = (
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/site-logo/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ export default function LogoEdit( {

const { createErrorNotice } = useDispatch( noticesStore );
const onUploadError = ( message ) => {
createErrorNotice( message[ 2 ], { type: 'snackbar' } );
createErrorNotice( message, { type: 'snackbar' } );
};

const controls = canUserEdit && logoUrl && (
Expand Down
4 changes: 4 additions & 0 deletions packages/media-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Breaking Change

- The `onError` now always receives the `message` as a string ([#39448](https://github.com/WordPress/gutenberg/pull/39448)).

## 3.6.0 (2022-05-04)

## 3.5.0 (2022-04-21)
Expand Down
47 changes: 26 additions & 21 deletions packages/media-utils/src/utils/upload-media.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,6 @@ export async function uploadMedia( {
return includes( allowedMimeTypesForUser, fileType );
};

// Build the error message including the filename.
const triggerError = ( error ) => {
error.message = [
<strong key="filename">{ error.file.name }</strong>,
': ',
error.message,
];

onError( error );
};

const validFiles = [];

for ( const mediaFile of files ) {
Expand All @@ -125,10 +114,14 @@ export async function uploadMedia( {
mediaFile.type &&
! isAllowedMimeTypeForUser( mediaFile.type )
) {
triggerError( {
onError( {
code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
message: __(
'Sorry, you are not allowed to upload this file type.'
message: sprintf(
// translators: %s: file name.
__(
'%s: Sorry, you are not allowed to upload this file type.'
),
mediaFile.name
),
file: mediaFile,
} );
Expand All @@ -138,20 +131,28 @@ export async function uploadMedia( {
// Check if the block supports this mime type.
// Defer to the server when type not detected.
if ( mediaFile.type && ! isAllowedType( mediaFile.type ) ) {
triggerError( {
onError( {
code: 'MIME_TYPE_NOT_SUPPORTED',
message: __( 'Sorry, this file type is not supported here.' ),
message: sprintf(
// translators: %s: file name.
__( '%s: Sorry, this file type is not supported here.' ),
mediaFile.name
),
file: mediaFile,
} );
continue;
}

// Verify if file is greater than the maximum file upload size allowed for the site.
if ( maxUploadFileSize && mediaFile.size > maxUploadFileSize ) {
triggerError( {
onError( {
code: 'SIZE_ABOVE_LIMIT',
message: __(
'This file exceeds the maximum upload size for this site.'
message: sprintf(
// translators: %s: file name.
__(
'%s: This file exceeds the maximum upload size for this site.'
),
mediaFile.name
),
file: mediaFile,
} );
Expand All @@ -160,9 +161,13 @@ export async function uploadMedia( {

// Don't allow empty files to be uploaded.
if ( mediaFile.size <= 0 ) {
triggerError( {
onError( {
code: 'EMPTY_FILE',
message: __( 'This file is empty.' ),
message: sprintf(
// translators: %s: file name.
__( '%s: This file is empty.' ),
mediaFile.name
),
file: mediaFile,
} );
continue;
Expand Down