Skip to content

Commit

Permalink
* More exception info should be provided #18
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaselkowski committed Jun 15, 2018
1 parent 1c7026c commit 6247dd0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
51 changes: 51 additions & 0 deletions src/Helpers/ExceptionDecorator.php
@@ -0,0 +1,51 @@
<?php
/**
* Created by PhpStorm.
* User: peter
* Date: 15.06.18
* Time: 14:48
*/

namespace Maslosoft\Manganel\Helpers;


use Elasticsearch\Common\Exceptions\BadRequest400Exception;
use Exception;
use function json_decode;
use Maslosoft\Manganel\Manganel;
use function str_replace;

class ExceptionDecorator
{
public static function getDecorated(Manganel $manganel, BadRequest400Exception $exception, $params)
{
// Throw previous exception,
// as it holds more meaningful information
$json = json_encode($params, JSON_PRETTY_PRINT);

$msg = $exception->getMessage();

$decoded = json_decode($msg);
if(!empty($decoded) && !empty($decoded->error->root_cause[0]->reason))
{
$msg = $decoded->error->root_cause[0]->reason;
}

$prevMsg = '';
$previous = $exception->getPrevious();
if(!empty($previous))
{
$prevMsg = '(' . $previous->getMessage() . ')';
}

$params = [
$msg . ' ' . $prevMsg,
$manganel->indexId,
$json
];


$message = vsprintf("Exception %s while querying `%s`: \n%s\n", $params);
return new BadRequest400Exception($message, 400, $exception);
}
}
7 changes: 2 additions & 5 deletions src/IndexManager.php
Expand Up @@ -18,6 +18,7 @@
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Mangan;
use Maslosoft\Manganel\Exceptions\ManganelException;
use Maslosoft\Manganel\Helpers\ExceptionDecorator;
use Maslosoft\Manganel\Helpers\RecursiveFilter;
use Maslosoft\Manganel\Helpers\TypeNamer;
use Maslosoft\Manganel\Meta\ManganelMeta;
Expand Down Expand Up @@ -134,11 +135,7 @@ public function index()
}
catch (BadRequest400Exception $e)
{
// Throw previous exception,
// as it holds more meaningfull information
$previous = $e->getPrevious();
$message = sprintf('Exception while indexing `%s`@`%s`: %s', get_class($this->model), $this->manganel->indexId, $previous->getMessage());
throw new BadRequest400Exception($message, 400, $e);
throw ExceptionDecorator::getDecorated($this->manganel, $e, $params);
}
return false;
}
Expand Down
27 changes: 3 additions & 24 deletions src/QueryBuilder.php
Expand Up @@ -18,6 +18,7 @@
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface;
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
use Maslosoft\Mangan\Traits\CriteriaAwareTrait;
use Maslosoft\Manganel\Helpers\ExceptionDecorator;
use Maslosoft\Manganel\Helpers\QueryBuilderDecorator;
use Maslosoft\Manganel\Helpers\RecursiveFilter;
use Maslosoft\Manganel\Helpers\TypeNamer;
Expand Down Expand Up @@ -107,17 +108,7 @@ public function count($q = null)
}
catch (BadRequest400Exception $e)
{
// Throw previous exception,
// as it holds more meaningfull information
$json = json_encode($params, JSON_PRETTY_PRINT);
$prevMsg = '';
$previous = $e->getPrevious();
if(!empty($previous))
{
$prevMsg = '(' . $previous->getMessage() . ') ';
}
$message = sprintf("Exception %swhile querying `%s`: \n%s\n", $prevMsg, $this->manganel->indexId, $json);
throw new BadRequest400Exception($message, 400, $e);
throw ExceptionDecorator::getDecorated($this->manganel, $e, $params);
}
if (empty($result) && empty($result['count']))
{
Expand Down Expand Up @@ -149,20 +140,8 @@ public function search($q = null, &$result = [])
}
catch (BadRequest400Exception $e)
{
// Throw previous exception,
// as it holds more meaningful information
$json = json_encode($params, JSON_PRETTY_PRINT);
$previous = $e->getPrevious();

$prevMsg = '';
$previous = $e->getPrevious();
if(!empty($previous))
{
$prevMsg = '(' . $previous->getMessage() . ') ';
}

$message = sprintf("Exception %swhile querying `%s`: \n%s\n", $prevMsg, $this->manganel->indexId, $json);
throw new BadRequest400Exception($message, 400, $e);
throw ExceptionDecorator::getDecorated($this->manganel, $e, $params);
}

if (empty($result) && empty($result['hits']) && empty($result['hits']['hits']))
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/_generated/UnitTesterActions.php
@@ -1,4 +1,4 @@
<?php //[STAMP] 0ee060a851d972f1187288383f2a9dcc
<?php //[STAMP] 6ba0d8f61b90d1b075b9e3dbf9c1fb3f
namespace _generated;

// This class was automatically generated by build task
Expand Down

0 comments on commit 6247dd0

Please sign in to comment.