Skip to content

Commit

Permalink
Send plugin error messages to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Feb 26, 2019
1 parent 4c28e68 commit 89fff67
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 0 deletions.
12 changes: 12 additions & 0 deletions client/jsonapi/src/Client/JsonApi/Basket/Address/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public function delete( ServerRequestInterface $request, ResponseInterface $resp
$view->item = $this->controller->get();
$status = 200;
}
catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
{
$status = 409;
$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
}
catch( \Aimeos\MShop\Exception $e )
{
$status = 404;
Expand Down Expand Up @@ -142,6 +148,12 @@ public function post( ServerRequestInterface $request, ResponseInterface $respon
$view->item = $this->controller->get();
$status = 201;
}
catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
{
$status = 409;
$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
}
catch( \Aimeos\MShop\Exception $e )
{
$status = 404;
Expand Down
24 changes: 24 additions & 0 deletions client/jsonapi/src/Client/JsonApi/Basket/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,28 @@ protected function clearCache()
$session->set( $key, null );
}
}


/**
* Translates the plugin error codes to human readable error strings.
*
* @param array $codes Associative list of scope and object as key and error code as value
* @return array List of translated error messages
*/
protected function translatePluginErrorCodes( array $codes )
{
$errors = [];
$i18n = $this->getContext()->getI18n();

foreach( $codes as $scope => $list )
{
foreach( $list as $object => $errcode )
{
$key = $scope . ( $scope !== 'product' ? '.' . $object : '' ) . '.' . $errcode;
$errors[] = $i18n->dt( 'mshop/code', $key );
}
}

return $errors;
}
}
12 changes: 12 additions & 0 deletions client/jsonapi/src/Client/JsonApi/Basket/Coupon/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public function delete( ServerRequestInterface $request, ResponseInterface $resp
$view->item = $this->controller->get();
$status = 200;
}
catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
{
$status = 409;
$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
}
catch( \Aimeos\MShop\Exception $e )
{
$status = 404;
Expand Down Expand Up @@ -142,6 +148,12 @@ public function post( ServerRequestInterface $request, ResponseInterface $respon
$view->item = $this->controller->get();
$status = 201;
}
catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
{
$status = 409;
$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
}
catch( \Aimeos\MShop\Exception $e )
{
$status = 404;
Expand Down
18 changes: 18 additions & 0 deletions client/jsonapi/src/Client/JsonApi/Basket/Product/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public function delete( ServerRequestInterface $request, ResponseInterface $resp
$view->item = $this->controller->get();
$status = 200;
}
catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
{
$status = 409;
$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
}
catch( \Aimeos\MShop\Exception $e )
{
$status = 404;
Expand Down Expand Up @@ -149,6 +155,12 @@ public function patch( ServerRequestInterface $request, ResponseInterface $respo
$view->item = $this->controller->get();
$status = 200;
}
catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
{
$status = 409;
$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
}
catch( \Aimeos\MShop\Exception $e )
{
$status = 404;
Expand Down Expand Up @@ -211,6 +223,12 @@ public function post( ServerRequestInterface $request, ResponseInterface $respon
$view->item = $this->controller->get();
$status = 201;
}
catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
{
$status = 409;
$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
}
catch( \Aimeos\MShop\Exception $e )
{
$status = 404;
Expand Down
12 changes: 12 additions & 0 deletions client/jsonapi/src/Client/JsonApi/Basket/Service/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public function delete( ServerRequestInterface $request, ResponseInterface $resp
$view->item = $this->controller->get();
$status = 200;
}
catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
{
$status = 409;
$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
}
catch( \Aimeos\MShop\Exception $e )
{
$status = 404;
Expand Down Expand Up @@ -153,6 +159,12 @@ public function post( ServerRequestInterface $request, ResponseInterface $respon
$view->item = $this->controller->get();
$status = 201;
}
catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
{
$status = 409;
$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
}
catch( \Aimeos\MShop\Exception $e )
{
$status = 404;
Expand Down
18 changes: 18 additions & 0 deletions client/jsonapi/src/Client/JsonApi/Basket/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public function delete( ServerRequestInterface $request, ResponseInterface $resp
$type = $view->param( 'id', 'default' );
$view->item = $this->controller->setType( $type )->clear()->get();
}
catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
{
$status = 409;
$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
}
catch( \Aimeos\MShop\Exception $e )
{
$status = 404;
Expand Down Expand Up @@ -146,6 +152,12 @@ public function patch( ServerRequestInterface $request, ResponseInterface $respo
$view->item = $basket;
$status = 200;
}
catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
{
$status = 409;
$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
}
catch( \Aimeos\MShop\Exception $e )
{
$status = 404;
Expand Down Expand Up @@ -182,6 +194,12 @@ public function post( ServerRequestInterface $request, ResponseInterface $respon
$view->item = $item;
$status = 200;
}
catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
{
$status = 409;
$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
}
catch( \Aimeos\MShop\Exception $e )
{
$status = 404;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ public function testDeleteById()
}


public function testDeletePluginException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );

$response = $object->delete( $this->view->request(), $this->view->response() );
$result = json_decode( (string) $response->getBody(), true );


$this->assertEquals( 409, $response->getStatusCode() );
$this->assertArrayHasKey( 'errors', $result );
}


public function testDeleteMShopException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
Expand Down Expand Up @@ -166,6 +179,19 @@ public function testPostMultiple()
}


public function testPostPluginException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );

$response = $object->post( $this->view->request(), $this->view->response() );
$result = json_decode( (string) $response->getBody(), true );


$this->assertEquals( 409, $response->getStatusCode() );
$this->assertArrayHasKey( 'errors', $result );
}


public function testPostMShopException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
Expand Down
26 changes: 26 additions & 0 deletions client/jsonapi/tests/Client/JsonApi/Basket/Coupon/StandardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ public function testDeleteById()
}


public function testDeletePluginException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );

$response = $object->delete( $this->view->request(), $this->view->response() );
$result = json_decode( (string) $response->getBody(), true );


$this->assertEquals( 409, $response->getStatusCode() );
$this->assertArrayHasKey( 'errors', $result );
}


public function testDeleteMShopException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
Expand Down Expand Up @@ -171,6 +184,19 @@ public function testPostMultiple()
}


