Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/OSS/Core/OssException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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'] : '';
}
}
51 changes: 49 additions & 2 deletions src/OSS/Result/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,14 +84,23 @@ 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(
'status' => $httpStatus,
'request-id' => $requestId,
'code' => $code,
'message' => $message,
'body' => $body
'body' => $body,
'ec' => $ec,
);
throw new OssException($details);
}
Expand All @@ -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
*
Expand All @@ -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
*
Expand Down
58 changes: 58 additions & 0 deletions tests/OSS/Tests/HeaderResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace OSS\Tests;

use OSS\Core\OssException;
use OSS\Result\ExistResult;
use OSS\Result\HeaderResult;
use OSS\Http\ResponseCore;

Expand All @@ -20,4 +22,60 @@ public function testGetHeader()
$data = $result->getData();
$this->assertEquals($data['key'], 'value');
}
public function testGetHeader2()
{
$xml = '<?xml version="1.0" ?>
<Error xmlns="http://doc.oss-cn-hangzhou.aliyuncs.com">
<Code>AccessDenied</Code>
<Message>***</Message>
<RequestId>*******</RequestId>
<HostId>oss-cn-hangzhou.aliyuncs.com</HostId>
<EC>0003-00000016</EC>
</Error>';
$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 = '<?xml version="1.0" ?>
<Error xmlns="http://doc.oss-cn-hangzhou.aliyuncs.com">
<Code>NotSuchKey</Code>
<Message>not exist</Message>
<RequestId>11111111111111111111111</RequestId>
<HostId>oss-cn-hangzhou.aliyuncs.com</HostId>
<EC>0003-00000016</EC>
</Error>';
$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);
}
}
}