Hmm, fine, this is exception for example:
<?php
/**
* Created by PhpStorm.
* User: User
* Date: 12.05.16
* Time: 15:35
*/
namespace Suzuki\App\Exceptions;
use Exception;
use Phalcon\Di;
use Phalcon\Http\Request;
use Phalcon\Http\Response;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Message;
use Phalcon\Queue\Beanstalk;
use Suzuki\App\Interfaces\ResponseExceptionInterface;
class ModelException extends Exception implements ResponseExceptionInterface
{
/**
* @var Model
*/
protected $model;
/**
* @var array
*/
protected $messages;
/**
* @param string $message
* @param int $code
* @param Exception|null $previous
* @param Model $model
*/
public function __construct($message, $code = 0, Exception $previous = null, Model $model)
{
parent::__construct($message, $code, $previous);
$this->model = $model;
$di = Di::getDefault();
$dispatcher = $di->get('dispatcher');
/** @var Beanstalk $queueLog */
$queueLog = $di->get('queue');
$queueLog->choose('log');
$queueLog->put([
'type' => 'modelException',
'message' => "
Can't save/delete/update/create model of class " . get_class($model) . " due to certain validation messages:" . PHP_EOL .
json_encode($this->getMessages()) . PHP_EOL .
"Model data:" . json_encode($model->toArray()) . PHP_EOL .
PHP_EOL .
'URL: ' . $di->get('request')->getURI() . PHP_EOL .
'Module name:' . $dispatcher->getModuleName() . PHP_EOL .
'Controller name:' . $dispatcher->getControllerName() . PHP_EOL .
'Action name:' . $dispatcher->getActionName() . PHP_EOL .
'Params:' . json_encode($dispatcher->getParams()) . PHP_EOL .
'POST:' . json_encode($_POST) . PHP_EOL .
'JSON:' . json_encode($di->get('request')->getJsonRawBody(true)) . PHP_EOL .
'User id:' . $di->get('session')->get('user') . PHP_EOL .
'IP:' . $di->get('request')->getClientAddress() . PHP_EOL .
'============================================================='
]);
}
private function getMessages()
{
if (empty($this->messages)) {
$messages = [];
/** @var Message $message */
foreach ($this->model->getMessages() as $message) {
$messages += [(string)$message->getField() => $message->getMessage()];
}
$this->messages = $messages;
}
return $this->messages;
}
/**
* @param string $message
* @param Model $model
* @return ModelException
*/
public static function create($message, $model)
{
return new ModelException($message, 0, null, $model);
}
/**
* @return string
*/
public function __toString()
{
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
/**
* @return Response
*/
public function getResponse()
{
$response = new Response();
$messages = $this->getMessages();
return $response->setJsonContent(['error' => $this->message, 'result' => $messages]);
}
}
Then somewhere in code:
if(!$model->save()){
throw ModelException::create("Can't update model", $model);
}
And in dispatcher(im going to move this to seperated class perhaps):
di->set('dispatcher', function () use ($config, $di) {
//Create an event manager
$eventsManager = new Manager();
//Attach a listener for type "dispatch"
$eventsManager->attach(
"dispatch",
function (Event $event, Dispatcher $dispatcher, Exception $exception) use ($di) {
// some code
if($event->getType() == 'beforeException') {
if($exception instanceof ResponseExceptionInterface) {
$dispatcher->setReturnedValue($exception->getResponse());
return false;
}
}
}
}
Also im using queue to do writing to logs, so it doesn't doing it when returning response.