Skip to content
Snippets Groups Projects
Commit 4587f67b authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Catch SpotifyWebAPI exception

parent 280ba111
No related branches found
No related tags found
1 merge request!53Resolve "Catch SpotifyWebAPIException"
This commit is part of merge request !53. Comments created here will be created in the context of that merge request.
This diff is collapsed.
...@@ -4,15 +4,14 @@ namespace App\Controller; ...@@ -4,15 +4,14 @@ namespace App\Controller;
use App\Service\SpotifyApiService; use App\Service\SpotifyApiService;
use App\Service\SpotifyPlaylistService; use App\Service\SpotifyPlaylistService;
use SpotifyWebAPI\SpotifyWebAPI;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
class ArtistController extends AbstractController class ArtistController extends AbstractController
{ {
/** @var SpotifyWebAPI */ private SpotifyWebAPI $spotifyApi;
private $spotifyApi;
public function __construct(SpotifyApiService $spotifyApiService) public function __construct(SpotifyApiService $spotifyApiService)
{ {
...@@ -24,10 +23,8 @@ class ArtistController extends AbstractController ...@@ -24,10 +23,8 @@ class ArtistController extends AbstractController
/** /**
* @Route("/artists/top", name="get-artists-top") * @Route("/artists/top", name="get-artists-top")
*
* @return Response
*/ */
public function getArtists(): Response public function getArtists(): JsonResponse
{ {
$rawTopArtists = $this->spotifyApi->getMyTop( $rawTopArtists = $this->spotifyApi->getMyTop(
'artists', 'artists',
......
...@@ -23,8 +23,6 @@ class AuthenticationController extends AbstractController ...@@ -23,8 +23,6 @@ class AuthenticationController extends AbstractController
/** /**
* @Route("/auth", name="authenticate") * @Route("/auth", name="authenticate")
*
* @return RedirectResponse
*/ */
public function authenticate(): RedirectResponse public function authenticate(): RedirectResponse
{ {
...@@ -46,8 +44,6 @@ class AuthenticationController extends AbstractController ...@@ -46,8 +44,6 @@ class AuthenticationController extends AbstractController
/** /**
* @Route("/callback", name="callback") * @Route("/callback", name="callback")
*
* @return RedirectResponse
*/ */
public function callback(Request $request): RedirectResponse public function callback(Request $request): RedirectResponse
{ {
...@@ -77,8 +73,6 @@ class AuthenticationController extends AbstractController ...@@ -77,8 +73,6 @@ class AuthenticationController extends AbstractController
/** /**
* @Route("/logout", name="logout") * @Route("/logout", name="logout")
*
* @return RedirectResponse
*/ */
public function logout(): RedirectResponse public function logout(): RedirectResponse
{ {
......
...@@ -4,17 +4,14 @@ namespace App\Controller; ...@@ -4,17 +4,14 @@ namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController class DefaultController extends AbstractController
{ {
/** /**
* @Route("/", name="homepage") * @Route("/", name="homepage")
*
* @return Response
*/ */
public function index(): Response public function index(): RedirectResponse
{ {
return new RedirectResponse($this->generateUrl('playlist-generator')); return new RedirectResponse($this->generateUrl('playlist-generator'));
} }
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace App\Controller; namespace App\Controller;
use App\Service\SpotifyApiService; use App\Service\SpotifyApiService;
use SpotifyWebAPI\SpotifyWebAPI;
use SpotifyWebAPI\SpotifyWebAPIException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
...@@ -10,8 +12,7 @@ use Symfony\Component\Routing\Annotation\Route; ...@@ -10,8 +12,7 @@ use Symfony\Component\Routing\Annotation\Route;
class NowPlayingController extends AbstractController class NowPlayingController extends AbstractController
{ {
/** @var SpotifyWebAPI */ private SpotifyWebAPI $spotifyApi;
private $spotifyApi;
public function __construct(SpotifyApiService $spotifyApiService) public function __construct(SpotifyApiService $spotifyApiService)
{ {
...@@ -20,20 +21,22 @@ class NowPlayingController extends AbstractController ...@@ -20,20 +21,22 @@ class NowPlayingController extends AbstractController
/** /**
* @Route("/now-playing", name="now-playing") * @Route("/now-playing", name="now-playing")
*
* @return Response
*/ */
public function index(): Response public function index(): Response
{ {
try {
$me = $this->spotifyApi->me();
} catch (SpotifyWebAPIException $e) {
return $this->render('error.html.twig');
}
return $this->render('now-playing/index.html.twig', [ return $this->render('now-playing/index.html.twig', [
'user' => $this->spotifyApi->me() 'user' => $me,
]); ]);
} }
/** /**
* @Route("/now-playing/data", name="now-playing-data") * @Route("/now-playing/data", name="now-playing-data")
*
* @return JsonResponse
*/ */
public function update(): JsonResponse public function update(): JsonResponse
{ {
......
...@@ -4,6 +4,8 @@ namespace App\Controller; ...@@ -4,6 +4,8 @@ namespace App\Controller;
use App\Service\SpotifyApiService; use App\Service\SpotifyApiService;
use App\Service\SpotifyPlaylistService; use App\Service\SpotifyPlaylistService;
use SpotifyWebAPI\SpotifyWebAPI;
use SpotifyWebAPI\SpotifyWebAPIException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
...@@ -13,11 +15,8 @@ use Symfony\Component\Routing\Annotation\Route; ...@@ -13,11 +15,8 @@ use Symfony\Component\Routing\Annotation\Route;
class PlaylistController extends AbstractController class PlaylistController extends AbstractController
{ {
/** @var SpotifyWebAPI */ private SpotifyWebAPI $spotifyApi;
private $spotifyApi; private SpotifyPlaylistService $spotifyPlaylistService;
/** @var SpotifyPlaylistService */
private $spotifyPlaylistService;
public function __construct(SpotifyApiService $spotifyApiService, SpotifyPlaylistService $spotifyPlaylistService) public function __construct(SpotifyApiService $spotifyApiService, SpotifyPlaylistService $spotifyPlaylistService)
{ {
...@@ -30,12 +29,14 @@ class PlaylistController extends AbstractController ...@@ -30,12 +29,14 @@ class PlaylistController extends AbstractController
/** /**
* @Route("/playlist-generator", name="playlist-generator") * @Route("/playlist-generator", name="playlist-generator")
*
* @return Response
*/ */
public function playlistGeneratorIndex(): Response public function playlistGeneratorIndex(): Response
{ {
try {
$me = $this->spotifyApi->me(); $me = $this->spotifyApi->me();
} catch (SpotifyWebAPIException $e) {
return $this->render('error.html.twig');
}
return $this->render('playlist-generator/index.html.twig', [ return $this->render('playlist-generator/index.html.twig', [
'user' => $me, 'user' => $me,
...@@ -44,12 +45,11 @@ class PlaylistController extends AbstractController ...@@ -44,12 +45,11 @@ class PlaylistController extends AbstractController
/** /**
* @Route("/playlists/user", name="get-user-playlists") * @Route("/playlists/user", name="get-user-playlists")
*
* @return Response
*/ */
public function getPlaylists(): Response public function getPlaylists(): JsonResponse
{ {
$me = $this->spotifyApi->me(); $me = $this->spotifyApi->me();
$rawPlaylists = $this->spotifyApi->getUserPlaylists( $rawPlaylists = $this->spotifyApi->getUserPlaylists(
$me->id, $me->id,
[ [
...@@ -71,8 +71,6 @@ class PlaylistController extends AbstractController ...@@ -71,8 +71,6 @@ class PlaylistController extends AbstractController
/** /**
* @Route("/show-playlist-content/{playlistId}", name="show-playlist-content") * @Route("/show-playlist-content/{playlistId}", name="show-playlist-content")
*e
* @return Response
*/ */
public function showPlaylistContent(string $playlistId): Response public function showPlaylistContent(string $playlistId): Response
{ {
...@@ -88,10 +86,8 @@ class PlaylistController extends AbstractController ...@@ -88,10 +86,8 @@ class PlaylistController extends AbstractController
/** /**
* @Route("/generate-playlist-from-playlists", name="generate-playlist-from-playlists") * @Route("/generate-playlist-from-playlists", name="generate-playlist-from-playlists")
*
* @return Response
*/ */
public function generatePlaylistFromPlaylists(Request $request): Response public function generatePlaylistFromPlaylists(Request $request): RedirectResponse
{ {
/** @var array */ /** @var array */
$selectedPlaylists = $request->query->get('selected-playlist'); $selectedPlaylists = $request->query->get('selected-playlist');
...@@ -122,7 +118,7 @@ class PlaylistController extends AbstractController ...@@ -122,7 +118,7 @@ class PlaylistController extends AbstractController
$recommendations = $this->spotifyApi->getRecommendations([ $recommendations = $this->spotifyApi->getRecommendations([
'seed_tracks' => [$track->track->id], 'seed_tracks' => [$track->track->id],
'limit' => ($generateLongPlaylist ? 50 : 30) 'limit' => ($generateLongPlaylist ? 50 : 30),
]); ]);
$this->spotifyPlaylistService->printLog(' -> Got ' . count($recommendations->tracks) . ' recommendatations.'); $this->spotifyPlaylistService->printLog(' -> Got ' . count($recommendations->tracks) . ' recommendatations.');
foreach ($recommendations->tracks as $recommendedTrack) { foreach ($recommendations->tracks as $recommendedTrack) {
...@@ -149,7 +145,7 @@ class PlaylistController extends AbstractController ...@@ -149,7 +145,7 @@ class PlaylistController extends AbstractController
$newPlaylist = $this->spotifyPlaylistService->createPlaylistWithRandomTracks( $newPlaylist = $this->spotifyPlaylistService->createPlaylistWithRandomTracks(
$this->spotifyApi, $this->spotifyApi,
$recommendedTrackIds, $recommendedTrackIds,
$generateLongPlaylist ? SpotifyPlaylistService::TRACKS_COUNT_IN_LONG_PLAYLIST : SpotifyPlaylistService::TRACKS_COUNT_IN_SHORT_PLAYLIST $generateLongPlaylist ? SpotifyPlaylistService::TRACKS_COUNT_IN_LONG_PLAYLIST : SpotifyPlaylistService::TRACKS_COUNT_IN_SHORT_PLAYLIST,
); );
$this->addFlash('infos', $this->spotifyPlaylistService->getCreatedPlaylistInformationMessage($newPlaylist)); $this->addFlash('infos', $this->spotifyPlaylistService->getCreatedPlaylistInformationMessage($newPlaylist));
...@@ -159,10 +155,8 @@ class PlaylistController extends AbstractController ...@@ -159,10 +155,8 @@ class PlaylistController extends AbstractController
/** /**
* @Route("/generate-playlist-from-top-artists", name="generate-playlist-from-top-artists") * @Route("/generate-playlist-from-top-artists", name="generate-playlist-from-top-artists")
*
* @return Response
*/ */
public function generatePlaylistFromTopArtists(Request $request): Response public function generatePlaylistFromTopArtists(Request $request): RedirectResponse
{ {
// Swith "random" or "selected" // Swith "random" or "selected"
if (null !== $request->query->get('generate-playlist-random-top-artists')) { if (null !== $request->query->get('generate-playlist-random-top-artists')) {
...@@ -174,11 +168,7 @@ class PlaylistController extends AbstractController ...@@ -174,11 +168,7 @@ class PlaylistController extends AbstractController
return new RedirectResponse($this->generateUrl('playlist-generator')); return new RedirectResponse($this->generateUrl('playlist-generator'));
} }
/** private function generatePlaylistFromSelectedTopArtists(Request $request): RedirectResponse
*
* @return Response
*/
private function generatePlaylistFromSelectedTopArtists(Request $request): Response
{ {
/** @var array */ /** @var array */
$selectedArtists = $request->query->get('selected-artist'); $selectedArtists = $request->query->get('selected-artist');
...@@ -225,11 +215,7 @@ class PlaylistController extends AbstractController ...@@ -225,11 +215,7 @@ class PlaylistController extends AbstractController
return new RedirectResponse($this->generateUrl('show-playlist-content', ['playlistId' => $newPlaylist->id])); return new RedirectResponse($this->generateUrl('show-playlist-content', ['playlistId' => $newPlaylist->id]));
} }
/** private function generatePlaylistFromRandomTopArtists(Request $request): RedirectResponse
*
* @return Response
*/
private function generatePlaylistFromRandomTopArtists(Request $request): Response
{ {
$countInTopArtists = random_int(4, 6); $countInTopArtists = random_int(4, 6);
$countInLessTopArtists = random_int(4, 6); $countInLessTopArtists = random_int(4, 6);
...@@ -284,10 +270,8 @@ class PlaylistController extends AbstractController ...@@ -284,10 +270,8 @@ class PlaylistController extends AbstractController
/** /**
* @Route("/generate-quick-playlist", name="generate-quick-playlist") * @Route("/generate-quick-playlist", name="generate-quick-playlist")
*
* @return Response
*/ */
public function generateQuickPlaylist(Request $request): Response public function generateQuickPlaylist(Request $request): RedirectResponse
{ {
// Swith between "daily mixes" or "tambouille" // Swith between "daily mixes" or "tambouille"
if (null !== $request->query->get('generate-quick-playlist-from-daily-mixes')) { if (null !== $request->query->get('generate-quick-playlist-from-daily-mixes')) {
...@@ -299,11 +283,7 @@ class PlaylistController extends AbstractController ...@@ -299,11 +283,7 @@ class PlaylistController extends AbstractController
return new RedirectResponse($this->generateUrl('playlist-generator')); return new RedirectResponse($this->generateUrl('playlist-generator'));
} }
/** private function generatePlaylistFromDailyMixes(Request $request): RedirectResponse
*
* @return Response
*/
private function generatePlaylistFromDailyMixes(Request $request): Response
{ {
$generateLongPlaylist = (null !== $request->query->get('check-quick-create-long-playlist')); $generateLongPlaylist = (null !== $request->query->get('check-quick-create-long-playlist'));
...@@ -319,11 +299,7 @@ class PlaylistController extends AbstractController ...@@ -319,11 +299,7 @@ class PlaylistController extends AbstractController
return new RedirectResponse($this->generateUrl('show-playlist-content', ['playlistId' => $newPlaylist->id])); return new RedirectResponse($this->generateUrl('show-playlist-content', ['playlistId' => $newPlaylist->id]));
} }
/** private function generateplaylistTambouilleMix(Request $request): RedirectResponse
*
* @return Response
*/
private function generateplaylistTambouilleMix(Request $request): Response
{ {
$generateLongPlaylist = (null !== $request->query->get('check-quick-create-long-playlist')); $generateLongPlaylist = (null !== $request->query->get('check-quick-create-long-playlist'));
......
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{% block title %}Spotify playlist generator{% endblock %}</title>
<link rel="shortcut icon" type="image/png" href="/favicon.png"/>
{% block stylesheets %}{{ encore_entry_link_tags('app') }}{% endblock %}
</head>
<body>
<div class="container-fluid mb-5 mt-2">
<div class="alert alert-warning mt-2">Something went wrong...</div>
</div>
{% block javascripts %}{{ encore_entry_script_tags('app') }}{% endblock %}
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment