Typo3 scheduler handling exceptions? - exception

I'm currently programming a scheduler task to run in my typo3 backend. For more security and a better debuggable workflow I want to throw exceptions that get displayed in the "scheduled tasks" section when running a task as well as display a red box rather than a green box because the task has failed. Unfortunately I cant get it work. Returning an exception ends in a printed exception-string with a green/success infobox. When just throw the exception by throw new Exception ends in a red/error infobox with no hint what the exception message was.
public function importCommand($filetype) {
try {
if(!$this->isValidFileTypeConfigured($filetype)) {
throw new \TYPO3\MbxRealestate\Helper\Exception\ImportImmoException('Unsupported filetype "' . $filetype . '" configured in ' . __CLASS__ . '::' . __FUNCTION__);
}
....
} catch (\Exception $ex) {
throw $ex; // throwing ...
return $ex; // or returning
}
return true;
}

You can use the Flash Message API in TYPO3 to output your errors. There are 5 types of errors and respective styling. See here:
http://docs.typo3.org/TYPO3/CoreApiReference/ApiOverview/FlashMessages/Index.html

Related

Show SQL error Message in AngularJS

I'm trying to create an application where the user can explicitly see the SQL rules they're violating if they enter a bad input to be persisted or used to query the database. I would like to show this as a popup message on the browser, also I'm using AngularJS on the client side and SpringBoot on the backend. I've seen the post where they are discussing how to print it on the console,
Show SQL error message.
public static void printSQLException(SQLException ex) {
for (Throwable e : ex) {
if (e instanceof SQLException) {
if (ignoreSQLException(
((SQLException)e).
getSQLState()) == false) {
e.printStackTrace(System.err);
System.err.println("SQLState: " +
((SQLException)e).getSQLState());
System.err.println("Error Code: " +
((SQLException)e).getErrorCode());
System.err.println("Message: " + e.getMessage());
Throwable t = ex.getCause();
while(t != null) {
System.out.println("Cause: " + t);
t = t.getCause();
}
}
}
}
}
But in that question it is showing how to retrieve it in the console, not the browser. By default the HttpError message is from SpringBoot where it gives a BadSQLGrammarExcpetion. I require the exact SQLError Message to show the actual error that occurs on the database. I'm a bit new to AngularJS since I'm mainly a backend developer. So if they're are any examples that can be offered I'd really appreciate it.
So I took a different approach...rather than explicitly trying to display the SQL errors which have occurred, based on the scenario which took place I've written my own Exceptions messages and displayed them through the error object. This doesn't give the users too much information so it may lead to security breaches, but just enough to know the problem that has occurred.

Checking to see if a property exists without throwing exception yii2

Depending on database result, some properties of my object may be populated or not.
Lets say I have a task object and if there is a message for it, so it has the $message property populated:
if($task->message === null)
throw new ErrorException('what the ...');
The problem is, whenever I want to check if this property is populated (accessing it), it throws an Getting unknown property exception and the execution terminates.
I think you can try with isset
if (isset($task->message )) {
// your code for is setted
} else {
// your code for not setted
}
Try-catch block also works:
use yii\base\Exception;
[...]
try {
echo $task->message;
} catch (Exception $e) {
// stuff
}

Silex : log dbal exceptions

I'm using Silex micro-framework. I searched a method to log every dbal exceptions (from requests, console commands, etc...)
I didn't find something which works. Even if I declare a callback function in $app->error();, it doesn't works to Console exceptions
How can I tell to dbal to log in a file all db errors/exceptions ?
If you have the fat version of Silex you should already have exception logging with MonologServiceProvider, as long as it has been registered.
The console is usually an instance of Symfony\Component\Console\Application (not Silex\Application).
Edit One way to accomplish logging console exceptions is to set console catch exceptions to false:
$console->setCatchExceptions(false);
And then wrap $console->run(); in a try catch:
try {
$console->run();
}
catch(\Doctrine\DBAL\DBALException $e) {
$app['monolog']->addError(sprintf('Console DBALException %s %s %s', $e->getMessage(), $e->getTraceAsString(), $e->getFile(), $e->getLine()));
}
catch(\Exception $e) {
$app['monolog']->addError(sprintf('Console Exception %s %s %s', $e->getMessage(), $e->getTraceAsString(), $e->getFile(), $e->getLine()));
}

