Skip to content

Commit

Permalink
Retry failed Google requests if the message says to (#120)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
samwilson authored Nov 30, 2023
1 parent 7ba3b50 commit 24c905c
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Engine/GoogleCloudVisionEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ] );
}
Expand Down

0 comments on commit 24c905c

Please sign in to comment.