Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Commit

Permalink
Added proper error handling at the library level. This basically iden…
Browse files Browse the repository at this point in the history
…tifies when the polymorphic item fetching methods come up empty and throws a 404 resource not found exception.

I also fixed a bug where doing single item fetch by its key would never work correctly.
  • Loading branch information
nickbart committed Apr 6, 2013
1 parent 92b7744 commit 94b749b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 13 deletions.
50 changes: 50 additions & 0 deletions Exception/Server/Library.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* Plex Exception (Plexception)
*
* @category php-plex
* @package Plex_Exception
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2013 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.2
*
* This file is part of php-plex.
*
* php-plex is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* php-plex is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

/**
* Exception to be thrown for any problems at the library level.
*
* @category php-plex
* @package Plex_Exception
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2013 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.2
*/
class Plex_Exception_Server_Library extends Plex_ExceptionAbstract
{
/**
* List of valid exception types for the library exception class.
* @var mixed[]
*/
protected $validTypes = array(
// Abstract type for specifiying that a resource of type and name was
// not found.
'RESOURCE_NOT_FOUND' => array(
'code' => 404,
'message' => 'The %s "%s" was not found.'
)
);
}
5 changes: 5 additions & 0 deletions Plex.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@

$phpPlexDir = dirname(__FILE__);

// Exception
require_once(sprintf('%s/Exception/ExceptionInterface.php', $phpPlexDir));
require_once(sprintf('%s/Exception/ExceptionAbstract.php', $phpPlexDir));
require_once(sprintf('%s/Exception/Machine.php', $phpPlexDir));
require_once(sprintf('%s/Exception/Server.php', $phpPlexDir));
require_once(sprintf('%s/Exception/Server/Library.php', $phpPlexDir));
// Machine
require_once(sprintf('%s/Machine/MachineInterface.php', $phpPlexDir));
require_once(sprintf('%s/Machine/MachineAbstract.php', $phpPlexDir));
// Server
require_once(sprintf('%s/Server.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/SectionAbstract.php', $phpPlexDir));
Expand All @@ -53,6 +57,7 @@
require_once(sprintf('%s/Server/Library/Item/Artist.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Item/Album.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Item/Track.php', $phpPlexDir));
// Client
require_once(sprintf('%s/Client.php', $phpPlexDir));
require_once(sprintf('%s/Client/ControllerAbstract.php', $phpPlexDir));
require_once(sprintf('%s/Client/Controller/Navigation.php', $phpPlexDir));
Expand Down
15 changes: 11 additions & 4 deletions Server/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* @package Plex_Server
* @subpackage Plex_Server_Library
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2012 Nick Bartkowiak
* @copyright (c) 2013 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.1
* @version 0.0.2
*
* This file is part of php-plex.
*
Expand All @@ -32,9 +32,9 @@
* @package Plex_Server
* @subpackage Plex_Server_Library
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2012 Nick Bartkowiak
* @copyright (c) 2013 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.1
* @version 0.0.2
*/
class Plex_Server_Library extends Plex_Server
{
Expand Down Expand Up @@ -258,6 +258,8 @@ public function getSections()
* @uses Plex_Server_Library_Section::getKey()
*
* @return Plex_Server_Library_Section The request library section.
*
* @throws Plex_Exception_Server_Library()
*/
public function getSectionByKey($key)
{
Expand All @@ -266,6 +268,11 @@ public function getSectionByKey($key)
return $section;
}
}

throw new Plex_Exception_Server_Library(
'RESOURCE_NOT_FOUND',
array('section', $key)
);
}

/**
Expand Down
41 changes: 32 additions & 9 deletions Server/Library/SectionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* @package Plex_Server
* @subpackage Plex_Server_Library
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2012 Nick Bartkowiak
* @copyright (c) 2013 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.1
* @version 0.0.2
*
* This file is part of php-plex.
*
Expand All @@ -33,9 +33,9 @@
* @package Plex_Server
* @subpackage Plex_Server_Library
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2012 Nick Bartkowiak
* @copyright (c) 2013 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.1
* @version 0.0.2
*/
abstract class Plex_Server_Library_SectionAbstract extends Plex_Server_Library
{
Expand Down Expand Up @@ -508,6 +508,9 @@ protected function getItemsByYear($year)
* @param integer|string $polymorphicData Either a rating key, a key, or a
* title for an exact title match that will be used to retrieve a single
* library item.
* @param boolean scopedToItem Tells the method whether or not we are
* scoped to an item. If we are scoped to an item then we are use get
* methods instead of search methods.
*
* @uses Plex_MachineAbstract::getCallingFunction()
* @uses Plex_Server_Library::getItems()
Expand All @@ -519,22 +522,32 @@ protected function getItemsByYear($year)
* @uses Plex_Server_Library_SectionAbstract::getPolymorphicItem()
*
* @return Plex_Server_Library_ItemAbstract The request Plex library item.
*
* @throws Plex_Exception_Server_Library()
*/
protected function getPolymorphicItem($polymorphicData, $scopedToItem = FALSE)
{
if (is_int($polymorphicData)) {
// If we have an integer then we can assume we have a rating key.
return reset(
if ($item = reset(
$this->getItems(
sprintf(
'%s/%d',
Plex_Server_Library::ENDPOINT_METADATA,
$polymorphicData
)
)
)) {
return $item;
}

throw new Plex_Exception_Server_Library(
'RESOURCE_NOT_FOUND',
array('item', $polymorphicData)
);

} else if (strpos($polymorphicData, Plex_Server_Library::ENDPOINT_METADATA)
!= FALSE) {
!== FALSE) {
// If the single item endpoint appears in the polymorphic data then
// is assumed we are dealing with a key, which is already a valid
// endpoint.
Expand All @@ -549,9 +562,16 @@ protected function getPolymorphicItem($polymorphicData, $scopedToItem = FALSE)
$polymorphicData
);

return reset($this->getItems($endpoint));
if ($item = reset($this->getItems($endpoint))) {
return $item;
}

throw new Plex_Exception_Server_Library(
'RESOURCE_NOT_FOUND',
array('item', $polymorphicData)
);

} else {

// If we don't have a rating key or a key then we just assume we're
// doing an exact title match.

Expand Down Expand Up @@ -604,7 +624,10 @@ protected function getPolymorphicItem($polymorphicData, $scopedToItem = FALSE)
}

// Tried to do an exact title match and came up empty.
return FALSE;
throw new Plex_Exception_Server_Library(
'RESOURCE_NOT_FOUND',
array('item', $polymorphicData)
);
}
}

Expand Down

0 comments on commit 94b749b

Please sign in to comment.