Degrade more gracefully than "HTTP ERROR 500"?

If a search fails, I get a line in my HTTP server error log, and the user gets a white browser screen (“HTTP ERROR 500”). How can I make it so that search errors degrade more gracefully with a more useful custom error page?

Umm, well you must have some sort of ‘uncaught exception’.

A (uncought) PHP fatal error will often just terminate the script, and if there no output already sent to browser, the user justs gets a generic error message from their browser.

SHould check server ‘error’ log, most servers will log something.

But particully during debugging, might be useful to set error reporting, and ‘display_errors’ so you (the developer can see them) - can be unwise on a production server, but during testing is very handy!

if ($debug)
   ini_set('display_errors',1);

Set the debug flag somewhere so can toggle it.

This isn’t a PHP problem, though, it’s a Manticore problem.

When a search is malformed Manticore will die and not return gracefully to PHP.

A typical error might be:

PHP Fatal error: Uncaught Manticoresearch\\Exceptions\\ResponseException: "index myindex: query error: syntax error, unexpected '\\"', expecting TOK_INT near '\\"~\\"~'" in /...location.../vendor/manticoresoftware/manticoresearch-php/src/Manticoresearch/Transport/Http.php:127 ...

So my question pertains to how to handle this in production. I can’t necessarily predict and sanitize every input that users will provide. (Can I?)

Thats because it an ‘uncaught’ exception


try {
    // run your code here
    $results = $index->search('space team');
}
catch (exception $e) {
    print "Sorry, the search failed. Please try again, or contact us for support";
  exit;
}

… so could display a ‘nice’ message’ to users. You could choose to do soemthing more with the more ‘technical’ message from searchd. eg catch ‘query error’ and tell the user was an explicit issue with the quyery they entered.

if dont have anythign to ‘gracefully’ handle ‘fatal’ exceptions, then PHP will abort, so (hopefully!) avoid a cascading failure (eg have a script that fetchs something fomr one place, and inserts into anoter. if then fetch fails, typically want to handle, to avoid inserting ‘broken’ data into second. )

Manticore itself is not qualified to decide how to gracefully handle the exception, as requiremnts vary by app!

https://www.php.net/manual/en/language.exceptions.php

If an exception is thrown and its current function scope has no catch block, the exception will “bubble up” the call stack to the calling function until it finds a matching catch block. All finally blocks it encounters along the way will be executed. If the call stack is unwound all the way to the global scope without encountering a matching catch block, the program will terminate with a fatal error unless a global exception handler has been set.

1 Like

Thanks, perfect, that’s exactly what I was looking for.

You can tell I learned PHP back in version 4, before this feature was introduced.

I’ve never needed to use it before now.