zend database error output - zend-framework

I cant see my error of Sql. It cuts my queries, and the error is meaningless except it informs there is an error.
Like
error in statement "select * from o.."
how can I get full query, so i can investigate how the error occured?
I previously wrote a sql function that throws debugging string if sql has error. I though zend'ers have needed and there should exists a code:
if(($error = mysql_error($conn)){
$cagiri=debug_backtrace();
$i=count($cagiri);
And says
[Caller__Function__] => dbSave
[Caller__Class__] => classBasic
[Arguments] => Array
(
[0] => function : loadLinks
[1] =>
[1] => sql error: Duplicate entry 'http://www.istanbulboncugu.com/Lokma' for key 'url'
[2] => query: insert into downloadLinks set `title`= 'Lokma : İstanbul - Avrupa', `url`= 'http://www.istanbulboncugu.com/Lokma', `site`= 'rssSehirFirsati', `status`= 'new'
So I dont dig 30 query, if there is error,
Error string shows function that composed the query. Etc.
I think Zend have lacks that kind of awareness.
I know zend not prefers hard and log path against smart ways ?!?

I start using that code, if there is error.
function results($q){
$results = Array();
try{
$results = $this->_db->query($q)->fetchAll();
}catch(Exception $e){
var_dump($q);
die($e->getMessage());
}
return $results;
}

If you are using Zend_Db_Select you can get the full created query by doing
$select->__toString();
In reply to your comment, this is how you can get the SQL error message:
try {
$this->db->insert('Users', $array );
} catch (Exception $e){
echo $e->getMessage();
}

Related

TYPO3, Extbase: mysqli error message, commands out of sync

I try to update a really old extbase extension which is from another programmer. TYPO3-Version is 8.7.13
The following function in my Repository throws the error:
"Commands out of sync; you can't run this command now
Doctrine\DBAL\Driver\Mysqli\MysqliException thrown in file
/Volumes/web/src/typo3_src-8.7.13/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php in line 249."
And this ist the function:
Public Function delete($standort, $month)
{
$m = date('m',$month);
$y = date('Y',$month);
$sql = "
DELETE FROM mytable
WHERE standort = ".$standort." AND FROM_UNIXTIME( monat, '%m' ) = $m AND FROM_UNIXTIME( monat, '%Y' ) = $y ";
$query = $this->createQuery();
$query->statement($sql);
$query->execute(TRUE);
}
with $query->execute(FALSE); no error is thrown but the sql is not executed. With $query->execute(TRUE); the error appears but the sql will be executed.
Can anybody help?
Thanks!
What about changing this statement to Doctrine? You can find the documentation here: https://docs.typo3.org/typo3cms/CoreApiReference/8.7/ApiOverview/Database/BasicCrud/Index.html#delete-a-row

Slim 3 blackholing errors

I have a small slim 3 app, and when I throw an exception slim simply shows the generic error message:
Slim Application Error
A website error has occurred. Sorry for the temporary inconvenience.
In slim 2 you can do something like this to turn on debug mode giving you backtraces etc:
$app->config('debug', true);
In slim 3 there doesn't seem to be one. Additionally, it seems to be overriding my exception and error handlers.
How can I get slim to spit out errors, or at least to call my error handlers (Which pipe the output to kint for debug information)
Looking through the source, it's possible to initialize slim 3 with error display like so:
$app = new \Slim\App(['settings' => ['displayErrorDetails' => true]]);
I'm not sure if it's possible to change this setting after the fact without replacing the errorHandler altogether.
To show full stack trace on default exception handler use what j-v said.
If you want to handle exceptions in Slim yourself then you need to override Slim's default exception handler as it will be used before your "not in Slim" error handler:
$app = new \Slim\App();
$container = $app->getContainer();
$container['errorHandler'] = function(ServerRequestInterface $request, ResponseInterface $response, Exception $exception) {
//Handle exception here
}
Error handling is rather well documented: Official Docs
$app = new \Slim\App();
$c = $app->getContainer();
$c['errorHandler'] = function ($c) {
return function ($request, $response, $exception) use ($c) {
return $c['response']->withStatus(500)
->withHeader('Content-Type', 'text/html')
->write('Something went wrong!');
};
};
Error handling is best solution to this. You can do something like to see Error Trace
$app = new \Slim\App();
$container = $app->getContainer();
$container['phpErrorHandler'] = $container['errorHandler'] = function ($c) {
return function ($request, $response, $exception) use ($c) {
return $c['response']->withStatus(500)
->withHeader('Content-Type', 'text/html')
->write('Something went wrong!<br><br>' .
nl2br($error->getTraceAsString()));
};
};
Make displayErrorDetails->true.
You will find cause of error.
$config = ['settings' => [
'addContentLengthHeader' => true,
'displayErrorDetails' => true
]];
$app = new \Slim\App($config)

MongoCursorTimeoutException in jenssegers/laravel-mongodb

I have a query which looks up data in a huge collection (over 48M), yet even if I add timeout=-1 to it, it still throws MongoCursorTimeoutException exception..
return \DB::connection('mongodb')->collection('stats')->timeout(-1)
->where('ip','=',$alias)
->where('created_at','>=', new \DateTime( $date ) )
->where('created_at','<=', new \DateTime( $date . ' 23:59:59' ) )
->count();
I am using this library: https://github.com/jenssegers/laravel-mongodb
Any ideas?
There is an issue PHP-1249 - MongoCursor::count() should use cursor's socket timeout submitted for PHP MongoDB driver v1.5.7 which was fixed in v1.5.8 in October, 2014.
The reply from the support:
Looking into the code a bit, it appears that both socket timeout and maxTimeMS is not passed along to the count command.
If you need an immediate work-around, you should be able to get by with MongoDB::command() for now (which can support both timeouts).
The workaround posted by one of the users is:
$countComand = $mongo->command(
array(
'count' => 'collection',
'query' => $query
),
array('socketTimeoutMS' => -1)
);
if($countComand['ok']){
$count = $countComand['n'];
} else {
// ... error ...
}
It seems that laravel-mongodb doesn't use MongoDB::command(). You either have to write your query explicitly without help of where methods as shown above or upgrade to v.1.5.8.

Modify Exception Handling using Hooks in Dancer

I'm trying to set up a hook to catch all exceptions and errors thrown from my Dancer application ( an API ) and pass them to a function that sets the HTTP status code and returns the hash ( serialized as JSON ).
Everything works fine when I use try/catch, but when I move it to a hook it runs the code but the response is formed using the default error mechanism instead of my function.
This is the hook I'm using:
# Handle errors
hook on_handler_exception => sub {
my $e = shift;
debug "ON HANDLER EXCEPTION";
return API::Exception->handle($e); # sets status code and returns hash depending on the exception
};
I also tried using halt instead of return to stop any further processing of the exception but it didn't alter anything.
How would I accomplish this with Dancer? Thanks.
use the "on_route_exception" hook instead ...
hook on_route_exception => sub
{
my ( $exception ) = #_;
error( $exception );
status( 'error' );
halt( { errors => [ { message => 'An unhandled exception occurred', code => 0 } ] } );
};
Have a look at the code of Dancer::Error.
I think something like
my $content = Dancer::Engine->engine("template")->apply_renderer($template_name, $ops);
return Dancer::Response->new(
status => $self->code,
headers => ['Content-Type' => 'text/html'],
content => $content);
from the _render_html method could help you.

Zend fetchRow() not working

I'm trying to fetch a row with a where statement but for some reason it throws an error at me.
This is the line
$row = $this->getDbTable()->fetchRow("order = $order");
I've put a die(); before this line and it does die,
Then I've put a die(); after this line and the die() doesn't get executed but throws an error.
The error doesn't help me much it only says "An error occurred Application error", there's nothing in my php error log either.
Help!
Going by your comments, I would try doing the where part 'properly'? E.g.:
$select = $this->getDbTable()->select()->where('order = ?', $order);
$row = $this->getDbTable()->fetchRow($select);
What is the situation you are needing to select by order? Is there a primary key you can select by?
Update:
Given your comments, maybe use update directly:
$table = $this->getDbTable();
$data = array( 'order' => $order+1 );
$where = $table->getAdapter()->quoteInto('order = ?', $order);
$table->update($data, $where);