Skip to content

Commit

Permalink
use correct exception to response methods in API handler #2766
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Pollock committed Oct 25, 2018
1 parent 4ab7fba commit fb9ca9c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cf2/Fields/Handlers/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function processFiles(array $files,array $hashes ){
$actual = md5_file( $file['tmp_name'] );

if ( $expected !== $actual ) {
throw new \Exception(__( 'Content hash did not match expected.' ), 412 );
throw new Exception(__( 'Content hash did not match expected.' ), 412 );
}


Expand Down
22 changes: 15 additions & 7 deletions cf2/RestApi/File/CreateFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace calderawp\calderaforms\cf2\RestApi\File;


use calderawp\calderaforms\cf2\Exception;
use calderawp\calderaforms\cf2\Fields\FieldTypes\FileFieldType;
use calderawp\calderaforms\cf2\Fields\Handlers\Cf1FileUploader;
use calderawp\calderaforms\cf2\Fields\Handlers\FileUpload;
Expand Down Expand Up @@ -76,7 +77,13 @@ public function permissionsCallback(\WP_REST_Request $request ){
* @since 1.8.0
*
* @param \WP_REST_Request $request
* @return mixed|\WP_Error|\WP_REST_Response
* @return \WP_REST_Response
*/

/**
* @param \WP_REST_Request $request
* @return mixed|null|\WP_REST_Response
* @throws \Exception
*/
public function createItem(\WP_REST_Request $request)
{
Expand All @@ -89,7 +96,8 @@ public function createItem(\WP_REST_Request $request)
$uploader = new Cf1FileUploader();
if( is_wp_error( $uploader ) ){
/** @var \WP_Error $uploader */
return new \WP_REST_Response(['message' => $uploader->get_error_message() ],$uploader->get_error_code() );
$e = Exception::fromWpError($uploader);
return $e->toResponse();
}
$hashes = $request->get_param( 'hashes');
$controlCode = $request->get_param( 'control' );
Expand All @@ -109,14 +117,14 @@ public function createItem(\WP_REST_Request $request)
$transdata,
$uploads
), DAY_IN_SECONDS );
}catch( \Exception $e ){
return new \WP_REST_Response(['message' => $e->getMessage() ],$e->getCode() );
}catch( Exception $e ){
return $e->toResponse();
}



if( is_wp_error( $controlCode ) ){
return $controlCode;
$e = Exception::fromWpError($controlCode);
return $e->toResponse();

}

$response = rest_ensure_response([
Expand Down
1 change: 1 addition & 0 deletions tests/Integration/FileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ public function testProcessInvalidTypeThrowsException()
$field,
new Cf1FileUploader()
);

$handler->processFiles($files, [md5_file($this->test_file)]);
}

Expand Down

0 comments on commit fb9ca9c

Please sign in to comment.