Skip to content

Commit

Permalink
Merge pull request #423 from kenjis/fix-network-error-handling
Browse files Browse the repository at this point in the history
fix: GitHub network error handling
  • Loading branch information
kenjis authored Feb 17, 2024
2 parents e8727de + 76024a7 commit e009184
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
6 changes: 4 additions & 2 deletions app/Controllers/Contribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Controllers;

use Github\Exception\ExceptionInterface;
use Psr\Http\Client\ClientExceptionInterface;

class Contribute extends BaseController
{
Expand All @@ -16,7 +16,9 @@ public function index()
// Contributors are already sorted, so grab the first 12
$data['contributors'][$id] = array_slice($contributors, 0, 12);
}
} catch (ExceptionInterface $e) {
} catch (ClientExceptionInterface $e) {
log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage());

$data['contributors'] = null;
}

Expand Down
6 changes: 4 additions & 2 deletions app/Controllers/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Controllers;

use Github\Exception\ExceptionInterface;
use Psr\Http\Client\ClientExceptionInterface;

class Download extends BaseController
{
Expand All @@ -18,7 +18,9 @@ public function index()
'v3link' => end($releases['framework3'])->download_url,
'v4link' => end($releases['framework4'])->download_url,
];
} catch (ExceptionInterface $e) {
} catch (ClientExceptionInterface $e) {
log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage());

$data = [
'v3name' => '<em>unknown</em>',
'v4name' => '<em>unknown</em>',
Expand Down
6 changes: 4 additions & 2 deletions app/Controllers/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Controllers;

use Github\Exception\ExceptionInterface;
use Psr\Http\Client\ClientExceptionInterface;

class Home extends BaseController
{
Expand All @@ -17,7 +17,9 @@ public function index()
'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count),
'forks_count' => number_format($repos['codeigniter4']->forks_count),
];
} catch (ExceptionInterface $e) {
} catch (ClientExceptionInterface $e) {
log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage());

$data = [
'html_url' => 'https://github.com/codeigniter4/CodeIgniter4',
'stargazers_count' => '',
Expand Down
30 changes: 30 additions & 0 deletions tests/feature/BasicPagesTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

use App\Libraries\GitHub;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\FeatureTestTrait;
use Config\Services;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\Request;
use Tests\Support\ProjectTestCase;

/**
Expand All @@ -20,6 +24,32 @@ public function testCanViewHome()
$result->assertSee('The small framework with powerful features');
}

public function testCanViewHomeWhenConnectException()
{
$github = $this->getMockBuilder(GitHub::class)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->onlyMethods(['getRepos'])
->getMock();
$github->method('getRepos')->willThrowException(
new ConnectException(
'cURL error 6: Could not resolve host: api.github.com (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://api.github.com/repos/bcit-ci/CodeIgniter',
new Request(
'GET',
'https://api.github.com/repos/bcit-ci/CodeIgniter'
)
)
);
Services::injectMock('github', $github);

$result = $this->get('/');

$result->assertStatus(200);
$result->assertSee('The small framework with powerful features');
}

public function testCanViewDiscuss()
{
$result = $this->get('/discuss');
Expand Down

0 comments on commit e009184

Please sign in to comment.