Skip to content

Commit fc74efa

Browse files
committed
fix error handling for image download/upload
1 parent f68bb40 commit fc74efa

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/block/design-library/edit.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,22 @@ const checkIfImageUrl = async value => {
5959
const matches = attributeUrl.pathname.match( /\/([^/]+\.(jpe?g|gif|png|mp4|webp))$/i )
6060

6161
if ( matches ) {
62-
const result = await apiFetch( {
63-
path: '/stackable/v3/design_library_image',
64-
method: 'POST',
65-
// eslint-disable-next-line camelcase
66-
data: { image_url: attributeUrl.href },
67-
} )
68-
if ( result.success ) {
69-
return result.new_url
62+
try {
63+
const result = await apiFetch( {
64+
path: '/stackable/v3/design_library_image',
65+
method: 'POST',
66+
// eslint-disable-next-line camelcase
67+
data: { image_url: attributeUrl.href },
68+
} )
69+
if ( result.success ) {
70+
return result.new_url
71+
}
72+
console.error( 'Stackable Design Library:', result.message ) // eslint-disable-line no-console
73+
return value
74+
} catch ( error ) {
75+
console.error( 'Stackable Design Library:', error.message ) // eslint-disable-line no-console
76+
return value
7077
}
71-
72-
console.error( 'Stackable Design Library:', result.message ) // eslint-disable-line no-console
73-
return value
7478
}
7579
return value
7680
}

src/design-library/init.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,15 @@ public function get_design_library_image( $request ) {
179179
if ( is_wp_error( $temp_filepath ) ) {
180180
return new WP_REST_Response( array(
181181
'success' => false,
182-
'message' => 'Failed to retrieve image from the provided URL.'
182+
'message' => $temp_filepath->get_error_message()
183183
), 500 );
184184
}
185185

186186
if ( ! file_exists( $temp_filepath ) || ! wp_filesize( $temp_filepath ) ) {
187-
@unlink( $temp_filepath );
187+
wp_delete_file( $temp_filepath );
188188
return new WP_REST_Response( array(
189189
'success' => false,
190+
// This is a custom check so we return a custom error message.
190191
'message' => 'Invalid file content retrieved from the provided URL.'
191192
), 400 );
192193
}
@@ -205,9 +206,10 @@ public function get_design_library_image( $request ) {
205206
&& ! wp_getimagesize( $temp_filepath )
206207
)
207208
) {
208-
@unlink( $temp_filepath );
209+
wp_delete_file( $temp_filepath );
209210
return new WP_REST_Response( array(
210211
'success' => false,
212+
// This is a custom check so we return a custom error message.
211213
'message' => 'The file is not a valid image/video.'
212214
), 400 );
213215
}
@@ -219,13 +221,13 @@ public function get_design_library_image( $request ) {
219221
) );
220222

221223
if ( file_exists( $temp_filepath ) ) {
222-
@unlink( $temp_filepath );
224+
wp_delete_file( $temp_filepath );
223225
}
224226

225227
if ( is_wp_error( $media_id ) ) {
226228
return new WP_REST_Response( array(
227229
'success' => false,
228-
'message' => 'An error occured during media upload.'
230+
'message' => $media_id->get_error_message()
229231
), 500 );
230232
}
231233

0 commit comments

Comments
 (0)