Triggering HTTP errors
Some times, we’ve gotta control some respones from our server, with PHP, specialy when we’re using Ajax, with which our response will not, necessarily, be show.
An example I can give is an experience I had while developin for theWebmind.org. Our ajax requisitions are mostly receiveing jSon answers. In other words, ANY error, either fatal or warning or notice, will brake the jSon code. We had to treat it on our client side, due to fix any javascript error while decoding the received jSon doce.
But, instead of that, a better option was to treat our errors inside our server side.
PHP allows us doing it by using set_error_handler(”func”);
You see details about this function here.
Well, forcing PHP to work with OUR method to treat all errors was the first step. BUT, I had a few problems while trying to specify this function to php on linux when my function was inside an object. I wanted to call a function I had in my framework, like this.
set_error_handler(”FrameWork::errHadler”);
Specifying a class method to this error hadler returned errors on Linux.
Ok, now, you can handle your errors, with your own function.
You can simply trigger it any time. For example, if the user has not submited a number in an “age” input, you want it to trigger an error.
if(!is_numeric($_POST['age']))
trigger_error(’age is not numeric’, E_USER_ERROR);
Ok, now, we all know that any error will end at our own function, let’s call it errHandler.
This function signature is this:
function errHandler($errno, $errstr, $errfile, $errline)
{}
$errno corresponds to the error code, which are in some constants, like:
E_USER_ERROR
E_USER_WARNING
E_USER_NOTICE
In other words, we may use a switch($errno)
switch ($errno)
{
case E_USER_ERROR: break;
case E_USER_WARNING: break;
case E_USER_NOTICE: break;
}
You may choose, for exapmle, to send an email to the code’s author when a fatal error occurres.
Let’s, in this example, change these options:
function errHandler($errno, $errstr, $errfile, $errline)
{
switch($errno)
{
case E_USER_ERROR:
{
$msg= “error!”.$errno.’\n’.$errstr.’\nFile: ‘.$errfile.’ at line ‘.$errline;
mail(’author@smtp.com’, ‘error message’, $msg);
$code= 499; // the response your browser will have
// these next 2 lines will output an error to the browser, this is the key
header(”HTTP/1.0 $code $msg”);
exit;
break;
}
case E_USER_WARNING:
{
echo “A warning message!“;
break;
}
case E_USER_NOTICE:
{
// will not do anything
break;
}
}
}
?>
Please, notice that all “notices” will be ignored, while we will write a message for each warning, and when an error ocurres, an email will be sent, and your browser will receive an http header error. This will force you ajax to go to a different status, not 200 // OK
With this, you can manage your errors in client side, to show a message, or take a different action.
You only have to be careful that, the header instruction, in PHP, MUST be returned before ANY charactere. For example:
echo ” “;
header(’any header instruction’);
will give you a warning, once you have already sent a space to your client. You have an error_handler set, then, it will be called, but, if your erro handler uses header there, then the default error message will be displayed.
After all, if you wan o force PHP to execute again, the default error_handler, use
restore_error_handler();
Below, you find a table with the HTTP response status codes:
1xx Informational
Request received, continuing process.
100 Continue
101 Switching Protocols
102 Processing (WebDAV) (RFC 2518 )
2xx Success
The action was successfully received, understood, and accepted.
200 OK
201 Created
202 Accepted
203 Non-Authoritative Information (since HTTP/1.1)
204 No Content
205 Reset Content
206 Partial Content
207 Multi-Status (WebDAV) (RFC 2518 )
3xx Redirection
The client must take additional action to complete the request.
300 Multiple Choices
301 Moved Permanently
302 Found
303 See Other (since HTTP/1.1)
304 Not Modified
305 Use Proxy (since HTTP/1.1)
306 Switch Proxy
307 Temporary Redirect (since HTTP/1.1)
4xx Client Error
The request contains bad syntax or cannot be fulfilled.
400 Bad Request
401 Unauthorized
402 Payment Required
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable
407 Proxy Authentication Required
408 Request Timeout
409 Conflict
410 Gone
411 Length Required
412 Precondition Failed
413 Request Entity Too Large
414 Request-URI Too Long
415 Unsupported Media Type
416 Requested Range Not Satisfiable
417 Expectation Failed
418 I’m a teapot
422 Unprocessable Entity (WebDAV) (RFC 4918 )
423 Locked (WebDAV) (RFC 4918 )
424 Failed Dependency (WebDAV) (RFC 4918 )
425 Unordered Collection (RFC 3648 )
426 Upgrade Required (RFC 2817 )
449 Retry With
5xx Server Error
The server failed to fulfil an apparently valid request.
500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported
506 Variant Also Negotiates (RFC 2295 )
507 Insufficient Storage (WebDAV) (RFC 4918 )
509 Bandwidth Limit Exceeded (Apache bw/limited extension)
510 Not Extended (RFC 2774 )
Comments
Leave a comment Trackback