M
M
Mikhail Konyukhov2012-10-31 19:04:30
PHP
Mikhail Konyukhov, 2012-10-31 19:04:30

How to catch an error in the root script and not fall into Error?

There is a script that runs in cron. It fetches data from the base (list), iterates through the list with foreach and performs an operation on each element of the list. When performing an operation on one of the elements, an error may occur - accordingly, the script crashes and does not complete to the end. Pseudocode:

$all = getData();
foreach ($all as $one){
   $one->doSomething(); //вот тут оно может упасть и не продолжить скрипт.
}


The bottom line - I want to intercept errors: Error, Warning, Notice, Fatal Error, unsubscribe to the log and continue working on the remaining elements of the list.
How can this be implemented?

PS It is errors, not exceptions!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Konyukhov, 2012-10-31
@piromanlynx

So far, he himself came up with a stupid way to fork the process:

foreach ($all as $one) {
    $pid = pcntl_fork();
    if ($pid == -1) {
       die('could not fork');
    } else if ($pid) {
      pcntl_wait($status); //Protect against Zombie children
      continue;
    } else {
      $one->doSomething(); 
      exit(0);
    }
}

A
ArtEx, 2012-10-31
@ArtEx

If I understand the task correctly, you can use set_error_handler('log_error'), where `log_error` is a function that will write your errors to the log file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question