Silex - my error handler isn't working - symfony

I am trying to set up a error handler on my controller to catch anything that might cause my page to malfunction. For example: in this scenario I am trying to catch any error that could possibly happen once my method calls a external API with a bad parameter but it doesn't seem to do anything except give me the typical ClientException in Middleware.php line 69:
Client error: 400 which isn't what I am exactly aiming for. Any advice will be greatly be appreciated or better ways of handling errors in Silex.
private function getSIS($url, $session, Application $app)
{
$message = "You don't have permission to access this!";
if($app['security']->isGranted('ROLE_SUPER_ADMIN'))
{
$client = new Client(['base_uri' => 'https://***********.ca/api/SIS/']);
if (!empty($session))
$response = $client->get($url, ['query' => 'session=' . $session]);
else
$response = $client->get($url);
return $response;
}
$app->error(function (\Exception $e, $code) {
switch($code) {
case 404:
$message = 'The requested page could not be found';
break;
default:
$message = 'We are sorry, but something went terribly wrong.';
}
return new Response($message);
});
return new Response($message);
}

The $app->error method might need to be placed outside the context of your controller actions. I'm not sure exactly how you have your application structured but maybe try placing the error block right before $app->run();
$app->error(function (\Exception $e, $code) use ($app) {
switch($code) {
case 404:
$message = 'The requested page could not be found';
break;
default:
$message = 'We are sorry, but something went terribly wrong.';
}
return new Response($message, $code);
});
$app->run();

Related

Required param state missing from persistent data

I've an issue with php-graph-sdk, I've those functions
protected function getFacebook()
{
static $facebook = null;
if($facebook == null){
$facebook = new Facebook\Facebook([
'app_id' => $this->getAppId(),
'app_secret' => $this->getAppSecret(),
'default_graph_version' => 'v2.10'
]);
}
return $facebook;
}
public function getLoginUrl($url)
{
$fb = $this->getFacebook();
$helper = $fb->getRedirectLoginHelper();
$autorisations = ['email'];
return $helper->getLoginUrl($url , $autorisations);
}
public function callback(&$error = null)
{
$fb = $this->getFacebook();
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exception\ResponseException $e) {
// When Graph returns an error
$error = 'Graph returned an error: ' . $e->getMessage();
return false;
} catch(Facebook\Exception\SDKException $e) {
// When validation fails or other local issues
$error = 'Facebook SDK returned an error: ' . $e->getMessage();
return false;
}
....
}
And I do
$url = $Facebook->getLoginUrl(URL);
And in the callback file
$token = $Facebook->callback($error);
When I click on the link, the callback file is executed and $helper->getAccessToken(); causes this error:
Uncaught Facebook\Exceptions\FacebookSDKException: Cross-site request forgery validation failed. Required param "state" missing from persistent data.
I've seen posts about that and no fix works for me
EDIT:
What I've found currently is that: Facebook SDK error: Cross-site request forgery validation failed. Required param "state" missing from persistent data
Cross-site request forgery validation failed required param state missing from persistent data
and
https://github.com/facebookarchive/php-graph-sdk/issues/1123
https://github.com/facebookarchive/php-graph-sdk/issues/1134
Finally I've solved my issue by switching samesite to Lax by adding that in config.php
ini_set('session.cookie_samesite','Lax');

Passing params to c# WebMethod from PHP/SOAP

I've seen other posts about this but nothing works for me.
Parameters are always null.
Using php soap to call a c# web service (asmx) always results in null values from the service.
Please help! Driving me insane.
[WebMethod]
public string CreateContact(string param1, string param2)
{
return param1 + "-" + param2;
}
$client = new SoapClient('https://etc....?wsdl');
$params = array('param1' => 'abc','param2' => 'xyz');
$result = $client->CreateContact($params);
echo $result->CreateContactResult;
I've tried var_dump also
I don't know what you are trying to do ...
If that is PHP ... you've got multiple errors.
Try this:
<?php
$client = new SoapClient('http://www.thomas-bayer.com/axis2/services/BLZService?wsdl');
$params = array('param1' => 'abc','param2' => 'xyz');
try{
$result = $client->CreateContact($params);
echo $result->CreateContactResult;
} catch (Exception $e) {
echo $e->getMessage();
}
?>
Should return you the error:
Function ("CreateContact") is not a valid method for this service
Valid methods for SoapClient you can find here:
http://php.net/manual/en/class.soapclient.php
Regards,
Ɓukasz Konkol

Symfony2: transactions fail with "There is no active transaction."

