Merge pull request #7343 from nextcloud/fix_exception_on_unreachable_server_take2

Actually set the status so we don't cause another exception
This commit is contained in:
Morris Jobke 2017-11-29 21:13:55 +01:00 committed by GitHub
commit 87aa0723f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 0 deletions

View File

@ -198,6 +198,7 @@ class GetSharedSecret extends Job {
$this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']);
} }
} catch (ConnectException $e) { } catch (ConnectException $e) {
$status = -1; // There is no status code if we could not connect
$this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']);
} catch (\Exception $e) { } catch (\Exception $e) {
$status = Http::STATUS_INTERNAL_SERVER_ERROR; $status = Http::STATUS_INTERNAL_SERVER_ERROR;

View File

@ -198,6 +198,7 @@ class RequestSharedSecret extends Job {
$this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']);
} }
} catch (ConnectException $e) { } catch (ConnectException $e) {
$status = -1; // There is no status code if we could not connect
$this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']);
} catch (\Exception $e) { } catch (\Exception $e) {
$status = Http::STATUS_INTERNAL_SERVER_ERROR; $status = Http::STATUS_INTERNAL_SERVER_ERROR;

View File

@ -28,6 +28,7 @@
namespace OCA\Federation\Tests\BackgroundJob; namespace OCA\Federation\Tests\BackgroundJob;
use GuzzleHttp\Exception\ConnectException;
use OCA\Federation\BackgroundJob\GetSharedSecret; use OCA\Federation\BackgroundJob\GetSharedSecret;
use OCA\Files_Sharing\Tests\TestCase; use OCA\Files_Sharing\Tests\TestCase;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
@ -277,4 +278,41 @@ class GetSharedSecretTest extends TestCase {
$this->invokePrivate($this->getSharedSecret, 'run', [$argument]); $this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
} }
public function testRunConnectionError() {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
$argument = ['url' => $target, 'token' => $token];
$this->timeFactory->method('getTime')
->willReturn(42);
$this->urlGenerator
->expects($this->once())
->method('getAbsoluteURL')
->with('/')
->willReturn($source);
$this->httpClient->expects($this->once())->method('get')
->with(
$target . '/ocs/v2.php/apps/federation/api/v1/shared-secret?format=json',
[
'query' =>
[
'url' => $source,
'token' => $token
],
'timeout' => 3,
'connect_timeout' => 3,
]
)->willThrowException($this->createMock(ConnectException::class));
$this->dbHandler->expects($this->never())->method('addToken');
$this->trustedServers->expects($this->never())->method('addSharedSecret');
$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
$this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
}
} }

View File

@ -27,6 +27,7 @@
namespace OCA\Federation\Tests\BackgroundJob; namespace OCA\Federation\Tests\BackgroundJob;
use GuzzleHttp\Exception\ConnectException;
use OCA\Federation\BackgroundJob\RequestSharedSecret; use OCA\Federation\BackgroundJob\RequestSharedSecret;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
@ -262,4 +263,41 @@ class RequestSharedSecretTest extends TestCase {
$this->invokePrivate($this->requestSharedSecret, 'run', [$argument]); $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
} }
public function testRunConnectionError() {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
$argument = ['url' => $target, 'token' => $token];
$this->timeFactory->method('getTime')->willReturn(42);
$this->urlGenerator
->expects($this->once())
->method('getAbsoluteURL')
->with('/')
->willReturn($source);
$this->httpClient
->expects($this->once())
->method('post')
->with(
$target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret?format=json',
[
'body' =>
[
'url' => $source,
'token' => $token
],
'timeout' => 3,
'connect_timeout' => 3,
]
)->willThrowException($this->createMock(ConnectException::class));
$this->dbHandler->expects($this->never())->method('addToken');
$this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
$this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
}
} }