diff --git a/src/OSS/Core/OssException.php b/src/OSS/Core/OssException.php index 2320c9e..4b5993a 100644 --- a/src/OSS/Core/OssException.php +++ b/src/OSS/Core/OssException.php @@ -18,7 +18,7 @@ function __construct($details) { if (is_array($details)) { $message = $details['code'] . ': ' . $details['message'] - . ' RequestId: ' . $details['request-id']; + . ' RequestId: ' . $details['request-id'] . " EC:".$details['ec']; parent::__construct($message); $this->details = $details; } else { @@ -51,4 +51,8 @@ public function getDetails() { return isset($this->details['body']) ? $this->details['body'] : ''; } + + public function getEc(){ + return isset($this->details['ec']) ? $this->details['ec'] : ''; + } } diff --git a/src/OSS/Result/Result.php b/src/OSS/Result/Result.php index e5d83d3..4c6416a 100644 --- a/src/OSS/Result/Result.php +++ b/src/OSS/Result/Result.php @@ -5,7 +5,6 @@ use OSS\Core\OssException; use OSS\Http\ResponseCore; - /** * Class Result, The result class of The operation of the base class, different requests in dealing with the return of data have different logic, * The specific parsing logic postponed to subclass implementation @@ -85,6 +84,14 @@ public function parseResponse() $requestId = strval($this->getRequestId()); $code = $this->retrieveErrorCode($this->rawResponse->body); $message = $this->retrieveErrorMessage($this->rawResponse->body); + $ec = $this->getHeaderEc(); + if (empty($code)){ + $code = $this->retrieveErrorFromHeader($this->rawResponse->header,'code'); + } + if (empty($message)){ + $message = $this->retrieveErrorFromHeader($this->rawResponse->header,'msg'); + } + $body = $this->rawResponse->body; $details = array( @@ -92,7 +99,8 @@ public function parseResponse() 'request-id' => $requestId, 'code' => $code, 'message' => $message, - 'body' => $body + 'body' => $body, + 'ec' => $ec, ); throw new OssException($details); } @@ -116,6 +124,28 @@ private function retrieveErrorMessage($body) return ''; } + + /** + * Get some msg from header + * @param $header array + * @param $type string code|msg|ec + * @return string + */ + private function retrieveErrorFromHeader($header,$type){ + if (isset($header['x-oss-err'])){ + $content = base64_decode($header['x-oss-err'],true); + switch ($type){ + case "code": + return $this->retrieveErrorCode($content); + case "msg": + return $this->retrieveErrorMessage($content); + } + }else{ + return ''; + } + + } + /** * Try to get the error Code from body * @@ -134,6 +164,23 @@ private function retrieveErrorCode($body) return ''; } + /** + * Try to get the ec Code from header + * @return mixed|string + */ + private function getHeaderEc() + { + + if (isset($this->rawResponse) && + isset($this->rawResponse->header) && + isset($this->rawResponse->header['x-oss-ec']) + ) { + return $this->rawResponse->header['x-oss-ec']; + } else { + return ''; + } + } + /** * Judging from the return http status code, [200-299] that is OK * diff --git a/tests/OSS/Tests/HeaderResultTest.php b/tests/OSS/Tests/HeaderResultTest.php index 9354422..7d20b6a 100644 --- a/tests/OSS/Tests/HeaderResultTest.php +++ b/tests/OSS/Tests/HeaderResultTest.php @@ -2,6 +2,8 @@ namespace OSS\Tests; +use OSS\Core\OssException; +use OSS\Result\ExistResult; use OSS\Result\HeaderResult; use OSS\Http\ResponseCore; @@ -20,4 +22,60 @@ public function testGetHeader() $data = $result->getData(); $this->assertEquals($data['key'], 'value'); } + public function testGetHeader2() + { + $xml = ' + + AccessDenied + *** + ******* + oss-cn-hangzhou.aliyuncs.com + 0003-00000016 +'; + $header = array( + "x-oss-request-id"=>"636B68BA80DA8539399F2397", + "x-oss-server-time"=>0, + "x-oss-ec"=>"0003-00000016", + "x-oss-err"=>base64_encode($xml), + ); + $response = new ResponseCore($header, "", 403); + + + try { + $result = new HeaderResult($response); + }catch (OssException $e){ + $this->assertEquals($e->getEc(),"0003-00000016"); + $this->assertEquals($e->getErrorMessage(),"***"); + $this->assertEquals($e->getErrorCode(),"AccessDenied"); + } + } + + + public function testIsExist() + { + $xml = ' + + NotSuchKey + not exist + 11111111111111111111111 + oss-cn-hangzhou.aliyuncs.com + 0003-00000016 +'; + $header = array( + "x-oss-request-id"=>"636B68BA80DA8539399F2397", + "x-oss-server-time"=>0, + "x-oss-ec"=>"0003-00000016", + "x-oss-err"=>base64_encode($xml), + ); + $response = new ResponseCore($header, "", 404); + + + try { + $result = new ExistResult($response); + $this->assertTrue($result->isOK()); + $this->assertEquals($result->getData(), false); + }catch (OssException $e){ + $this->assertTrue(false); + } + } }