I've spend several hours trying to resolve this issue. Google and Stackoverflow didn't help much either. So any advice is most welcome here.
I'm trying to apply a rollback logic with transactions when updating two tables in relation:
The code in general is:
// ...
$em = $this->getDoctrine()->getEntityManager();
$em->getConnection()->beginTransaction();
foreach($dataArr as $data) {
$userObj = $em->getRepository('AcmeBundle:User')->find($userId);
$userObj->setActive(1);
$em->persist($userObj);
$em->getConnection()->commit();
}
$storeObj = $em->getRepository('AcmeBundle:Store')->find($storeId);
$storeObj->setIsOpen(1);
$em->persist($storeObj);
$em->getConnection()->commit();
try {
$em->flush();
$em->clear();
} catch(Exception $e) {
$em->getConnection()->rollback();
$em->close();
throw $e;
}
My PDO drivers are enabled, updating without transactions works as expected, but once I beginTransaction() and try to commit() nothing works and I get the There is no active transaction. exception.
Some sources suggested using only commit() without persist() but it doesn't make any difference. I'm probably doing something really stupid here, but I just cannot see what it is.
After
$this->em->getConnection()->beginTransaction();
you must write:
$this->em->getConnection()->setAutoCommit(false);
It works for me :)
I once accidentally got this error
by doing following:
$em->getConnection()->beginTransaction();
try {
$em->persist($entityA);
$em->flush();
$em->persist($entityB);
$em->flush();
$em->getConnection()->commit();
//exception thrown here
$mailer->send($from, $to, $subject, $text);
} catch (\Exception($ex)) {
$em->getConnection()->rollback();
}
So, you already have guessed that there should not be any code after commit as in the case when this arbitary code($mailer service in our example) throws an exception transaction would be closed before the moment catch block is executed. Maybe this will save a minute or two to somebody:)
Since the version 1.5.2 of DoctrineBundle, you can configure the connection to use auto_commit in the configuration of your project.
# app/config/config.yml (sf2-3) or config/doctrine.yaml (sf4)
doctrine:
dbal:
auto_commit: false
As #prodigitalson correctly suggested I needed to do a commit() before the flush() in order for the get the queries executed. So the working code now is:
$em = $this->getDoctrine()->getEntityManager();
$em->getConnection()->beginTransaction();
foreach($dataArr as $data) {
$userObj = $em->getRepository('AcmeBundle:User')->find($userId);
$userObj->setActive(1);
$em->persist($userObj);
// this is no longer needed
// $em->getConnection()->commit();
}
$storeObj = $em->getRepository('AcmeBundle:Store')->find($storeId);
$storeObj->setIsOpen(1);
$em->persist($storeObj);
// this is no longer needed
// $em->getConnection()->commit();
try {
// Do a commit before the FLUSH
$em->getConnection()->commit();
$em->flush();
$em->clear();
} catch(Exception $e) {
$em->getConnection()->rollback();
$em->close();
throw $e;
}

Symfony2 Doctrine - Flushing in kernel.response listener flushs bad data

In order to do some logging for my Symfony2 app, I created a service that logs any connection, here is the method called on kernel.response :
public function log(FilterResponseEvent $event)
{
$log = new Log();
$request = $event->getRequest();
$response = $event->getResponse();
//fill the Log entity with stuff from request & response data
$manager = $this->container->get('doctrine.orm.entity_manager');
$manager->persist($log);
$manager->flush();
}
All of this seems fine, however when I execute a test like this one (patch with empty data to trigger a failure):
$this->client->request(
'PATCH',
'/users/testificate',
array(
'firstName' => '',
)
);
Which calls this action :
protected function processForm($item, $method = 'PATCH')
{
$form = $this->createForm(new $this->form(), $item, array('method' => $method));
$form->handleRequest($this->getRequest());
if ($form->isValid()) {
$response = new Response();
// Set the `Location` header only when creating new resources
if ($method == 'POST') {
$response->setStatusCode(201);
$response->headers->set('Location',
$this->generateUrl(
'get_' . strtolower($class), array('slug' => $item->getId()),
true // absolute
)
);
}
else {
$response->setStatusCode(204);
}
$this->em->flush();
return $response;
}
$this->em->detach($item);
return RestView::create($form, 400);
}
Although the test fails, the entity is patched, and of course it must not.
After some search what I've learnt is:
The parameters enter the form validator
The validation fails, thus returning a 400 http code without flushing the entity
However during the validation process, the entity gets hydrated with the invalid data
When the service is called on kernel.response, the $manager->flush(); flush all the data... including the bad data provided by the PATCH test.
What I've tried thus far:
1) Do a $manager->clear(); before $manager->persist(); ... doesn't change anything
2) Do a $manager->detach($item); if the form validation failed... doesn't change anything
Thanks !
I recently stumbled across problems with flushing in kernel.response when upgrading from Doctrine 2.3.4 to the latest 2.4 branch. Try flusing the log entities from kernel.terminate. Leave any modifications to the Response in kernel.response.

Symfony2 request called twice

I have a problem with Symfony2 duplicating requests.
The example controller below should just return a transparent gif. However, when the URL is requested there are two 'hits' to the request and doSomething() is called twice instead of once.
// simple action in a controller, ends up being called twice
public function testAction() {
doSomething();
$response = new Response(base64_decode('R0lGODlhAQABAIAAAOrq6gAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='));
$response->headers->set('Content-Type', 'image/gif');
return $response;
}
If I change the response line so it sets a blank response then this stops happening (i.e. doSomething() is only called once):
// with a blank response it is only called once
public function testAction() {
doSomething();
$response = new Response('');
$response->headers->set('Content-Type', 'image/gif');
return $response;
}
If I remove the headers line then it's only called once:
// without the Content-Type header it is only called once
public function testAction() {
doSomething();
$response = new Response(base64_decode('R0lGODlhAQABAIAAAOrq6gAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='));
return $response;
}
Finally (here's the punchline)... if I change the Content-Type to text/html (or text/css) then it is only called once!
// if a text content type is used, it is only called once
public function testAction() {
doSomething();
$response = new Response(base64_decode('R0lGODlhAQABAIAAAOrq6gAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='));
$response->headers->set('Content-Type', 'text/html');
return $response;
}
But with real content and the proper headers it is called twice (and two identical looking entries appear in the web profiler). The same problem also happens if a php template is used to render the content (I haven't tried with twig).
What could be causing this and how can it be prevented (or worked around)?
Edit: So, in summary, it was a Firefox bug and nothing to do with Symfony at all.

Resources