Skip to content

Commit

Permalink
Provide a more detailed reason when unable to verify a site token via…
Browse files Browse the repository at this point in the history
… `SiteVerification::insert`.
  • Loading branch information
eason9487 committed Jul 12, 2023
1 parent df29aea commit 2cab1cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
14 changes: 10 additions & 4 deletions src/API/Google/SiteVerification.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected function get_token( string $identifier ): string {
*
* @param string $identifier The URL of the site to verify (including protocol).
*
* @throws Exception When unable to verify token.
* @throws ExceptionWithResponseData When unable to verify token.
*/
protected function insert( string $identifier ) {
/** @var SiteVerificationService $service */
Expand All @@ -160,9 +160,15 @@ protected function insert( string $identifier ) {
$service->webResource->insert( self::VERIFICATION_METHOD, $post_body );
} catch ( GoogleServiceException $e ) {
do_action( 'woocommerce_gla_sv_client_exception', $e, __METHOD__ );
throw new Exception(
__( 'Unable to insert site verification.', 'google-listings-and-ads' ),
$e->getCode()

$errors = $this->get_exception_errors( $e );

throw new ExceptionWithResponseData(
/* translators: %s Error message */
sprintf( __( 'Unable to insert site verification: %s', 'google-listings-and-ads' ), reset( $errors ) ),
$e->getCode(),
null,
[ 'errors' => $errors ]
);
}
}
Expand Down
21 changes: 9 additions & 12 deletions tests/Unit/API/Google/SiteVerificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,15 @@ public function test_verify_site_insert_exception() {

$this->verification_service->webResource
->method( 'insert' )
->willThrowException( new GoogleServiceException( 'error', 400 ) );

try {
$this->verification->verify_site( $this->site_url );
} catch ( Exception $e ) {
$this->assertEquals( 1, did_action( 'woocommerce_gla_site_verify_failure' ) );
$this->assertEquals( 400, $e->getCode() );
$this->assertEquals(
'Unable to insert site verification.',
$e->getMessage()
);
}
->willThrowException( $this->get_google_service_exception( 400, 'No necessary verification token.' ) );

$this->expectException( ExceptionWithResponseData::class );
$this->expectExceptionCode( 400 );
$this->expectExceptionMessage( 'Unable to insert site verification: No necessary verification token.' );

$this->verification->verify_site( $this->site_url );

$this->assertEquals( 1, did_action( 'woocommerce_gla_site_verify_failure' ) );
}

public function test_verify_site() {
Expand Down

0 comments on commit 2cab1cd

Please sign in to comment.