Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit 179eec3

Browse files
committed
Added offset fields to FacebookResumableUploadException. Added test for resumable upload.
1 parent dd8f1a7 commit 179eec3

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

src/Facebook/Exceptions/FacebookResponseException.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,22 @@ public static function create(FacebookResponse $response)
9090
// Video upload resumable error
9191
case 1363030:
9292
case 1363019:
93-
case 1363037:
9493
case 1363033:
9594
case 1363021:
9695
case 1363041:
9796
return new static($response, new FacebookResumableUploadException($message, $code));
97+
case 1363037:
98+
$previousException = new FacebookResumableUploadException($message, $code);
99+
100+
$startOffset = isset($data['error']['error_data']['start_offset']) ?
101+
$data['error']['error_data']['start_offset'] : null;
102+
$previousException->setStartOffset($startOffset);
103+
104+
$endOffset = isset($data['error']['error_data']['end_offset']) ?
105+
$data['error']['error_data']['end_offset'] : null;
106+
$previousException->setEndOffset($endOffset);
107+
108+
return new static($response, $previousException);
98109
}
99110
}
100111

src/Facebook/Exceptions/FacebookResumableUploadException.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,39 @@
3030
*/
3131
class FacebookResumableUploadException extends FacebookSDKException
3232
{
33+
protected $startOffset;
34+
35+
protected $endOffset;
36+
37+
/**
38+
* @return int
39+
*/
40+
public function getStartOffset()
41+
{
42+
return $this->startOffset;
43+
}
44+
45+
/**
46+
* @param int $startOffset
47+
*/
48+
public function setStartOffset($startOffset)
49+
{
50+
$this->startOffset = $startOffset;
51+
}
52+
53+
/**
54+
* @return int
55+
*/
56+
public function getEndOffset()
57+
{
58+
return $this->endOffset;
59+
}
60+
61+
/**
62+
* @param int $endOffset
63+
*/
64+
public function setEndOffset($endOffset)
65+
{
66+
$this->endOffset = $endOffset;
67+
}
3368
}

src/Facebook/FileUpload/FacebookResumableUploader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ public function transfer($endpoint, FacebookTransferChunk $chunk, $allowToThrow
121121
throw $e;
122122
}
123123

124-
if ($e->getSubErrorCode() === 1363037) {
124+
if (!is_null($preException->getStartOffset()) && !is_null($preException->getEndOffset())) {
125125
return new FacebookTransferChunk(
126126
$chunk->getFile(),
127127
$chunk->getUploadSessionId(),
128128
$chunk->getVideoId(),
129-
$e->getResponseData()['error']['error_data']['start_offset'],
130-
$e->getResponseData()['error']['error_data']['end_offset']
129+
$preException->getStartOffset(),
130+
$preException->getEndOffset()
131131
);
132132
}
133133

tests/FileUpload/FacebookResumableUploaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,14 @@ public function testFailedResumableTransferWillNotThrowAndReturnSameChunk()
9898
$newChunk = $uploader->transfer('/me/videos', $chunk);
9999
$this->assertSame($newChunk, $chunk);
100100
}
101+
102+
public function testFailedResumableTransferWillNotThrowAndReturnNewChunk()
103+
{
104+
$this->graphApi->failOnTransferAndUploadNewChunk();
105+
$uploader = new FacebookResumableUploader($this->fbApp, $this->client, 'access_token', 'v2.4');
106+
107+
$chunk = new FacebookTransferChunk($this->file, '1', '2', '3', '4');
108+
$newChunk = $uploader->transfer('/me/videos', $chunk);
109+
$this->assertEquals('40', $newChunk->getStartOffset());
110+
}
101111
}

tests/Fixtures/FakeGraphApiForResumableUpload.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public function failOnTransfer()
4242
$this->respondWith = 'FAIL_ON_TRANSFER';
4343
}
4444

45+
public function failOnTransferAndUploadNewChunk()
46+
{
47+
$this->respondWith = 'FAIL_ON_TRANSFER_AND_UPLOAD_NEW_CHUNK';
48+
}
49+
4550
public function send($url, $method, $body, array $headers, $timeOut)
4651
{
4752
// Could be start, transfer or finish
@@ -81,6 +86,15 @@ private function respondTransfer()
8186
);
8287
}
8388

89+
if ($this->respondWith == 'FAIL_ON_TRANSFER_AND_UPLOAD_NEW_CHUNK') {
90+
return new GraphRawResponse(
91+
"HTTP/1.1 500 OK\r\nFoo: Bar",
92+
'{"error":{"message":"There was a problem uploading your video. Please try uploading it again.",' .
93+
'"type":"OAuthException","code":6001,"error_subcode":1363037,' .
94+
'"error_data":{"start_offset":40,"end_offset":50}}}'
95+
);
96+
}
97+
8498
switch ($this->transferCount) {
8599
case 0:
86100
$data = ['start_offset' => 20, 'end_offset' => 40];

0 commit comments

Comments
 (0)