Skip to content

Commit 0236a05

Browse files
committed
Read error information from header
1 parent 572d0f8 commit 0236a05

File tree

3 files changed

+135
-3
lines changed

3 files changed

+135
-3
lines changed

src/OSS/Core/OssException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function __construct($details)
1818
{
1919
if (is_array($details)) {
2020
$message = $details['code'] . ': ' . $details['message']
21-
. ' RequestId: ' . $details['request-id'];
21+
. ' RequestId: ' . $details['request-id'] . " EC:".$details['ec'];
2222
parent::__construct($message);
2323
$this->details = $details;
2424
} else {
@@ -51,4 +51,8 @@ public function getDetails()
5151
{
5252
return isset($this->details['body']) ? $this->details['body'] : '';
5353
}
54+
55+
public function getEc(){
56+
return isset($this->details['ec']) ? $this->details['ec'] : '';
57+
}
5458
}

src/OSS/Result/Result.php

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use OSS\Core\OssException;
66
use OSS\Http\ResponseCore;
77

8-
98
/**
109
* Class Result, The result class of The operation of the base class, different requests in dealing with the return of data have different logic,
1110
* The specific parsing logic postponed to subclass implementation
@@ -85,14 +84,27 @@ public function parseResponse()
8584
$requestId = strval($this->getRequestId());
8685
$code = $this->retrieveErrorCode($this->rawResponse->body);
8786
$message = $this->retrieveErrorMessage($this->rawResponse->body);
87+
$ec = $this->retrieveErrorEc($this->rawResponse->body);
88+
if (empty($code)){
89+
$code = $this->retrieveErrorFromHeader($this->rawResponse->header,'code');
90+
}
91+
if (empty($message)){
92+
$message = $this->retrieveErrorFromHeader($this->rawResponse->header,'msg');
93+
}
94+
95+
if (empty($ec)){
96+
$ec = $this->getOssEc();
97+
}
98+
8899
$body = $this->rawResponse->body;
89100

90101
$details = array(
91102
'status' => $httpStatus,
92103
'request-id' => $requestId,
93104
'code' => $code,
94105
'message' => $message,
95-
'body' => $body
106+
'body' => $body,
107+
'ec' => $ec,
96108
);
97109
throw new OssException($details);
98110
}
@@ -116,6 +128,28 @@ private function retrieveErrorMessage($body)
116128
return '';
117129
}
118130

131+
132+
/**
133+
* Get some msg from header
134+
* @param $header array
135+
* @param $type string code|msg|ec
136+
* @return string
137+
*/
138+
private function retrieveErrorFromHeader($header,$type){
139+
if (isset($header['x-oss-err'])){
140+
$content = base64_decode($header['x-oss-err'],true);
141+
switch ($type){
142+
case "code":
143+
return $this->retrieveErrorCode($content);
144+
case "msg":
145+
return $this->retrieveErrorMessage($content);
146+
}
147+
}else{
148+
return '';
149+
}
150+
151+
}
152+
119153
/**
120154
* Try to get the error Code from body
121155
*
@@ -134,6 +168,42 @@ private function retrieveErrorCode($body)
134168
return '';
135169
}
136170

171+
/**
172+
* Try to get the ec Code from body
173+
* @return mixed|string
174+
*/
175+
private function getOssEc()
176+
{
177+
178+
if (isset($this->rawResponse) &&
179+
isset($this->rawResponse->header) &&
180+
isset($this->rawResponse->header['x-oss-ec'])
181+
) {
182+
return $this->rawResponse->header['x-oss-ec'];
183+
} else {
184+
return '';
185+
}
186+
}
187+
188+
/**
189+
* Try to get the EC from body
190+
*
191+
* @param $body
192+
* @return string
193+
*/
194+
private function retrieveErrorEc($body)
195+
{
196+
if (empty($body) || false === strpos($body, '<?xml')) {
197+
return '';
198+
}
199+
$xml = simplexml_load_string($body);
200+
if (isset($xml->EC)) {
201+
return strval($xml->EC);
202+
}
203+
return '';
204+
}
205+
206+
137207
/**
138208
* Judging from the return http status code, [200-299] that is OK
139209
*

tests/OSS/Tests/HeaderResultTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace OSS\Tests;
44

5+
use OSS\Core\OssException;
6+
use OSS\Result\ExistResult;
57
use OSS\Result\HeaderResult;
68
use OSS\Http\ResponseCore;
79

@@ -20,4 +22,60 @@ public function testGetHeader()
2022
$data = $result->getData();
2123
$this->assertEquals($data['key'], 'value');
2224
}
25+
public function testGetHeader2()
26+
{
27+
$xml = '<?xml version="1.0" ?>
28+
<Error xmlns="http://doc.oss-cn-hangzhou.aliyuncs.com">
29+
<Code>AccessDenied</Code>
30+
<Message>***</Message>
31+
<RequestId>*******</RequestId>
32+
<HostId>oss-cn-hangzhou.aliyuncs.com</HostId>
33+
<EC>0003-00000016</EC>
34+
</Error>';
35+
$header = array(
36+
"x-oss-request-id"=>"636B68BA80DA8539399F2397",
37+
"x-oss-server-time"=>0,
38+
"x-oss-ec"=>"0003-00000016",
39+
"x-oss-err"=>base64_encode($xml),
40+
);
41+
$response = new ResponseCore($header, "", 403);
42+
43+
44+
try {
45+
$result = new HeaderResult($response);
46+
}catch (OssException $e){
47+
$this->assertEquals($e->getEc(),"0003-00000016");
48+
$this->assertEquals($e->getErrorMessage(),"***");
49+
$this->assertEquals($e->getErrorCode(),"AccessDenied");
50+
}
51+
}
52+
53+
54+
public function testIsExist()
55+
{
56+
$xml = '<?xml version="1.0" ?>
57+
<Error xmlns="http://doc.oss-cn-hangzhou.aliyuncs.com">
58+
<Code>NotSuchKey</Code>
59+
<Message>not exist</Message>
60+
<RequestId>11111111111111111111111</RequestId>
61+
<HostId>oss-cn-hangzhou.aliyuncs.com</HostId>
62+
<EC>0003-00000016</EC>
63+
</Error>';
64+
$header = array(
65+
"x-oss-request-id"=>"636B68BA80DA8539399F2397",
66+
"x-oss-server-time"=>0,
67+
"x-oss-ec"=>"0003-00000016",
68+
"x-oss-err"=>base64_encode($xml),
69+
);
70+
$response = new ResponseCore($header, "", 404);
71+
72+
73+
try {
74+
$result = new ExistResult($response);
75+
$this->assertTrue($result->isOK());
76+
$this->assertEquals($result->getData(), false);
77+
}catch (OssException $e){
78+
$this->assertTrue(false);
79+
}
80+
}
2381
}

0 commit comments

Comments
 (0)