From 21cbbdf4d27bf8b28d02c0f01bdabfa8818791b0 Mon Sep 17 00:00:00 2001 From: Roland Schilter Date: Wed, 1 Dec 2010 12:54:04 +0100 Subject: [PATCH] Add generic PHP exception matching for javax.jcr.* --- src/Jackalope/Transport/Davex/Request.php | 24 ++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Jackalope/Transport/Davex/Request.php b/src/Jackalope/Transport/Davex/Request.php index c369e93b..a4b2cce2 100644 --- a/src/Jackalope/Transport/Davex/Request.php +++ b/src/Jackalope/Transport/Davex/Request.php @@ -208,17 +208,31 @@ public function execute() $errClass = $err->getElementsByTagNameNS(Client::NS_DCR, 'class')->item(0)->textContent; $errMsg = $err->getElementsByTagNameNS(Client::NS_DCR, 'message')->item(0)->textContent; + $exceptionMsg = 'HTTP ' . $httpCode . ': ' . $errMsg; switch($errClass) { case 'javax.jcr.NoSuchWorkspaceException': - throw new \PHPCR\NoSuchWorkspaceException('HTTP '.$httpCode . ": $errMsg"); + throw new \PHPCR\NoSuchWorkspaceException($exceptionMsg); case 'javax.jcr.nodetype.NoSuchNodeTypeException': - throw new \PHPCR\NodeType\NoSuchNodeTypeException('HTTP '.$httpCode . ": $errMsg"); + throw new \PHPCR\NodeType\NoSuchNodeTypeException($exceptionMsg); case 'javax.jcr.ItemNotFoundException': - throw new \PHPCR\ItemNotFoundException('HTTP '.$httpCode . ": $errMsg"); + throw new \PHPCR\ItemNotFoundException($exceptionMsg); + case 'javax.jcr.nodetype.ConstraintViolationException': + throw new \PHPCR\NodeType\ConstraintViolationException($exceptionMsg); //TODO: map more errors here? default: - throw new \PHPCR\RepositoryException('HTTP '.$httpCode . ": $errMsg ($errClass)"); + + // try generic + $class = substr($errClass, strlen('javax.jcr.')); + $class = explode('.', $class); + array_walk($class, function(&$ns) { $ns = ucfirst(str_replace('nodetype', 'NodeType', $ns)); }); + $class = '\\PHPCR\\'.implode('\\', $class); + + if (class_exists($class)) { + throw new $class($exceptionMsg); + } else { + throw new \PHPCR\RepositoryException($exceptionMsg . " ($errClass)"); + } } } } @@ -274,4 +288,4 @@ public function executeJson() //TODO: are there error responses in json format? if so, handle them return $json; } -} \ No newline at end of file +}