1 <?php
2 /**
3 * Copyright 2014 Klarna AB
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * File containing the ConnectorException class.
18 */
19
20 namespace Klarna\Rest\Transport\Exception;
21
22 use GuzzleHttp\Exception\RequestException;
23 use GuzzleHttp\Message\ResponseInterface;
24
25 /**
26 * ConnectorException is used to represent a API error response.
27 */
28 class ConnectorException extends \RuntimeException
29 {
30 /**
31 * API response error code.
32 *
33 * @var string
34 */
35 protected $errorCode;
36
37 /**
38 * API response error messages.
39 *
40 * @var string[]
41 */
42 protected $messages;
43
44 /**
45 * API response error correlation ID.
46 *
47 * @var string
48 */
49 protected $correlationId;
50
51 /**
52 * Constructs a connector exception instance.
53 *
54 * @param array $data Error data
55 * @param RequestException $prev Previous exception
56 */
57 public function __construct(
58 array $data,
59 RequestException $prev
60 ) {
61 $messages = implode(', ', $data['error_messages']);
62 $message = "{$data['error_code']}: {$messages} (#{$data['correlation_id']})";
63
64 parent::__construct($message, $prev->getCode(), $prev);
65
66 $this->errorCode = $data['error_code'];
67 $this->messages = $data['error_messages'];
68 $this->correlationId = $data['correlation_id'];
69 }
70
71 /**
72 * Gets the API error code for this exception.
73 *
74 * @return string
75 */
76 public function getErrorCode()
77 {
78 return $this->errorCode;
79 }
80
81 /**
82 * Gets the API error messages for this exception.
83 *
84 * @return array
85 */
86 public function getMessages()
87 {
88 return $this->messages;
89 }
90
91 /**
92 * Gets the API correlation ID for this exception.
93 *
94 * @return string
95 */
96 public function getCorrelationId()
97 {
98 return $this->correlationId;
99 }
100
101 /**
102 * Gets the HTTP response for this API error.
103 *
104 * @return ResponseInterface
105 */
106 public function getResponse()
107 {
108 return $this->getPrevious()->getResponse();
109 }
110 }
111