public function testPostPluginException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );

$response = $object->post( $this->view->request(), $this->view->response() );
$result = json_decode( (string) $response->getBody(), true );


$this->assertEquals( 409, $response->getStatusCode() );
$this->assertArrayHasKey( 'errors', $result );
}


public function testPostMShopException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ public function testDeleteById()
}


public function testDeletePluginException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );

$response = $object->delete( $this->view->request(), $this->view->response() );
$result = json_decode( (string) $response->getBody(), true );


$this->assertEquals( 409, $response->getStatusCode() );
$this->assertArrayHasKey( 'errors', $result );
}


public function testDeleteMShopException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
Expand Down Expand Up @@ -163,6 +176,22 @@ public function testPatch()
}


public function testPatchPluginException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );

$body = '{"data": {"attributes": []}}';
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );

$response = $object->patch( $request, $this->view->response() );
$result = json_decode( (string) $response->getBody(), true );


$this->assertEquals( 409, $response->getStatusCode() );
$this->assertArrayHasKey( 'errors', $result );
}


public function testPatchMShopException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
Expand Down Expand Up @@ -248,6 +277,19 @@ public function testPostMultiple()
}


public function testPostPluginException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );

$response = $object->post( $this->view->request(), $this->view->response() );
$result = json_decode( (string) $response->getBody(), true );


$this->assertEquals( 409, $response->getStatusCode() );
$this->assertArrayHasKey( 'errors', $result );
}


public function testPostMShopException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ public function testDeleteById()
}


public function testDeletePluginException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );

$response = $object->delete( $this->view->request(), $this->view->response() );
$result = json_decode( (string) $response->getBody(), true );


$this->assertEquals( 409, $response->getStatusCode() );
$this->assertArrayHasKey( 'errors', $result );
}


public function testDeleteMShopException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
Expand Down Expand Up @@ -178,6 +191,19 @@ public function testPostMultiple()
}


public function testPostPluginException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );

$response = $object->post( $this->view->request(), $this->view->response() );
$result = json_decode( (string) $response->getBody(), true );


$this->assertEquals( 409, $response->getStatusCode() );
$this->assertArrayHasKey( 'errors', $result );
}


public function testPostMShopException()
{
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
Expand Down

0 comments on commit 89fff67

Please sign in to comment.