Skip to content

Commit

Permalink
+ Added configurable via events option to handle indexing errors, for…
Browse files Browse the repository at this point in the history
… instance to prevent exception from stopping whole application
  • Loading branch information
pmaselkowski committed Jun 19, 2018
1 parent e88f06f commit 475e3b8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
22 changes: 22 additions & 0 deletions src/Events/ErrorEvent.php
@@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: peter
* Date: 19.06.18
* Time: 08:39
*/

namespace Maslosoft\Manganel\Events;


use Exception;
use Maslosoft\Mangan\Events\ModelEvent;

class ErrorEvent extends ModelEvent
{
/**
* Exception intercepted by this event
* @var Exception
*/
public $exception = null;
}
Expand Up @@ -12,11 +12,14 @@
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
use Exception;
use function json_decode;
use Maslosoft\Mangan\Events\Event;
use Maslosoft\Manganel\Events\ErrorEvent;
use Maslosoft\Manganel\Manganel;
use function str_replace;

class ExceptionDecorator
class ExceptionHandler
{

public static function getDecorated(Manganel $manganel, BadRequest400Exception $exception, $params)
{
// Throw previous exception,
Expand All @@ -26,14 +29,14 @@ public static function getDecorated(Manganel $manganel, BadRequest400Exception $
$msg = $exception->getMessage();

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

$prevMsg = '';
$previous = $exception->getPrevious();
if(!empty($previous))
if (!empty($previous))
{
$prevMsg = '(' . $previous->getMessage() . ')';
}
Expand All @@ -48,4 +51,23 @@ public static function getDecorated(Manganel $manganel, BadRequest400Exception $
$message = vsprintf("Exception %s while querying `%s`: \n%s\n", $params);
return new BadRequest400Exception($message, 400, $exception);
}

public static function handled(Exception $e, $model = null, $event = null)
{
if(empty($model))
{
return false;
}
if(empty($event))
{
return false;
}
$evt = new ErrorEvent($model);
$evt->exception = $e;
if(Event::hasHandler($model, $event) && Event::handled($model, $event, $evt))
{
return true;
}
return false;
}
}
18 changes: 15 additions & 3 deletions src/IndexManager.php
Expand Up @@ -15,10 +15,13 @@
use Closure;
use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
use Exception;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Events\Event;
use Maslosoft\Mangan\Mangan;
use Maslosoft\Manganel\Events\ErrorEvent;
use Maslosoft\Manganel\Exceptions\ManganelException;
use Maslosoft\Manganel\Helpers\ExceptionDecorator;
use Maslosoft\Manganel\Helpers\ExceptionHandler;
use Maslosoft\Manganel\Helpers\RecursiveFilter;
use Maslosoft\Manganel\Helpers\TypeNamer;
use Maslosoft\Manganel\Meta\ManganelMeta;
Expand All @@ -31,6 +34,7 @@
*/
class IndexManager
{
const EventIndexingError = 'indexingErrorEvent';

/**
* Manganel instance
Expand Down Expand Up @@ -135,7 +139,15 @@ public function index()
}
catch (BadRequest400Exception $e)
{
throw ExceptionDecorator::getDecorated($this->manganel, $e, $params);
throw ExceptionHandler::getDecorated($this->manganel, $e, $params);
}
catch(Exception $e)
{
if(ExceptionHandler::handled($e, $this->model, self::EventIndexingError))
{
return false;
}
throw $e;
}
return false;
}
Expand All @@ -153,7 +165,7 @@ public function get($id = null)
{
if (!$this->isIndexable)
{
return;
return null;
}
$params = $id ? ['id' => (string) $id] : [];
$data = $this->getClient()->get($this->getParams($params))['_source'];
Expand Down
6 changes: 3 additions & 3 deletions src/QueryBuilder.php
Expand Up @@ -18,7 +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\ExceptionHandler;
use Maslosoft\Manganel\Helpers\QueryBuilderDecorator;
use Maslosoft\Manganel\Helpers\RecursiveFilter;
use Maslosoft\Manganel\Helpers\TypeNamer;
Expand Down Expand Up @@ -108,7 +108,7 @@ public function count($q = null)
}
catch (BadRequest400Exception $e)
{
throw ExceptionDecorator::getDecorated($this->manganel, $e, $params);
throw ExceptionHandler::getDecorated($this->manganel, $e, $params);
}
if (empty($result) && empty($result['count']))
{
Expand Down Expand Up @@ -141,7 +141,7 @@ public function search($q = null, &$result = [])
catch (BadRequest400Exception $e)
{

throw ExceptionDecorator::getDecorated($this->manganel, $e, $params);
throw ExceptionHandler::getDecorated($this->manganel, $e, $params);
}

if (empty($result) && empty($result['hits']) && empty($result['hits']['hits']))
Expand Down

0 comments on commit 475e3b8

Please sign in to comment.