|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BackblazeB2; |
|
4
|
|
|
|
|
5
|
|
|
use BackblazeB2\Exceptions\B2Exception; |
|
6
|
|
|
use BackblazeB2\Exceptions\BadJsonException; |
|
7
|
|
|
use BackblazeB2\Exceptions\BadValueException; |
|
8
|
|
|
use BackblazeB2\Exceptions\BucketAlreadyExistsException; |
|
9
|
|
|
use BackblazeB2\Exceptions\BucketNotEmptyException; |
|
10
|
|
|
use BackblazeB2\Exceptions\FileNotPresentException; |
|
11
|
|
|
use BackblazeB2\Exceptions\NotFoundException; |
|
12
|
|
|
use BackblazeB2\Exceptions\UnauthorizedAccessException; |
|
13
|
|
|
use GuzzleHttp\Psr7\Response; |
|
14
|
|
|
|
|
15
|
|
|
class ErrorHandler |
|
16
|
|
|
{ |
|
17
|
|
|
protected static $mappings = [ |
|
18
|
|
|
'bad_json' => BadJsonException::class, |
|
19
|
|
|
'bad_value' => BadValueException::class, |
|
20
|
|
|
'duplicate_bucket_name' => BucketAlreadyExistsException::class, |
|
21
|
|
|
'not_found' => NotFoundException::class, |
|
22
|
|
|
'file_not_present' => FileNotPresentException::class, |
|
23
|
|
|
'cannot_delete_non_empty_bucket' => BucketNotEmptyException::class, |
|
24
|
|
|
'unauthorized' => UnauthorizedAccessException::class, |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param Response $response |
|
29
|
|
|
* |
|
30
|
|
|
* @throws B2Exception |
|
31
|
|
|
*/ |
|
32
|
7 |
|
public static function handleErrorResponse(Response $response) |
|
33
|
|
|
{ |
|
34
|
7 |
|
$responseJson = json_decode($response->getBody(), true); |
|
35
|
|
|
|
|
36
|
7 |
|
if (isset(self::$mappings[$responseJson['code']])) { |
|
37
|
7 |
|
$exceptionClass = self::$mappings[$responseJson['code']]; |
|
38
|
|
|
} else { |
|
39
|
|
|
// We don't have an exception mapped to this response error, throw generic exception |
|
40
|
|
|
$exceptionClass = B2Exception::class; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
7 |
|
throw new $exceptionClass('Received error from B2: '.$responseJson['message']); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|