/** * @param \Zend\Mvc\MvcEvent $oEvent * @return \BoilerAppLogger\Logger\MvcLogger */ public function logError(\Zend\Mvc\MvcEvent $oEvent) { //Create log error $oLogError = new \BoilerAppLogger\Entity\LogErrorEntity(); if (!($oException = $oEvent->getParam('exception')) instanceof \Exception) { if (!($oResponse = $oEvent->getResponse()) instanceof \Zend\Http\Response) { throw new \LogicException(sprintf('Response expects an instance of \\Zend\\Http\\Response, "%s" given', is_object($oResponse) ? get_class($oResponse) : gettype($oResponse))); } $oException = new \RuntimeException($oEvent->getError(), $oResponse->getStatusCode()); } else { $oLogError->setLogErrorFile($oException->getFile())->setLogErrorLine($oException->getLine()); } $this->getLogErrorRepository()->create($oLogError->setLogErrorMessage($oException->getMessage())->setLogErrorCode($oException->getCode())->setLogErrorTrace($oException->getTraceAsString())->setLogErrorLog($this->getCurrentLog())); return $this; }
/** * Log events also take an exception as message argument (here with previous exception) */ public function testCreateWithExceptionWithPrevious() { $logEvent = LogEvent::create(ILogger::LEVEL_ERROR, $this->invargEx, array(), 'PHPUnit'); $logEventData = $logEvent->getArrayCopy(); $this->assertNotEmpty($logEventData); $this->assertInternalType('array', $logEventData); $this->assertArrayHasKey('msg', $logEventData); $this->assertArrayHasKey('exception', $logEventData); $this->assertEquals($this->invargEx->getMessage(), $logEventData['msg']); $this->assertNotEmpty($logEventData['exception']); $this->assertInternalType('array', $logEventData['exception']); $exceptionArray = $logEventData['exception']; $this->assertArrayHasKey('file', $exceptionArray); $this->assertArrayHasKey('message', $exceptionArray); $this->assertArrayHasKey('code', $exceptionArray); $this->assertArrayHasKey('line', $exceptionArray); $this->assertArrayHasKey('trace', $exceptionArray); $this->assertArrayHasKey('previous', $exceptionArray); $this->assertEquals(__FILE__, $exceptionArray['file']); $this->assertEquals($this->invargEx->getMessage(), $exceptionArray['message']); $this->assertEquals($this->invargEx->getCode(), $exceptionArray['code']); $this->assertEquals($this->invargEx->getLine(), $exceptionArray['line']); $this->assertNotEmpty($exceptionArray['trace']); $previousArray = $exceptionArray['previous']; $this->assertArrayHasKey('file', $previousArray); $this->assertArrayHasKey('message', $previousArray); $this->assertArrayHasKey('code', $previousArray); $this->assertArrayHasKey('line', $previousArray); $this->assertArrayHasKey('trace', $previousArray); $this->assertArrayNotHasKey('previous', $previousArray); $this->assertEquals(__FILE__, $previousArray['file']); $this->assertEquals($this->runtimeEx->getMessage(), $previousArray['message']); $this->assertEquals($this->runtimeEx->getCode(), $previousArray['code']); $this->assertEquals($this->runtimeEx->getLine(), $previousArray['line']); $this->assertNotEmpty($previousArray['trace']); }
public function testDefFormatWithPreviousException() { $formatter = new JsonFormatter(); $exception = new \RuntimeException('Foo', 0, new \LogicException('Wut?')); $message = $formatter->format(array('level_name' => 'CRITICAL', 'channel' => 'core', 'context' => array('exception' => $exception), 'datetime' => new \DateTime(), 'extra' => array(), 'message' => 'foobar')); if (version_compare(PHP_VERSION, '5.4.0', '>=')) { $pathPrevious = substr(json_encode($exception->getPrevious()->getFile(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), 1, -1); $pathException = substr(json_encode($exception->getFile(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), 1, -1); } else { $pathPrevious = substr(json_encode($exception->getPrevious()->getFile()), 1, -1); $pathException = substr(json_encode($exception->getFile()), 1, -1); } $this->assertEquals('{"level_name":"CRITICAL","channel":"core","context":{"exception":{"class":"RuntimeException","message":"' . $exception->getMessage() . '","code":' . $exception->getCode() . ',"file":"' . $pathException . ':' . $exception->getLine() . '","previous":{"class":"LogicException","message":"' . $exception->getPrevious()->getMessage() . '","code":' . $exception->getPrevious()->getCode() . ',"file":"' . $pathPrevious . ':' . $exception->getPrevious()->getLine() . '"}}},"datetime":' . json_encode(new \DateTime()) . ',"extra":[],"message":"foobar"}' . "\n", $message); }
/** * Callback to handle both JError and Exception * * @param KCommandContext $context Command chain context * caller => KObject, data => mixed * * @return KException */ protected function _actionException($context) { $exception = $context->data; //if JException then conver it to KException if ($exception instanceof JException) { $exception = new RuntimeException($exception->getMessage(), $exception->getCode()); } //if cli just print the error and exit if (PHP_SAPI == 'cli') { print "\n"; print $exception . "\n"; print debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); exit(0); } $code = $exception->getCode(); //check if the error is code is valid if ($code < 400 || $code >= 600) { $code = KHttpResponse::INTERNAL_SERVER_ERROR; } $context->response->status = $code; $config = array('response' => $context->response, 'request' => $context->request, 'theme' => $this->_application->getTemplate()); //if ajax or the format is not html //then return the exception in json format if ($context->request->isAjax() || $context->request->getFormat() != 'html') { $context->request->setFormat('json'); } $this->getService('com://site/application.controller.exception', $config)->layout('error')->render($exception); $this->send($context); }
/** * Publishes a message to the backend. * * @param Message $message * @return void * @throws \RuntimeException If publishing fails */ public function produce(Message $message) { $type = $message->getType(); $body = array('ticket' => $message->getTicket()); try { $this->logger->debug(sprintf('Publish message for job %s to sonata backend', $message->getTicket()), ['type' => $type, 'body' => $body]); $queue = $this->registry->get($message->getType())->getQueue(); $this->backendProvider->getBackend($queue)->createAndPublish($type, $body); } catch (\Exception $e) { $this->logger->error(sprintf('Failed to publish message (Error: %s)', $e->getMessage()), ['exception' => $e]); if (!$e instanceof \RuntimeException) { $e = new \RuntimeException($e->getMessage(), $e->getCode(), $e); } throw $e; } }
/** * connect to the db * * @param MingoConfig $config * @return boolean */ public function connect(MingoConfig $config = null) { if (!empty($config)) { $this->setConfig($config); } //if $this->connected = false; try { // actually connect to the interface... $this->connected = $this->_connect($this->getConfig()); if ($this->hasDebug()) { if (!is_bool($this->connected)) { throw new UnexpectedValueException(sprintf('_%s() expected to return type: boolean, got: %s', __FUNCTION__, gettype($this->connected))); } //if } //if } catch (Exception $e) { if ($this->hasDebug()) { $e_msg = array(); $msg = array(); foreach ($config as $key => $val) { $msg[] = sprintf('[%s] => %s', $key, empty($val) ? '""' : $val); } //foreach $e_msg[] = sprintf('db connection failed with message "%s" and connection variables: %s', $e->getMessage(), join(',', $msg)); $e = new RuntimeException(join(PHP_EOL, $e_msg), $e->getCode(), $e); } //if $this->handleException($e); } //try/catch return $this->connected; }