Skip to content

Commit bbc600b

Browse files
committed
Removing exceptions
This is mostly for a simplification - it seemed the only reason we had the special exception interface was so that we could turn the exceptions in the dev environment into JSON. But there are other ways to do this, and I'm not sure I care enough anyways :).
1 parent fccb120 commit bbc600b

9 files changed

+10
-168
lines changed

app/config/services.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ services:
3434
class: AppBundle\Issues\GitHubRequestHandler
3535
arguments: ['@app.github.labels_api', '@event_dispatcher', '@app.repository_provider', '@service_container']
3636

37-
app.github.exception_listener:
38-
class: AppBundle\Listener\GitHubExceptionListener
39-
arguments: ['%kernel.debug%']
40-
tags:
41-
- { name: kernel.event_subscriber }
42-
4337
app.repository_provider:
4438
class: AppBundle\Repository\Provider\InMemoryRepositoryProvider
4539
arguments: [ '%repositories%' ]

src/AppBundle/Exception/GitHubAccessDeniedException.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/AppBundle/Exception/GitHubBadRequestException.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/AppBundle/Exception/GitHubExceptionInterface.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/AppBundle/Exception/GitHubInvalidConfigurationException.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/AppBundle/Exception/GitHubPreconditionFailedException.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/AppBundle/Exception/GitHubRuntimeException.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/AppBundle/Issues/GitHubRequestHandler.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
namespace AppBundle\Issues;
44

55
use AppBundle\Event\GitHubEvent;
6-
use AppBundle\Exception\GitHubAccessDeniedException;
7-
use AppBundle\Exception\GitHubBadRequestException;
8-
use AppBundle\Exception\GitHubExceptionInterface;
9-
use AppBundle\Exception\GitHubInvalidConfigurationException;
10-
use AppBundle\Exception\GitHubPreconditionFailedException;
11-
use AppBundle\Exception\GitHubRuntimeException;
126
use AppBundle\Repository\Provider\RepositoryProviderInterface;
13-
use Github\Api\Issue\Labels;
147
use Symfony\Component\DependencyInjection\ContainerInterface;
158
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
169
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
11+
use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException;
12+
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
1713

1814
/**
1915
* Handles GitHub webhook requests
@@ -36,33 +32,31 @@ public function __construct(EventDispatcherInterface $dispatcher, RepositoryProv
3632
/**
3733
* @param Request $request
3834
* @return array The response data
39-
*
40-
* @throws GitHubExceptionInterface When the request or the configuration are invalid
4135
*/
4236
public function handle(Request $request)
4337
{
4438
$data = json_decode($request->getContent(), true);
4539
if (null === $data) {
46-
throw new GitHubBadRequestException('Invalid JSON body!');
40+
throw new BadRequestHttpException('Invalid JSON body!');
4741
}
4842

4943
$repositoryFullName = isset($data['repository']['full_name']) ? $data['repository']['full_name'] : null;
5044
if (empty($repositoryFullName)) {
51-
throw new GitHubBadRequestException('No repository name!');
45+
throw new BadRequestHttpException('No repository name!');
5246
}
5347

5448
$repository = $this->repositoryProvider->getRepository($repositoryFullName);
5549

5650
if (!$repository) {
57-
throw new GitHubPreconditionFailedException(sprintf('Unsupported repository "%s".', $repositoryFullName));
51+
throw new PreconditionFailedHttpException(sprintf('Unsupported repository "%s".', $repositoryFullName));
5852
}
5953

6054
if ($repository->getSecret()) {
6155
if (!$request->headers->has('X-Hub-Signature')) {
62-
throw new GitHubAccessDeniedException('The request is not secured.');
56+
throw new AccessDeniedException('The request is not secured.');
6357
}
6458
if (!$this->authenticate($request->headers->get('X-Hub-Signature'), $repository->getSecret(), $request->getContent())) {
65-
throw new GitHubAccessDeniedException('Invalid signature.');
59+
throw new AccessDeniedException('Invalid signature.');
6660
}
6761
}
6862

@@ -76,22 +70,18 @@ public function handle(Request $request)
7670
try {
7771
$this->dispatcher->dispatch('github.'.$eventName, $event);
7872
} catch (\Exception $e) {
79-
throw new GitHubRuntimeException(sprintf('Failed dispatching "%s" event for "%s" repository.', $eventName, $repository), 0, $e);
73+
throw new \RuntimeException(sprintf('Failed dispatching "%s" event for "%s" repository.', $eventName, $repository), 0, $e);
8074
}
8175

8276
$responseData = $event->getResponseData();
8377

84-
if (empty($responseData)) {
85-
throw new GitHubPreconditionFailedException(sprintf('Unsupported event "%s"', $eventName));
86-
}
87-
8878
return $responseData;
8979
}
9080

9181
private function authenticate($hash, $key, $data)
9282
{
9383
if (!extension_loaded('hash')) {
94-
throw new GitHubInvalidConfigurationException('"hash" extension is needed to check request signature.');
84+
throw new \RuntimeException('"hash" extension is needed to check request signature.');
9585
}
9686

9787
return $hash !== 'sha1='.hash_hmac('sha1', $data, $key);

src/AppBundle/Listener/GitHubExceptionListener.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)