On Mon, 2007-12-10 at 18:25 -0500, Steve Finkelstein wrote:
> Hi all,
>
> I'm having a brain freeze with some simple code that I wrote and now trying
> to refactor.
>
> I have a block of code that looks like this:
>
> public function backup()
> {
> $fname = "$this->dbName.sql.$this->zip";
>
> $this->cmd = "mysqldump -Q -u $this->dbUser -p$this->dbPass
> $this->dbName 2>&1 > {$this->dest}/{$this->dbName}.{$this->lastId}.sql";
>
>
>
> $res = shell_exec("$this->cmd");
> error_log("First res: ".$res);
>
> if(!$res) {
> $this->cmd = ($this->zip==="bz2") ?
> "bzip2
> {$this->dest}/{$this->dbName}.{$this->lastId}.sql 2>&1" :
> "gzip
> {$this->dest}/{$this->dbName}.{$this->lastId}.sql 2>&1";
>
> $res = shell_exec("$this->cmd");
> error_log("second error: ".$res);
> return !$res;
> }
>
> return FALSE;
> }
>
> Now instead of that FALSE, is there a way I can pass FALSE with a particular
> error message? This is because on the other end I have code that looks like
> this:
>
> if($mysqlDump->backup()) {
> $success = array('success' => '1');
> $sqlres = mysql_query($sql, $link) or
> die(json_message('error',mysql_error()));
> shell_exec('/usr/bin/touch /tmp/build_transfer');
> mysql_close($link);
> return '(' . json_encode($success) . ')';
> } else {
> $fail = array('fail' => $res);
> return '(' . json_encode($fail) . ')';
> }
>
> I'd ultimately like to be able to deliver a failure message from the return
> value...and properly catch that message so I can send it back in JSON format
> to the client browser to report what the error is.
>
> Think I should approach this with some try{..} catch code? Am I overlooking
> something really simple? :-)
You can use try/catch, or since your current semantics are to return
boolean only, you could check do the following:
<?php
if( ($status = backup()) !== true )
{
if( is_string( $status ) )
{
echo 'Failure: '.$status."\n";
}
}
?>
Cheers,
Rob.
--
...........................................................
SwarmBuy.com - http://www.swarmbuy.com
Leveraging the buying power of the masses!
...........................................................
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php