Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 2dd22eb

Browse files
check for DeepL HTTP error codes
1 parent d2436b9 commit 2dd22eb

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/DeepL.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ class DeepL
3434
*/
3535
const API_URL_DESTINATION_LANG = 'target_lang=%s';
3636

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+
3750
/**
3851
* Supported translation source languages
3952
*
@@ -81,7 +94,7 @@ class DeepL
8194
/**
8295
* DeepL constructor
8396
*
84-
* @param $authKey
97+
* @param $authKey string
8598
*/
8699
public function __construct($authKey)
87100
{
@@ -92,7 +105,7 @@ public function __construct($authKey)
92105
}
93106

94107
/**
95-
* DeepL Destructor
108+
* DeepL destructor
96109
*/
97110
public function __destruct()
98111
{
@@ -125,8 +138,8 @@ public function translate($text, $sourceLanguage = 'de', $destinationLanguage =
125138
/**
126139
* Check if the given languages are supported
127140
*
128-
* @param $sourceLanguage
129-
* @param $destinationLanguage
141+
* @param $sourceLanguage string
142+
* @param $destinationLanguage string
130143
*
131144
* @return boolean
132145
*
@@ -152,9 +165,9 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
152165
/**
153166
* Build the URL for the DeepL API request
154167
*
155-
* @param $text
156-
* @param $sourceLanguage
157-
* @param $destinationLanguage
168+
* @param $text string
169+
* @param $sourceLanguage string
170+
* @param $destinationLanguage string
158171
*
159172
* @return string
160173
*/
@@ -179,7 +192,7 @@ protected function buildUrl($text, $sourceLanguage, $destinationLanguage)
179192
/**
180193
* Make a request to the given URL
181194
*
182-
* @param $url
195+
* @param $url string
183196
*
184197
* @return array
185198
*
@@ -190,17 +203,15 @@ protected function request($url)
190203
curl_setopt($this->curl, CURLOPT_URL, $url);
191204
$response = curl_exec($this->curl);
192205

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);
195208

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);
201211
}
202-
203-
throw new DeepLException($errorString, $errorNumber);
212+
}
213+
else {
214+
throw new DeepLException('There was a cURL Request Error.');
204215
}
205216

206217
$translationsArray = json_decode($response, true);

0 commit comments

Comments
 (0)