I was looking in the wrong places. The error I was trying to hide was a socket() error, but that is actually a PHP error and not a BaseX Exception! Errors and exceptions are handled differently of course. So I had to resort to PHP error_reporting to temporarily disable the generated error when we can't connect to the server. Like this:
// error_reporting returns last state it was in $previousReportingLevel = error_reporting(0); $session = new Session($dbhost, $dbport, $dbuser, $dbpwd); error_reporting($previousReportingLevel);
You still have to wrap this in a try/catch block and ignore the catch though, because the BaseX script will also throw a 'Can't communicate with server' error. I'm not sure why this is the case. Considering PHP already returns false AND reports an error on socket_connect() fail it might be better to just return false when this error occurs on session creation. That way we can still throw our own Exception if needed, but more importantly we get a false return when the server cannot be connected to. Just a thought.
Kind regards
Bram
-----Oorspronkelijk bericht----- Van: Christian Grün [mailto:christian.gruen@gmail.com] Verzonden: donderdag 27 oktober 2016 10:32 Aan: Bram Vanroy bram.vanroy1@student.kuleuven.be CC: BaseX basex-talk@mailman.uni-konstanz.de Onderwerp: Re: [basex-talk] Check if server is running, silently
Hi Bram,
Unfortunately I cannot give you help on PHP (my knowledge about this language is around 20 years old now). If you check out our Java sources [1], you will see that our ping does basically the same what you do: We try to create a connection and return true if we succeed.
Regarding logging, I would assume that it’s your freedom to decide what you do in the catch branch. I am not sure where/when Apache comes into play in your setup?
Christian
[1] https://github.com/BaseXdb/basex/blob/25f3d67/basex-core/src/main/java/org/b...
On Thu, Oct 27, 2016 at 10:22 AM, Bram Vanroy bram.vanroy1@student.kuleuven.be wrote:
Hi all
To make clear to users which corpora are currently available and which aren’t, I’m checking to see if a BaseX server instance exists and then check if a specific database exists. Testing for the database is done with db:query (as I learned through this mailinglist), but I’m not sure how I can silently test for the server in PHP.
All BaseX examples use try-catch blocks, which makes sense to catch errors that go wrong, and do something with them. If I’m not mistaken these errors are automatically logged by Apache (maybe also by other webservices). This means that if I’m only checking the existence of a server with the code below (variables left out), I will always write an error to a log file. Obviously, that is not what I want because I know that the error ‘Cannot communicate with server’ is to be expected when the server does not exist!
try { $session = new Session($dbhost, $dbport, $dbuser, $dbpwd); // db:exists returns a string 'false', still need to convert
to bool
$corpusExists =
toBoolean($session->query("db:exists('$databaseName')")->execute());
$session->close(); if ($corpusExists) { $availabilityArray[] = $databaseName; } } catch (Exception $e) { // }
This script works perfectly fine, but I do not want the errors to be logged (for this case) to prevent my error log to be full of the same error. Is there another way to test for a BaseX instance only? For instance, a way to return false when the server does not exist, I could do something like:
$session = new Session($dbhost, $dbport, $dbuser, $dbpwd);
If ($session) {
// db:exists returns a string 'false', still need to convert
to bool
$corpusExists =
toBoolean($session->query("db:exists('$databaseName')")->execute());
$session->close(); if ($corpusExists) { $availabilityArray[] = $databaseName; }
}
Thank you in advance
Bram Vanroy