From 24c905c60c80fc5029ae8fbe9644a240e7bf84c6 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Thu, 30 Nov 2023 22:17:57 +0800 Subject: [PATCH] Retry failed Google requests if the message says to (#120) Check the returned error message for a particular string. This is of course fragile, but there doesn't look to be a better way, and hopefully this is a short term workaround until our thumbnail serving becomes more reliable. Bug: T332125 --- src/Engine/GoogleCloudVisionEngine.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Engine/GoogleCloudVisionEngine.php b/src/Engine/GoogleCloudVisionEngine.php index 7a3fdb39..34080a02 100644 --- a/src/Engine/GoogleCloudVisionEngine.php +++ b/src/Engine/GoogleCloudVisionEngine.php @@ -70,6 +70,18 @@ public function getResult( $imageUrlOrData = $image->hasData() ? $image->getData() : $image->getUrl(); $response = $this->imageAnnotator->textDetection( $imageUrlOrData, [ 'imageContext' => $imageContext ] ); + // Re-try with direct upload if the error returned is something similar to + // "The URL does not appear to be accessible by us. Please double check or download the content and pass it in." + // There doesn't seem to be a specific error code for this (it is usually 3, but that's also used for other + // things), so it seems like we have to check the actual message string. + if ( $response->getError() + && stripos( $response->getError()->getMessage(), 'download the content and pass it in' ) !== false + ) { + $image = $this->getImage( $imageUrl, $crop, self::DO_DOWNLOAD_IMAGE ); + $response = $this->imageAnnotator->textDetection( $image->getData(), [ 'imageContext' => $imageContext ] ); + } + + // Other errors, report to the user. if ( $response->getError() ) { throw new OcrException( 'google-error', [ $response->getError()->getMessage() ] ); }