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 /**
23 * ConnectorException is used to represent a API error response.
24 */
25 class ConnectorException extends \RuntimeException
26 {
27 /**
28 * API response error code.
29 *
30 * @var string
31 */
32 protected $errorCode;
33
34 /**
35 * API response error messages.
36 *
37 * @var string[]
38 */
39 protected $messages;
40
41 /**
42 * API response error correlation ID.
43 *
44 * @var string
45 */
46 protected $correlationId;
47
48 /**
49 * Constructs a connector exception instance.
50 *
51 * @param array $data Error data
52 */
53 public function __construct(
54 array $data,
55 $code = 0
56 ) {
57 $data = self::setDefaultData($data);
58
59 $messages = implode(', ', $data['error_messages']);
60 $serviceVersion = isset($data['service_version']) ? $data['service_version'] : '';
61 $message = "{$data['error_code']}: {$messages} (#{$data['correlation_id']})";
62 $message .= $serviceVersion ? " ServiceVersion: $serviceVersion" : '';
63
64 parent::__construct($message, $code);
65
66 $this->errorCode = $data['error_code'];
67 $this->messages = $data['error_messages'];
68 $this->correlationId = $data['correlation_id'];
69 $this->serviceVersion = $serviceVersion;
70 }
71
72 /**
73 * Gets the API error code for this exception.
74 *
75 * @return string
76 */
77 public function getErrorCode()
78 {
79 return $this->errorCode;
80 }
81
82 /**
83 * Gets the API error messages for this exception.
84 *
85 * @return array
86 */
87 public function getMessages()
88 {
89 return $this->messages;
90 }
91
92 /**
93 * Gets the API correlation ID for this exception.
94 *
95 * @return string
96 */
97 public function getCorrelationId()
98 {
99 return $this->correlationId;
100 }
101
102 /**
103 * Gets the API Service version for this exception.
104 *
105 * @return string
106 */
107 public function getServiceVersion()
108 {
109 return $this->serviceVersion;
110 }
111
112 /**
113 * @deprecated Function is not longer used. Will always return null
114 * Gets the HTTP response for this API error.
115 *
116 * @return null
117 */
118 public function getResponse()
119 {
120 return null;
121 }
122
123 private static function setDefaultData($data)
124 {
125 $defaults = [
126 'error_code' => 'UNDEFINED',
127 'error_messages' => [],
128 'correlation_id' => 'UNDEFINED',
129 ];
130
131 foreach ($defaults as $field => $default) {
132 if (!isset($data[$field])) {
133 $data[$field] = $default;
134 }
135 }
136
137 // We need to have a special check for error_message and merge the message to error_messages
138 if (isset($data['error_message'])) {
139 array_push($data['error_messages'], $data['error_message']);
140 }
141
142 return $data;
143 }
144 }
145