@@ -34,6 +34,19 @@ class DeepL
34
34
*/
35
35
const API_URL_DESTINATION_LANG = 'target_lang=%s ' ;
36
36
37
+ /**
38
+ * DeepL HTTP error codes
39
+ *
40
+ * @var array
41
+ */
42
+ protected $ errorCodes = array (
43
+ 400 => 'Wrong request, please check error message and your parameters. ' ,
44
+ 403 => 'Authorization failed. Please supply a valid auth_key parameter. ' ,
45
+ 413 => 'Request Entity Too Large. The request size exceeds the current limit. ' ,
46
+ 429 => 'Too many requests. Please wait and send your request once again. ' ,
47
+ 456 => 'Quota exceeded. The character limit has been reached. '
48
+ );
49
+
37
50
/**
38
51
* Supported translation source languages
39
52
*
@@ -81,7 +94,7 @@ class DeepL
81
94
/**
82
95
* DeepL constructor
83
96
*
84
- * @param $authKey
97
+ * @param $authKey string
85
98
*/
86
99
public function __construct ($ authKey )
87
100
{
@@ -92,7 +105,7 @@ public function __construct($authKey)
92
105
}
93
106
94
107
/**
95
- * DeepL Destructor
108
+ * DeepL destructor
96
109
*/
97
110
public function __destruct ()
98
111
{
@@ -125,8 +138,8 @@ public function translate($text, $sourceLanguage = 'de', $destinationLanguage =
125
138
/**
126
139
* Check if the given languages are supported
127
140
*
128
- * @param $sourceLanguage
129
- * @param $destinationLanguage
141
+ * @param $sourceLanguage string
142
+ * @param $destinationLanguage string
130
143
*
131
144
* @return boolean
132
145
*
@@ -152,9 +165,9 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
152
165
/**
153
166
* Build the URL for the DeepL API request
154
167
*
155
- * @param $text
156
- * @param $sourceLanguage
157
- * @param $destinationLanguage
168
+ * @param $text string
169
+ * @param $sourceLanguage string
170
+ * @param $destinationLanguage string
158
171
*
159
172
* @return string
160
173
*/
@@ -179,7 +192,7 @@ protected function buildUrl($text, $sourceLanguage, $destinationLanguage)
179
192
/**
180
193
* Make a request to the given URL
181
194
*
182
- * @param $url
195
+ * @param $url string
183
196
*
184
197
* @return array
185
198
*
@@ -190,17 +203,15 @@ protected function request($url)
190
203
curl_setopt ($ this ->curl , CURLOPT_URL , $ url );
191
204
$ response = curl_exec ($ this ->curl );
192
205
193
- // if there was a curl error
194
- if ( $ response == false ) {
206
+ if (! curl_errno ( $ this -> curl )) {
207
+ $ httpCode = curl_getinfo ( $ this -> curl , CURLINFO_HTTP_CODE );
195
208
196
- $ errorString = curl_error ($ this ->curl );
197
- $ errorNumber = curl_errno ($ this ->curl );
198
-
199
- if (!$ errorString ) {
200
- $ errorString = 'There was a cURL Response Error - please check your DeepL Auth Key. ' ;
209
+ if ($ httpCode != 200 && array_key_exists ($ httpCode , $ this ->errorCodes )) {
210
+ throw new DeepLException ($ this ->errorCodes [$ httpCode ], $ httpCode );
201
211
}
202
-
203
- throw new DeepLException ($ errorString , $ errorNumber );
212
+ }
213
+ else {
214
+ throw new DeepLException ('There was a cURL Request Error. ' );
204
215
}
205
216
206
217
$ translationsArray = json_decode ($ response , true );
0 commit comments