exception tracking and blocking it in zend framework 2

How to stop exception from showing in zend framework 2 and instead when exception is thrown i want to redirect to 404 page .
Actually when user fires wrong url or some how any query gets executed in a wrong way exception is thrown , so i need to block this exception and instead redirect to any other well designed page . I'm unable to track the the exception point or rather catch the exception or from where exception is generated . I have used this code
You can handle the exceptions in anyway you want after catching it as the following example in which you are catching the exception globally...:
In the onBootstrap method i have attached the following code in Module.php in a function to execute when an event occurs, the following attach a function to be executed when an error (exception) is raised:
public function onBootstrap(MvcEvent $e)
{
$application = $e->getApplication();
$em = $application->getEventManager();
//handle the dispatch error (exception)
$em->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this,
'handleError'));
//handle the view render error (exception)
$em->attach(\Zend\Mvc\MvcEvent::EVENT_RENDER_ERROR, array($this,
'handleError'));
}
and then defineed in module.php only the function to handle the error
public function handleError(MvcEvent $e)
{
//get the exception
$exception = $e->getParam('exception');
//...handle the exception... maybe log it and redirect to another page,
//or send an email that an exception occurred...
}
I found this code from stackoverflow only , but it is not working , i mean when i'm passing wrong parameters in url , it is showing " A 404 error occurred
Page not found.
The requested controller was unable to dispatch the request.
Controller:
Front\Controller\Front
No Exception available "
Please i need help on this.
you can turn off exceptions in zf2 by chaining 'display_exceptions' => TRUE to 'display_exceptions' => false, [module/Application/config/module.config.php]

How to implement expected exceptions?

I am trying my first features with Behat and I am facing the problem I don't know how to implement expected exceptions.
I found the issue https://github.com/Behat/Behat/issues/140 and robocoder is talking about one possible way, which is used by Behat, too. But it seems that they aren't really handling exceptions.
My point is to achieve forced exception handling. I don't want any construct catching all exceptions and forget them.
One possible way would be:
When <player> transfers <transfer> from his account it should fail with <error>
Implementation
try {
...
} catch (\Exception $ex) {
assertEquals($error, $ex->getMessage());
}
I don't like the scenario description. I want to use the then keyword, e.g.
When <player> transfers <transfer> from his account
Then it should fail with error <error>
This description has the disadvantage I need two methods:
method1($arg1, $arg2) {
// Test the transfer
}
method2($arg1, $arg2) {
// Check if the exception is the right one
}
To be able to check in method2 the exception needs to be stored.
The only possible way I see is to use a try/catch and store it to a variable.
Someone else would catch it and do nothing with it. Nobody will notice, when running the tests.
How can I prevent that exceptions are discarded?
Has anybody else a similar scenario implemented?
Thanks for any hints.
EDIT:
Behat context:
playerTransfer($player, $amount) {
$player->transfer($amount);
}
Method from entity class:
transfer($amount) {
if ($this->getWealth() < $amount) {
throw NotEnoughMoney();
}
...
}
Always try to catch method outcome to context class field, for example:
//inside Behat context class method
try {
$this->outcome = $func();
}
catch(\Exception $ex) {
$this->outcome = $ex;
}
Now when expecting exception at next step just check if $this->outcome is instanceof desired exception with message/code.
I think the problem is in your implementation. Do you check if transfer is successful in "When transfers from his account" ? Do you need to check it ?
Failure test:
When <player> transfers <transfer> from his account
Then I should see error <error>
Successful step:
When <player> transfers <transfer> from his account
Then I should see "transfer successful"
Here's how I successfully did it in a project of mine where I had to repeat a few steps till the condition held true:
/**
* #Given /^I execute some conditions$/
*/
public function executeConditions()
{
$flag = 1;
do {
try {
<steps to be executed till the condition holds true>
$flag=1;
} catch (\Exception $ex) {
$flag = 0;
}
}while ($flag>0);
}