~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to src/OpenStack/Common/Transport/Exception/RequestException.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/*
 
4
 * (c) Copyright 2014 Rackspace US, Inc.
 
5
 *
 
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 
7
 * not use this file except in compliance with the License. You may obtain
 
8
 * a copy of the License at
 
9
 *
 
10
 *      http://www.apache.org/licenses/LICENSE-2.0
 
11
 *
 
12
 * Unless required by applicable law or agreed to in writing, software
 
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
15
 * License for the specific language governing permissions and limitations
 
16
 * under the License.
 
17
 */
 
18
 
 
19
namespace OpenStack\Common\Transport\Exception;
 
20
 
 
21
use OpenStack\Common\Exception;
 
22
use OpenStack\Common\Transport\RequestInterface;
 
23
use OpenStack\Common\Transport\ResponseInterface;
 
24
 
 
25
/**
 
26
 * Base exception that is thrown for requests that result in a HTTP error.
 
27
 */
 
28
class RequestException extends Exception
 
29
{
 
30
    /** @var \OpenStack\Common\Transport\RequestInterface */
 
31
    protected $request;
 
32
 
 
33
    /** @var \OpenStack\Common\Transport\ResponseInterface */
 
34
    protected $response;
 
35
 
 
36
    /**
 
37
     * Construct this exception like any other, but also inject Request and
 
38
     * Response objects in case the user needs them for debugging.
 
39
     *
 
40
     * @param string                                        $errorMessage Human-readable explanation of error
 
41
     * @param \OpenStack\Common\Transport\RequestInterface  $request      The failed request
 
42
     * @param \OpenStack\Common\Transport\ResponseInterface $response     The server's response
 
43
     */
 
44
    public function __construct($errorMessage, RequestInterface $request, ResponseInterface $response)
 
45
    {
 
46
        parent::__construct($errorMessage, $response->getStatusCode());
 
47
 
 
48
        $this->request  = $request;
 
49
        $this->response = $response;
 
50
    }
 
51
 
 
52
    /**
 
53
     * Factory method that creates an appropriate Exception object based on the
 
54
     * Response's status code. The message is constructed here also.
 
55
     *
 
56
     * @param \OpenStack\Common\Transport\RequestInterface  $request  The failed request
 
57
     * @param \OpenStack\Common\Transport\ResponseInterface $response The API's response
 
58
     * @return self
 
59
     */
 
60
    public static function create(RequestInterface $request, ResponseInterface $response)
 
61
    {
 
62
        $label = 'A HTTP error occurred';
 
63
 
 
64
        $status = $response->getStatusCode();
 
65
 
 
66
        $exceptions = [
 
67
            401 => 'UnauthorizedException',
 
68
            403 => 'ForbiddenException',
 
69
            404 => 'ResourceNotFoundException',
 
70
            405 => 'MethodNotAllowedException',
 
71
            409 => 'ConflictException',
 
72
            411 => 'LengthRequiredException',
 
73
            422 => 'UnprocessableEntityException',
 
74
            500 => 'ServerException'
 
75
        ];
 
76
 
 
77
        $message = sprintf(
 
78
            "%s\n[Status] %s (%s)\n[URL] %s\n[Message] %s\n", $label,
 
79
            (string) $request->getUrl(),
 
80
            $status, $response->getReasonPhrase(),
 
81
            (string) $response->getBody()
 
82
        );
 
83
 
 
84
        // Find custom exception class or use default
 
85
        $exceptionClass = isset($exceptions[$status])
 
86
            ? sprintf("%s\\%s", __NAMESPACE__, $exceptions[$status])
 
87
            : __CLASS__;
 
88
 
 
89
        return new $exceptionClass($message, $request, $response);
 
90
    }
 
91
 
 
92
    /**
 
93
     * Returns the server response.
 
94
     *
 
95
     * @return \OpenStack\Common\Transport\ResponseInterface
 
96
     */
 
97
    public function getResponse()
 
98
    {
 
99
        return $this->response;
 
100
    }
 
101
 
 
102
    /**
 
103
     * Returns the request that caused error.
 
104
     *
 
105
     * @return \OpenStack\Common\Transport\RequestInterface
 
106
     */
 
107
    public function getRequest()
 
108
    {
 
109
        return $this->request;
 
110
    }
 
111
 
 
b'\\ No newline at end of file'