This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added proper error handling at the library level. This basically iden…
…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
Showing
4 changed files
with
98 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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 | ||
{ | ||
|
@@ -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) | ||
{ | ||
|
@@ -266,6 +268,11 @@ public function getSectionByKey($key) | |
return $section; | ||
} | ||
} | ||
|
||
throw new Plex_Exception_Server_Library( | ||
'RESOURCE_NOT_FOUND', | ||
array('section', $key) | ||
); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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 | ||
{ | ||
|
@@ -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() | ||
|
@@ -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. | ||
|
@@ -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. | ||
|
||
|
@@ -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) | ||
); | ||
} | ||
} | ||
|
||
|