Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: GitHub network error handling #423

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
'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());

Check warning on line 21 in app/Controllers/Home.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ $repos = $this->github->getRepos(); $data = ['html_url' => $repos['framework4']->html_url, 'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count), 'forks_count' => number_format($repos['codeigniter4']->forks_count)]; } catch (ClientExceptionInterface $e) { - log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + log_message('error', __METHOD__ . '[' . '] ' . get_class($e) . ': ' . $e->getMessage()); $data = ['html_url' => 'https://github.com/codeigniter4/CodeIgniter4', 'stargazers_count' => '', 'forks_count' => '']; } echo $this->render('home', $data); } }

Check warning on line 21 in app/Controllers/Home.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ $repos = $this->github->getRepos(); $data = ['html_url' => $repos['framework4']->html_url, 'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count), 'forks_count' => number_format($repos['codeigniter4']->forks_count)]; } catch (ClientExceptionInterface $e) { - log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + log_message('error', __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); $data = ['html_url' => 'https://github.com/codeigniter4/CodeIgniter4', 'stargazers_count' => '', 'forks_count' => '']; } echo $this->render('home', $data); } }

Check warning on line 21 in app/Controllers/Home.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ $repos = $this->github->getRepos(); $data = ['html_url' => $repos['framework4']->html_url, 'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count), 'forks_count' => number_format($repos['codeigniter4']->forks_count)]; } catch (ClientExceptionInterface $e) { - log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + log_message('error', '[' . '] ' . get_class($e) . ': ' . $e->getMessage()); $data = ['html_url' => 'https://github.com/codeigniter4/CodeIgniter4', 'stargazers_count' => '', 'forks_count' => '']; } echo $this->render('home', $data); } }

Check warning on line 21 in app/Controllers/Home.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ $repos = $this->github->getRepos(); $data = ['html_url' => $repos['framework4']->html_url, 'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count), 'forks_count' => number_format($repos['codeigniter4']->forks_count)]; } catch (ClientExceptionInterface $e) { - log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + log_message('error', '[' . '] ' . __METHOD__ . get_class($e) . ': ' . $e->getMessage()); $data = ['html_url' => 'https://github.com/codeigniter4/CodeIgniter4', 'stargazers_count' => '', 'forks_count' => '']; } echo $this->render('home', $data); } }

Check warning on line 21 in app/Controllers/Home.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ $repos = $this->github->getRepos(); $data = ['html_url' => $repos['framework4']->html_url, 'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count), 'forks_count' => number_format($repos['codeigniter4']->forks_count)]; } catch (ClientExceptionInterface $e) { - log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + log_message('error', '[' . __METHOD__ . get_class($e) . ': ' . $e->getMessage()); $data = ['html_url' => 'https://github.com/codeigniter4/CodeIgniter4', 'stargazers_count' => '', 'forks_count' => '']; } echo $this->render('home', $data); } }

Check warning on line 21 in app/Controllers/Home.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ $repos = $this->github->getRepos(); $data = ['html_url' => $repos['framework4']->html_url, 'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count), 'forks_count' => number_format($repos['codeigniter4']->forks_count)]; } catch (ClientExceptionInterface $e) { - log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + log_message('error', '[' . __METHOD__ . get_class($e) . '] ' . ': ' . $e->getMessage()); $data = ['html_url' => 'https://github.com/codeigniter4/CodeIgniter4', 'stargazers_count' => '', 'forks_count' => '']; } echo $this->render('home', $data); } }

Check warning on line 21 in app/Controllers/Home.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ $repos = $this->github->getRepos(); $data = ['html_url' => $repos['framework4']->html_url, 'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count), 'forks_count' => number_format($repos['codeigniter4']->forks_count)]; } catch (ClientExceptionInterface $e) { - log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + log_message('error', '[' . __METHOD__ . '] ' . ': ' . $e->getMessage()); $data = ['html_url' => 'https://github.com/codeigniter4/CodeIgniter4', 'stargazers_count' => '', 'forks_count' => '']; } echo $this->render('home', $data); } }

Check warning on line 21 in app/Controllers/Home.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ $repos = $this->github->getRepos(); $data = ['html_url' => $repos['framework4']->html_url, 'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count), 'forks_count' => number_format($repos['codeigniter4']->forks_count)]; } catch (ClientExceptionInterface $e) { - log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + log_message('error', '[' . __METHOD__ . '] ' . ': ' . get_class($e) . $e->getMessage()); $data = ['html_url' => 'https://github.com/codeigniter4/CodeIgniter4', 'stargazers_count' => '', 'forks_count' => '']; } echo $this->render('home', $data); } }

Check warning on line 21 in app/Controllers/Home.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ $repos = $this->github->getRepos(); $data = ['html_url' => $repos['framework4']->html_url, 'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count), 'forks_count' => number_format($repos['codeigniter4']->forks_count)]; } catch (ClientExceptionInterface $e) { - log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . $e->getMessage()); $data = ['html_url' => 'https://github.com/codeigniter4/CodeIgniter4', 'stargazers_count' => '', 'forks_count' => '']; } echo $this->render('home', $data); } }

Check warning on line 21 in app/Controllers/Home.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ $repos = $this->github->getRepos(); $data = ['html_url' => $repos['framework4']->html_url, 'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count), 'forks_count' => number_format($repos['codeigniter4']->forks_count)]; } catch (ClientExceptionInterface $e) { - log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . $e->getMessage() . ': '); $data = ['html_url' => 'https://github.com/codeigniter4/CodeIgniter4', 'stargazers_count' => '', 'forks_count' => '']; } echo $this->render('home', $data); } }

$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 @@ -12,7 +16,7 @@
use DatabaseTestTrait;
use FeatureTestTrait;

public function testCanViewHome()

Check warning on line 19 in tests/feature/BasicPagesTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 Unit Tests

Took 1.42s from 0.50s limit to run BasicPagesTest::testCanViewHome
{
$result = $this->get('/');

Expand All @@ -20,6 +24,32 @@
$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 All @@ -28,7 +58,7 @@
$result->assertSee('Security issues should be reported');
}

public function testCanViewContribute()

Check warning on line 61 in tests/feature/BasicPagesTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 Unit Tests

Took 1.34s from 0.50s limit to run BasicPagesTest::testCanViewContribute
{
$result = $this->get('/contribute');

Expand All @@ -36,7 +66,7 @@
$result->assertSee('Contribute to CodeIgniter');
}

public function testCanViewDownload()

Check warning on line 69 in tests/feature/BasicPagesTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 Unit Tests

Took 0.78s from 0.50s limit to run BasicPagesTest::testCanViewDownload
{
$result = $this->get('/download');

Expand Down
Loading