Symfony 2.8 : Get message command using Process in controller - symfony

I have a file command handling data add database . I use Document Symfony 2.8 Process for the handling in the controller.
Everything was running fine. But I got an error . In the file command I use if-else to check data. When processed and running $process->run() , I cannot get the error in else (in else. I use echo to show the error).
if (!empty($id)) {
// handling code
} else {
echo "not found Id"
}
Do you have any ideas? help me please !
Document i use : https://symfony.com/doc/current/components/process.html

Normaly you can get the output of the process by using : echo $process->getOutput(); or in your case you can throw exception like
if (!empty($id)) {
// handling code
} else {
throw new \Exception('Id not found');
}
and put your process within try catch block like that
try {
$process->mustRun();
echo $process->getOutput();
} catch (\Exception $exception) {
echo $exception->getMessage();
}

Related

PDO query to local sqlite file returns "No such Table" error SQLSTATE[HY000]

Trying to select data into php page running on local Mac MAMP environment from sqlite file of my Things database.
Both DB connection & select code below and Things.sqlite files are located in same directory on the machine.
Getting "SQLSTATE[HY000]: General error: 1 no such table: TMTask" error though there's clearly such table as visible on the screenshot of viewing file in Base app.
Screenshot
<?php
try {
$db = new PDO("sqlite:".__DIR__."Things.sqlite3");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
echo "Unable to connect to database";
echo $e->getMessage();
exit;
}
try {
$results = $db->query("SELECT * from TMTask");
echo "Retrieved results.";
} catch (Exception $e) {
echo "Unable to retrieve results: ".$e->getMessage();
//exit;
}
var_dump($results->fetchAll());
?>
Thanks #your-common-sense! It helped. I figured out there was missing "/" in the file path.

How to retrieve error messages from Sabre API

I am using the getReservationRQ API (I am able to retrieve other info from the API) but I am not able to echo out the error messages. Does anyone have any ideas? Please see my code below.
private function handle_api_error($xml) {
//echo "<textarea>".$xml->asXML()."</textarea>";
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['soap-env'])->Body;
//$this->echo_xml($soap);
foreach($soap->children($ns['stl17'])->GetReservationRS->Errors as $error) {
$error_msg .= $error->Message.'<br />';
}
$this->error_msg="Sabre SOAP API error: ".$error_msg;
return false;
}
I recommend you to use some tool like Postman or SoapUI to see the actual error you are getting.
My guess is that it might be a different namespace: $ns['???']

Error establishing a database connection - WordPress

How am I going to fix this problem? I tried many methods to solve this one but none of them work.I already changed the default value on each forms,still the same. Need some advice here. Thanks!
enter image description here
try this->
$con=new MySQLi("localhost","root","pwd","db");
try
{
if($con)
{
echo "Db connected successfully"
}
else
{
die("Error In Establishing Db Connection");
}
}
catch(Exception $ex)
{
echo $ex->getMessage();
}
then after include this connection file in other where u need it use $con object to fire queries and get results.Also Check your wp.config file for credentials and database and also for localhost

phpunit avoid truncated output

How can I avoid phpunit testing output being truncated:
1) Tests\ApiTest::testGetMetricList
GuzzleHttp\Exception\ServerException: Server error: `GET http://localhost/micobe/myproject_p4/index.php/investors/get_metric_list` resulted in a `500 Internal Server Error` response:
<br />
<font size='1'><table class='xdebug-error xe-exception' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr (truncated...)
/var/www/html/landing-myproject-page/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113
/var/www/html/landing-myproject-page/vendor/guzzlehttp/guzzle/src/Middleware.php:65
/var/www/html/landing-myproject-page/vendor/guzzlehttp/promises/src/Promise.php:203
/var/www/html/landing-myproject-page/vendor/guzzlehttp/promises/src/Promise.php:156
/var/www/html/landing-myproject-page/vendor/guzzlehttp/promises/src/TaskQueue.php:47
/var/www/html/landing-myproject-page/vendor/guzzlehttp/promises/src/Promise.php:246
/var/www/html/landing-myproject-page/vendor/guzzlehttp/promises/src/Promise.php:223
/var/www/html/landing-myproject-page/vendor/guzzlehttp/promises/src/Promise.php:267
/var/www/html/landing-myproject-page/vendor/guzzlehttp/promises/src/Promise.php:225
/var/www/html/landing-myproject-page/vendor/guzzlehttp/promises/src/Promise.php:62
/var/www/html/landing-myproject-page/vendor/guzzlehttp/guzzle/src/Client.php:131
/var/www/html/landing-myproject-page/app/Http/Controllers/ApiController.php:171
/var/www/html/landing-myproject-page/tests/ApiTest.php:31
If you experienced the difficult task to debug the (truncated...) error message through PhpUnit  see below global solution !
The exception caught is related to the guzzle version
A try-catch around your phpunit test would get the GuzzleException $e
Example
 $client = new GuzzleHttp\Client(['base_uri' => 'https://foo.com/api/']);
try {
$response = $client->request('GET','/v1/testYourEndpoint');
} catch (\GuzzleHttp\Exception\ClientErrorResponseException $e) {
var_dump($e->getResponse()->getBody()->getContents());
} catch (\GuzzleHttp\Exception\RequestException $e) {
print_r($e->xdebug_message);
var_dump($e->getResponse()->getBody()->getContents());
} catch (\GuzzleHttp\Exception\ClientException $e) {
var_dump($e->getResponse()->getBody()->getContents());
}
Guzzle truncated the output, not PHPUnit. If you're using Laravel you can use dd($response->getBody()); to get the full output.

Supress Error messages in Symfony/Twig?

How can I suppress error messages in a symfony controller / swift message, e.g. if the email of the user doesn't exist, something is wrong with the smpt server or the email address simply doesn't comply with RFC 2822.
For example, we got following CRITICAL error...
request.CRITICAL: Swift_RfcComplianceException: Address in mailbox given [ xxx#xxx.com ] does not comply with RFC 2822, 3.6.2. (uncaught exception) at $
... the user then get's a symfony error page "An Error occured" which I need to suppress in any case.
A simple #$this->get('mailer')->send($message); doesn't work here unfortunately ...
protected function generateEmail($name)
{
$user = $this->getDoctrine()
->getRepository('XXX')
->findOneBy(array('name' => $name));
if (!$user) {
exit();
}
else {
$message = \Swift_Message::newInstance()
->setSubject('xxx')
->setFrom(array('xxx#xxx.com' => 'xxx'))
->setTo($user->getEmail())
->setContentType('text/html')
->setBody(
$this->renderView(
'AcmeBundle:Template:mail/confirmed.html.twig'
)
)
;
$this->get('mailer')->send($message);
// a simple #$this->get('mailer')->send($message); doesn't work here
}
return true;
}
To simply suppress the error, wrap the send method in a try-catch block. Choose the exception type wisely. The following example just catches Swift_RfcComplicanceExceptions.
try {
$this->get('mailer')->send($message);
} catch (\Swift_RfcComplianceException $exception) {
// do something like proper error handling or log this error somewhere
}
I'd consider it advisably to apply some validation beforehand, so you can display a nice error to your user.
I'm not sure that a try catch block will work, because mails may be sent later in the request process : http://symfony.com/fr/doc/current/components/http_kernel/introduction.html#l-evenement-kernel-terminate
If you use the framework Full Stack edition, this is the default behavior.
I've also found some refs here : Unable to send e-mail from within custom Symfony2 command but can from elsewhere in app
You can change the Swiftmailer spool strategy in your config to send direct mails...

Resources