1. <?php
  2. /**
  3. * Wraps an entry processing function to catch and report errors.
  4. *
  5. * @param callable $processor The function to process an entry.
  6. * @param mixed $entry The entry to process.
  7. * @return mixed The result of the processor function, or null on error.
  8. */
  9. function wrapEntryProcessor(callable $processor, $entry)
  10. {
  11. try {
  12. $result = $processor($entry); // Execute the processor function
  13. return $result;
  14. } catch (\Exception $e) {
  15. // Log the error (replace with your logging mechanism)
  16. error_log("Error processing entry: " . $e->getMessage());
  17. return null; // Or handle the error as needed
  18. }
  19. }
  20. // Example Usage (for testing)
  21. function processEntry($entry) {
  22. if($entry == 'error') {
  23. throw new \Exception("Simulated error");
  24. }
  25. return "Processed: " . $entry;
  26. }
  27. $entry = 'test';
  28. $result = wrapEntryProcessor('processEntry', $entry);
  29. if ($result !== null) {
  30. echo "Result: " . $result . "\n";
  31. } else {
  32. echo "Error processing entry.\n";
  33. }
  34. $entry = 'error';
  35. $result = wrapEntryProcessor('processEntry', $entry);
  36. if ($result !== null) {
  37. echo "Result: " . $result . "\n";
  38. } else {
  39. echo "Error processing entry.\n";
  40. }
  41. ?>

Add your comment