0

I'm doing a small program which needs to read error meesage when errors occur.

For example, one operation triggered an error, and php log recorded it as:

PHP Fatal error:  Smarty error: [in /mnt/n2my_web/templates/ja_JP/mail/reservation_create.t.txt line 16]: syntax error: mismatched tag {/if}. (Smarty_Compiler.class.php, line 2338) in /mnt/n2my_web/lib/Smarty/Smarty.class.php on line 1092

I know by setting ini_set('display_errors', '1'); the error message can be printed. But I need to read it, in order to format it.

By which means can I achieve this? Any answer is appreciated. :)

2
  • Do you want to know about errors in general or the specific case of fatal errors, which are a whole different issue and cannot be treated like normal errors? Commented Dec 13, 2013 at 1:24
  • Hi Mike, actually errors in this project will always be smarty error, that is to say all the errors should be php fatal errors. Commented Dec 13, 2013 at 1:26

2 Answers 2

2

Use this: http://www.php.net/manual/en/function.error-get-last.php , to handling error, I recommend you to just use custom exception and error handler

http://www.php.net/manual/en/function.set-exception-handler.php.

http://www.php.net/manual/en/function.set-error-handler.php

Sign up to request clarification or add additional context in comments.

2 Comments

Worth noting that error_get_last() won't be any good for anything more severe than an E_WARN since execution halts at that point.
Hi egig, thanks for your recommendation. As for this question, as Sammitch said terminating point should be considered. I think it's my express causes confusions, thank you.
0

Really the only way to can access any error information around a fatal error is to use register_shutdown_function() to try to catch these errors and work with them before the script is terminated. This is not 100% reliable.

register_shutdown_function(function() {
   $last_error = error_get_last();
   if(!is_null($last_error) && $last_error['type'] === E_ERROR) {
       /*
       Do something with $last_error info.
       $last_error will contain array with keys as follows:
       ['type']['message']['file']['line']
       */
   }
});

2 Comments

Hi Mike, my script terminated at calling a smarty function. So I placed register_shutdown_function() just 1 line before calling smarty function it seems doesn't work. Then I put it at the beginning of my own function, also doesn't work. Where to register should be a question?
Mike, your answer is right, but I guess unless ini_set('display_errors', '1'); is set, or error message will not be returned. I'm not sure, in my practice if the display_errors is not set I cannot get the message from error_get_last();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.