phpunit avoid truncated output - console

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.

Related

Symfony 2.8 : Get message command using Process in controller

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();
}

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.

Errors thrown on Meteor server methods dont log original stack trace

Since upgrading to meteor 1.8.0.2 from 1.6.x I've noticed that any method call that results in an error is printed on the server console as:
Exception while invoking method 'login' [object Object]
Exception while invoking method 'customMethodByMe' [object Object]
This happens both on development on my mac and deployed on Galaxy.
Before, the whole stack trace was printed, but this Object object logging doesnt help me figure out the actual problem. My solution so far has been to wrap method implementation in a try catch statement that logs the original exception and rethrows it for the client to know.
Meteor.methods({
'customMethodByMe'() {
try {
return customMethodByMeImpl();
} catch (e) {
console.log(e)
throw e;
}
}
});
In this case, since the error is on the login method which is in a package, I cannot update it to print the real problem. On the client, all I get on any error is that there was a 500 error on the server, so no root cause either.
{"isClientSafe":true,"error":500,"reason":"Internal server error","message":"Internal server error [500]","errorType":"Meteor.Error"}
Any ideas on how to temporarly solve this? I've been searching for a server level error handler but so far have found nothing.
thanks
I did not get to experience this directly, but when I need to print an object, I usually use JSON.stringfy.
Meteor.methods({
'customMethodByMe'() {
try {
return customMethodByMeImpl();
} catch (e) {
console.log(JSON.stringify(e));
throw e;
}
}
});
This should resolve so that you can at least read the error log.

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...

500 Internal Server Error when using HttpWebRequest, how can I get to the real error?

I'm trying to improve the information provided in response to an error handled within an app.
This is the code:
Try
httpRequestObj = HttpWebRequest.Create(strRequest)
httpRequestObj.Method = "GET"
httpRequestObj.UseDefaultCredentials = True
* httpResponse = httpRequestObj.GetResponse
Using reader As StreamReader = New StreamReader(httpResponse.GetResponseStream())
strXML = reader.ReadToEnd()
End Using
Catch ex As WebException
'do something with ex
End Try
The webexception is thrown on the * line
Currently all I see in the Exception is "The remote server returned an error: (500) Internal Server Error". I've looked at the exception in debug but the info I need isn't there- I guess the response would need to be read in to see that info but it never gets that far.
If I take the request and paste it into my browser directly I can see the error details in XML format that is returned from the API I'm calling, info like:
<Error>
<description>info I want to get to here</description>
<detail />
<code>info I want to get to here</code>
<source />
<category>info I want to get to here</category>
<file>info I want to get to here</file>
<line>info I want to get to here</line>
<pad />
</Error>
Is there any way I can change this code so that I can get past the 500 error and see the actual response, I'd like to be able to parse this xml to find out the real problem for the failure.
Note: the Exception does have an ex.Response (System.Net.HttpWebResponse), but I can't see the info I need in there, only a load of Header info.
You can get the error response from the exception....
try
{
....
} catch(Exception e) {
if (e is WebException && ((WebException)e).Status==WebExceptionStatus.ProtocolError)
{
WebResponse errResp = ((WebException)e).Response;
using(Stream respStream = errResp.GetResponseStream())
{
// read the error response
}
}
}
System.Net.WebResponse response = null;
try
{
response = wreq.GetResponse();
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
string error = new System.IO.StreamReader(e.Response.GetResponseStream()).ReadToEnd();
}
}
catch (Exception e)
{
}
as simple as this, You will get entire response in the string error.
Try to use Fiddler. It's debuging proxy, which will show you all data sending between client and server. You'll be able to see all headers and context as well.

Resources