Php change fatal error format - php

I would like to try to change the formatting of fatal error but I can only modify the values of message, file and line.
I would expect to be able to change the css and the html of the message.
Thanks in advance.

Fatal errors in PHP are catchable, so you can make your own exception handler or even use a third party package like https://github.com/filp/whoops

You can catch fatal errors in PHP. Refer to this answer here:
https://stackoverflow.com/a/4410769/9051466
After determining if its a fatal error, you can just create a custom page, redirect the user to that custom page, then display what error message you want the user to see.

Related

Fatal error: Call to undefined method MarketplaceWebService_Model_GetReportListByNextTokenResult::getReportInfo()

I get this error in MWS API PHP Library when I try to get report list by next token:
Fatal error: Call to undefined method
MarketplaceWebService_Model_GetReportListByNextTokenResult::getReportInfo()
The weird thing is, in the code sample, that is the right function. And also I have checked if the report info exists by using:
$response->isSetGetReportListByNextTokenResult()
What should I do to get the report info?
When I open up the library, it turns out their code sample for this API is wrong. The sample uses getReportInfo() when it should be getReportInfoList().

How to set custom error in php?

I am working on php online testing IDE.
I want to display error like http://phpfiddle.org/
Example
Code
Result
-----------------------------------------------------------
I want errors without the filepath.
I am trying with set_error_handler() but it is not working on parse error and fatal error.
You can use register_shutdown_function(), then within the called function, you may use debug_backtrace() for tracing, function, line, code & etc.

change php error output to custom message with link

How do I change the error output of a php error? For example if its a syntax error, or a server side time out, i want to echo a message that allows the user to refresh the page.
Heres the code I want to add my error message to:
$XML->registerXPathNamespace('tree','www.tree.com'); <--occasionally errors here, so I want to output my own error message.
Use the function set_error_handler to define a custom function to be called when there is an error. You can then decide to do whatever you want within that function with the error.
If you only want it for a specific duration, you can restore it afterwards with restore_error_handler.
set_error_handler('yourHandler');
...
$XML->registerXPathNamespace('tree','www.tree.com');
...
restore_error_handler();
function yourHandler(int $errno , string $errstr) {
//show link to refresh page, whatever. full signature can be found on PHP manual page
}
Well, quick check in php manual for registerXPathNamespace shows that it returns TURE or FALSE. Which is perfect for my solution.
if (!#$XML->registerXPathNamespace('tree','www.tree.com')) {
echo '<b>ERROR:</b> Could not register xml path. Please reload the page!';
}
Not that, if it returns FALSE, then the message will be displayed. And the # in front of $XML will disable the original errors that, that action may cause.

How do I incorporate the line number of php source code in a custom error message?

I want to do my proper error generator when i'm programming (HTML + PHP).
How can i take the line, when i have an error, and put in a variable?
Example :
echo " Error # 03: variable undefined line #".$line." ";
Thanks.
the variables you'd be looking for are:
__LINE__
__FILE__
__FUNCTION__
__CLASS__
there is a predefined constant, __LINE__ that contains the line where it was actually called.
However, I guess that trigger_error() function perfectly fits "error generator" term, thus being exactly what you're looking for.
It will not only show you a line and a file and a timestamp, but also will follow general behavior of PHP error reporting settings, which is very important - you should never echo errors implicitly but rather put it into standard error stream
for the custom error handler there is also a debug_backtrace() function.
Assuming you are referring to PHP compile time errors and warnings, the line number is automatically displayed. Since these messages are generated at compile time (and as such can cause the script not to execute fully) I would recommend using the default messages rather than using a custom solution.
If PHP is not displaying the error messages, use the following code to display all of the PHP error messages and warnings on a page:
error_reporting(E_ALL);

exception not being caught

I have the following PHP code on a web page:
$wsdl_url = "someURL?wsdl";
try {
$client = new SoapClient($wsdl_url, array('login' => 'mylogin','password' => 'mypassword'));
$client->myWebMethod(); // <-- problem call
} catch (Exception $e) {
echo "none";
}
It's a basic call to a web service. The problem is that when an error is thrown on the line $client->myWebMethod(), echo "none" is not printed. In fact, nothing in the catch block runs. Hence, I don't think the exception is being caught.
A fatal error is displayed on the web page.
Question: Any ideas on why this is happening? I expected all exceptions to be caught and handled with this code. But what I'm getting is that the fatal error is being displayed on the page. Maybe web services are handled differently?
EDIT: the error is that it's missing a bunch of required parameters. if I add the parameters the call works fine. I am purposely omitting the parameters to get the error, so i would know how to handle it.
The error is something like: Fatal error: SOAP-ERROR: Object hasn't 'myparameter1'
Thanks in advance.
I got the same exact problem this morning while running a custom module for Drupal which required an external SOAP Web Service. To be honest, I'm not quite sure how I solved the problem.
Turns out it was all about clearing my Web Server's cache that had to do with the respective WSDL. In your tmp/ folder you will find various files named wsdl-yourservice-etc. Delete them and it should be OK. If not, the problem lies in the code and more specifically in the sequence and syntax of the arguments passed to the WSDL.
I hope I helped.
Unfortunately, this is not a catchable error.
However, you can check if the soap extension is loaded before trying to instantiate it by calling get_loaded_extensions with something along the lines of:
if (in_array('soap', get_loaded_extensions())) {
// it's loaded!
}

Categories