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 $serviceVersion = isset($data['service_version']) ? $data['service_version'] : '';
63 $message = "{$data['error_code']}: {$messages} (#{$data['correlation_id']})";
64 $message .= $serviceVersion ? " ServiceVersion: $serviceVersion" : '';
65
66 parent::__construct($message, $prev->getCode(), $prev);
67
68 $this->errorCode = $data['error_code'];
69 $this->messages = $data['error_messages'];
70 $this->correlationId = $data['correlation_id'];
71 $this->serviceVersion = $serviceVersion;
72 }
73
74 /**
75 * Gets the API error code for this exception.
76 *
77 * @return string
78 */
79 public function getErrorCode()
80 {
81 return $this->errorCode;
82 }
83
84 /**
85 * Gets the API error messages for this exception.
86 *
87 * @return array
88 */
89 public function getMessages()
90 {
91 return $this->messages;
92 }
93
94 /**
95 * Gets the API correlation ID for this exception.
96 *
97 * @return string
98 */
99 public function getCorrelationId()
100 {
101 return $this->correlationId;
102 }
103
104 /**
105 * Gets the API Service version for this exception.
106 *
107 * @return string
108 */
109 public function getServiceVersion()
110 {
111 return $this->serviceVersion;
112 }
113
114 /**
115 * Gets the HTTP response for this API error.
116 *
117 * @return ResponseInterface
118 */
119 public function getResponse()
120 {
121 return $this->getPrevious()->getResponse();
122 }
123 }
124