deepl-python icon indicating copy to clipboard operation
deepl-python copied to clipboard

DeepLException

Open SamueLacombe opened this issue 3 years ago • 4 comments

Hello,

There is an example of DeepLException : except deepl.DeepLException as error: # Errors during upload raise a DeepLException print(error)

but

  1. I'm not sure how to check the error "code" returned (501, 404, etc)
  2. I noticed in the source code that there is supposed to be an argument "should_retry". Should I expect this argument to be always present in my error and just have to do : if error.should_retry: call again
  3. Having an example how to simulate an error returned by DeepL would be practical to debug.

Best regards, Samuel

SamueLacombe avatar Jul 19 '22 01:07 SamueLacombe

Hi @SamueLacombe, thanks for the questions.

  1. The HTTP status code is not included in the exception, because we didn't expect it to be needed. The HTTP status code is already checked by the library to choose the type of exception and message. Do you need it for your application?
  2. The should_retry parameter is used internally when deciding whether to retry requests. The library retries failed requests, by default up to 5 times.
  3. A few ways you could trigger errors: use an incorrect auth-key, use an invalid target or source language code, or try to translate an unsupported document type (e.g. image files).

Thanks for creating this issue; these things could be made more clear in the documentation.

daniel-jones-deepl avatar Jul 20 '22 14:07 daniel-jones-deepl

Hello,

I had to implement a "Exponential Backoff" with google automl api. I was trying to implement it with deepl aswell, if ever it fail for specific reason on the server side, but without the error code, I could retry on useless errors.

I believe it's possible to get bad gateway randomly like any other API?

Best regards, Samuel

SamueLacombe avatar Aug 02 '22 20:08 SamueLacombe

+1 to the above, just implementing some exception handling and I think it's very different based on the type of error. e.g. for 502 Service Unavailable (also 429, maybe 456..), I would just backoff and retry later.

For 404 Not Found or 400 Bad Request, I would raise it to my Sentry because likely I'm doing something wrong and retrying will just spam your servers.

arski avatar Aug 15 '22 12:08 arski

Hi Samuel, this library already retries failed requests with exponential backoff, so you might not need to implement it yourself. The function which decides whether to retry a request is http_client._should_retry(). We retry 429 and >=500 (excluding 503), so a 502 error would be retried.

I will look into exposing the HTTP error code in exceptions, and possibly the shouldRetry flag as well.

daniel-jones-deepl avatar Aug 31 '22 07:08 daniel-jones-deepl

Thanks Daniel! Any potential ETA on this? Cheers

arski avatar Sep 29 '22 12:09 arski

Hi again, sorry for the long delay. v1.12.0 adds properties should_retry and (Optional) http_status_code to DeepLException. Please let me know if there are any problems.

daniel-jones-deepl avatar Jan 09 '23 14:01 daniel-jones-deepl

thank you!

arski avatar Feb 27 '23 18:02 arski