From b5fd624f6132595323b59c5516d399c7c169c211 Mon Sep 17 00:00:00 2001 From: konrad Date: Mon, 24 May 2010 16:30:10 +0000 Subject: [PATCH] converted transaction error reporting to exceptions instead of hard exit git-svn-id: https://silmor.de/svn/softmagic/pack/trunk@470 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- phpbase/autoload.php | 1 + phpbase/exception.php | 28 ++++++++++++++++++++++++++++ phpbase/transaction.php | 23 ++++++++--------------- woc/phpout.cpp | 4 ++-- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/phpbase/autoload.php b/phpbase/autoload.php index a7e52de..3498aee 100644 --- a/phpbase/autoload.php +++ b/phpbase/autoload.php @@ -17,6 +17,7 @@ $AUTOCLASS["WobTable"]= $d."/table.php"; $AUTOCLASS["WobSchemaBase"]=$d."/schema.php"; $AUTOCLASS["WobTransactionBase"]=$d."/transaction.php"; $AUTOCLASS["WobXmlException"]=$d."/exception.php"; +$AUTOCLASS["WobTransactionError"]=$d."/exception.php"; $AUTOCLASS["WObject"]=$d."/object.php"; //load the linguist dummies, since we use them quite often include_once($d."/tr.php"); diff --git a/phpbase/exception.php b/phpbase/exception.php index 732ac74..f41c026 100644 --- a/phpbase/exception.php +++ b/phpbase/exception.php @@ -13,4 +13,32 @@ class WobXmlException extends Exception {}; +/**general Wob Error - thrown whenever a transaction fails for any reason*/ +class WobTransactionError extends Exception +{ + protected $etype="_system"; + protected $estr=""; + + public function __construct($errorString,$errorType="_system") + { + parent::__construct($errorString); + $this->etype=$errorType; + $this->estr=$errorString; + } + + /**used by machine oriented transaction to print the proper XML representation of the error*/ + public function printXml() + { + header("X-WobResponse-Status: Error"); + print("etype)."\">".xq($this->estr)."\n"); + } + + /**returns the error type*/ + public function errorType(){return $this->etype;} + + /**returns the error string*/ + public function errorString(){return $this->estr;} + +}; + ?> \ No newline at end of file diff --git a/phpbase/transaction.php b/phpbase/transaction.php index da46df1..c3feaef 100644 --- a/phpbase/transaction.php +++ b/phpbase/transaction.php @@ -51,34 +51,29 @@ class WobTransactionBase { /**called if the transaction is not known. aborts the script.*/ static public function noSuchTransaction() { - header("X-WobResponse-Status: Error"); - print("".tr("Request is not known, Aborting.")."\n"); $this->abortTransaction(); - exit(); + throw new WobTransactionError(tr("Request is not known, Aborting."),"non-wob"); } /**called if authentication fails*/ public function notAuthenticated(){ - header("X-WobResponse-Status: Error"); - print("".tr("User is not authenticated or does not have permission to execute this request, Aborting.")."\n"); $this->abortTransaction(); - exit(); + throw new WobTransactionError(tr("User is not authenticated or does not have permission to execute this request, Aborting."),"auth"); } /**called if XML parsing fails*/ public function xmlParserError(){ - header("X-WobResponse-Status: Error"); - print("".tr("Error while parsing request XML, Aborting.")."\n"); $this->abortTransaction(); - exit(); + throw new WobTransactionError(tr("Error while parsing request XML, Aborting."),"xml"); } /**called for generic exception handling*/ public function handleException($ex){ - header("X-WobResponse-Status: Error"); - print("".xq($ex->getMessage())."\n"); + //throw on, if it already is internal + if(is_a($ex,"WobTransactionError")) + throw $ex; $this->abortTransaction(); - exit(); + throw new WobTransactionError($ex->getMessage(),"exception"); } /**called to abort a transactions flow @@ -86,10 +81,8 @@ class WobTransactionBase { \param $text the human readable text returned to the client */ public function abortWithError($text,$type="server"){ - header("X-WobResponse-Status: Error"); - print("".xq($text)."\n"); $this->abortTransaction(); - exit(); + throw new WobTransactionError($text,$type); } /**called internally if a transaction is not implemented*/ diff --git a/woc/phpout.cpp b/woc/phpout.cpp index 82e2deb..55801e8 100644 --- a/woc/phpout.cpp +++ b/woc/phpout.cpp @@ -22,8 +22,8 @@ static const QByteArray SCHEMASTART("class WobSchema extends WobSchemaBase\n{\nf static const QByteArray SCHEMAEND("}};\n"); static const QByteArray TRANSACTCLASS("class WobTransaction extends WobTransactionBase\n{\n"); -static const QByteArray TRANSACTSTART(" static public function handle(){switch(WobTransactionBase::getTransactionName()){\n"); -static const QByteArray TRANSACTEND("\tdefault:WobTransactionBase::noSuchTransaction();break;\n }}\n"); +static const QByteArray TRANSACTSTART(" static public function handle(){\n try{switch(WobTransactionBase::getTransactionName()){\n"); +static const QByteArray TRANSACTEND("\tdefault:WobTransactionBase::noSuchTransaction();break;\n }}catch(WobTransactionError $er){$er->printXml();}\n }\n"); WocPHPServerOut::WocPHPServerOut(const QDomElement&el) { -- 1.7.2.5