[ticket/10631] Simplify exceptions

PHPBB-10631
This commit is contained in:
Nathan Guse 2012-07-23 20:28:04 -05:00 committed by Unknown Bliss
parent 89f4cf6a8c
commit 2a7e129291
4 changed files with 55 additions and 85 deletions

View file

@ -49,9 +49,12 @@ class acp_extensions
{
$md_manager = new phpbb_extension_metadata_manager($ext_name, $db, $phpbb_extension_manager, $phpbb_root_path, ".$phpEx", $template, $config);
try{
try
{
$md_manager->get_metadata('all');
} catch( Exception $e ) {
}
catch(phpbb_extension_exception $e)
{
trigger_error($e);
}
}
@ -172,7 +175,8 @@ class acp_extensions
{
$md_manager = $phpbb_extension_manager->get_extension_metadata_manager($name, $this->template);
try {
try
{
$this->template->assign_block_vars('enabled', array(
'EXT_NAME' => $md_manager->get_metadata('display-name'),
@ -183,7 +187,9 @@ class acp_extensions
'DISABLE' => $this->u_action . '&action=disable_pre&ext_name=' . $name,
'PURGE' => $this->u_action . '&action=purge_pre&ext_name=' . $name,
));
} catch( Exception $e ) {
}
catch(phpbb_extension_exception $e)
{
$this->template->assign_block_vars('disabled', array(
'EXT_NAME' => $this->user->lang('EXTENSION_INVALID_LIST', $name, $e),
));
@ -204,7 +210,8 @@ class acp_extensions
{
$md_manager = $phpbb_extension_manager->get_extension_metadata_manager($name, $this->template);
try {
try
{
$this->template->assign_block_vars('disabled', array(
'EXT_NAME' => $md_manager->get_metadata('display-name'),
@ -215,7 +222,9 @@ class acp_extensions
'ENABLE' => $this->u_action . '&action=enable_pre&ext_name=' . $name,
'PURGE' => $this->u_action . '&action=purge_pre&ext_name=' . $name,
));
} catch( Exception $e ) {
}
catch(phpbb_extension_exception $e)
{
$this->template->assign_block_vars('disabled', array(
'EXT_NAME' => $this->user->lang('EXTENSION_INVALID_LIST', $name, $e),
));
@ -238,7 +247,8 @@ class acp_extensions
{
$md_manager = $phpbb_extension_manager->get_extension_metadata_manager($name, $this->template);
try {
try
{
$this->template->assign_block_vars('disabled', array(
'EXT_NAME' => $md_manager->get_metadata('display-name'),
@ -248,7 +258,9 @@ class acp_extensions
$this->output_actions('disabled', array(
'ENABLE' => $this->u_action . '&action=enable_pre&ext_name=' . $name,
));
} catch( Exception $e ) {
}
catch(phpbb_extension_exception $e)
{
$this->template->assign_block_vars('disabled', array(
'EXT_NAME' => $this->user->lang('EXTENSION_INVALID_LIST', $name, $e),
));

View file

@ -1,69 +0,0 @@
<?php
/**
*
* @package extension
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Exception class for metadata
*/
class phpbb_exception_metadata extends LogicException
{
const NOT_SET = 10001;
const INVALID = 10002;
const FILE_GET_CONTENTS = 10003;
const JSON_DECODE = 10004;
const FILE_DOES_NOT_EXIST = 10005;
public function __construct($code, $field_name)
{
$this->code = $code;
$this->field_name = $field_name;
}
public function __toString()
{
return sprintf($this->getErrorMessage(), $this->field_name);
}
public function getErrorMessage()
{
switch ($this->code)
{
case self::NOT_SET:
return 'The "%s" meta field has not been set.';
break;
case self::INVALID:
return 'The "%s" meta field is not valid.';
break;
case self::FILE_GET_CONTENTS:
return 'file_get_contents failed on %s';
break;
case self::JSON_DECODE:
return 'json_decode failed on %s';
break;
case self::FILE_DOES_NOT_EXIST:
return 'Required file does not exist at %s';
break;
default:
return 'An unexpected error has occurred.';
break;
}
}
}

View file

@ -0,0 +1,27 @@
<?php
/**
*
* @package extension
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Exception class for metadata
*/
class phpbb_extension_exception extends UnexpectedValueException
{
public function __toString()
{
return $this->getMessage();
}
}

View file

@ -114,7 +114,7 @@ class phpbb_extension_metadata_manager
if (!file_exists($this->metadata_file))
{
throw new phpbb_exception_metadata(phpbb_exception_metadata::FILE_DOES_NOT_EXIST, $this->metadata_file);
throw new phpbb_extension_exception('The required file does not exist: ' . $this->metadata_file);
}
}
@ -127,18 +127,18 @@ class phpbb_extension_metadata_manager
{
if (!file_exists($this->metadata_file))
{
throw new phpbb_exception_metadata(phpbb_exception_metadata::FILE_DOES_NOT_EXIST, $this->metadata_file);
throw new phpbb_extension_exception('The required file does not exist: ' . $this->metadata_file);
}
else
{
if (!($file_contents = file_get_contents($this->metadata_file)))
{
throw new phpbb_exception_metadata(phpbb_exception_metadata::FILE_GET_CONTENTS, $this->metadata_file);
throw new phpbb_extension_exception('file_get_contents failed on ' . $this->metadata_file);
}
if (($metadata = json_decode($file_contents, true)) === NULL)
{
throw new phpbb_exception_metadata(phpbb_exception_metadata::JSON_DECODE, $this->metadata_file);
throw new phpbb_extension_exception('json_decode failed on ' . $this->metadata_file);
}
$this->metadata = $metadata;
@ -182,12 +182,12 @@ class phpbb_extension_metadata_manager
{
if (!isset($this->metadata[$name]))
{
throw new phpbb_exception_metadata(phpbb_exception_metadata::NOT_SET, $name);
throw new phpbb_extension_exception("Required meta field '$name' has not been set.");
}
if (!preg_match($fields[$name], $this->metadata[$name]))
{
throw new phpbb_exception_metadata(phpbb_exception_metadata::INVALID, $name);
throw new phpbb_extension_exception("Meta field '$name' is invalid.");
}
}
@ -222,14 +222,14 @@ class phpbb_extension_metadata_manager
{
if (empty($this->metadata['authors']))
{
throw new phpbb_exception_metadata(phpbb_exception_metadata::NOT_SET, 'authors');
throw new phpbb_extension_exception("Required meta field 'authors' has not been set.");
}
foreach ($this->metadata['authors'] as $author)
{
if (!isset($author['name']))
{
throw new phpbb_exception_metadata(phpbb_exception_metadata::NOT_SET, 'author name');
throw new phpbb_extension_exception("Required meta field 'author name' has not been set.");
}
}