2
0
mirror of https://github.com/boostorg/website.git synced 2026-01-19 04:42:17 +00:00

Adjust a lot of the error handling.

Use exceptions for most internal errors. Return error codes for most errors.
This commit is contained in:
Daniel James
2016-09-29 02:08:52 +01:00
parent f4472fc751
commit b8120cc53c
15 changed files with 57 additions and 57 deletions

View File

@@ -10,9 +10,11 @@ function html_encode($text) {
return htmlentities($text, ENT_COMPAT, 'UTF-8');
}
class BoostException extends RuntimeException {}
spl_autoload_register(function($name) {
if (!preg_match('@^[A-Za-z0-9\\\\_]*$@', $name)) {
throw new \RuntimeException("Invalid autoload name: {$name}");
throw new BoostException("Invalid autoload name: {$name}");
}
$file_path = __DIR__.'/'

View File

@@ -11,7 +11,7 @@ class BoostBookParser {
$state = new BoostBookParser_State();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
if (!xml_parse_into_struct($parser, file_get_contents($filename), $state->values, $state->index)) {
die("Error parsing XML");
throw new BoostException("Error parsing XML");
}
xml_parser_free($parser);
@@ -77,7 +77,7 @@ class BoostBookParser {
if (method_exists($this, $name)) {
return $this->{$name}($state);
} else {
die("Unknown node type {$state->get_tag()}\n");
throw new BoostException("Unknown node type {$state->get_tag()}");
}
}
@@ -92,11 +92,11 @@ class BoostBookParser {
$result .= $this->x($state);
}
if ($state->get_tag() != $tag) {
die("Parse error\n");
throw new BoostException("Parse error");
}
return $result;
default:
die("Invalid node in convert_children_to_xhtml: {$state->get_type()}.\n");
throw new BoostException("Invalid node in convert_children_to_xhtml: {$state->get_type()}");
}
}
@@ -108,7 +108,7 @@ class BoostBookParser {
case 'open':
break;
case 'default':
die("Error parsing article.\n");
throw new BoostException("Error parsing article");
}
for(++$state->pos; $state->get_type() != 'close'; ++$state->pos) {
if (in_array($state->get_tag(), array('title', 'articleinfo'))) {
@@ -283,7 +283,7 @@ class BoostBookParser {
}
break;
default:
die("Invalid node in skip_to_end_of_tag.\n");
throw new BoostException("Invalid node in skip_to_end_of_tag");
}
}
@@ -301,7 +301,7 @@ class BoostBookParser {
}
return;
default:
die("Invalid node in skip_to_end_of_tag.\n");
throw new BoostException("Invalid node in skip_to_end_of_tag");
}
}
@@ -330,7 +330,7 @@ class BoostBookParser {
}
return $found;
default:
die("Invalid node in skip_to_end_of_tag.\n");
throw new BoostException("Invalid node in skip_to_end_of_tag");
}
}

View File

@@ -55,7 +55,7 @@ class BoostLibraries
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
if (!xml_parse_into_struct($parser, $xml, $values)) {
die("Error parsing XML");
throw new BoostLibraries_DecodeException("Error parsing XML", $xml);
}
xml_parser_free($parser);
@@ -121,9 +121,9 @@ class BoostLibraries
{
$value = isset($val['value']) ? trim($val['value']) : false;
if($value && $value != 'true' && $value != 'false') {
echo 'Invalid value for ',html_encode($val['tag']),
': ', $value, "\n";
exit(0);
throw new BoostLibraries_DecodeException(
'Invalid value for ',$val['tag'].': '.$value,
$xml);
}
$lib[$val['tag']] = ($value == 'true');
}
@@ -140,8 +140,8 @@ class BoostLibraries
}
break;
default:
echo 'Invalid tag: ', html_encode($val['tag']), "\n";
exit(0);
throw new BoostLibraries_DecodeException(
'Invalid tag: '.$val['tag'], $xml);
}
}
else if ($val['tag'] == 'library' && $val['type'] == 'close' && $lib)
@@ -151,8 +151,8 @@ class BoostLibraries
}
else
{
echo 'Invalid tag: ', html_encode($val['tag']), "\n";
exit(0);
throw new BoostLibraries_DecodeException(
'Invalid tag: '.$val['tag'], $xml);
}
}
@@ -167,7 +167,7 @@ class BoostLibraries
$import = json_decode($json, true);
if (!$import) {
throw new library_decode_exception("Error decoding json.", $json);
throw new BoostLibraries_DecodeException("Error decoding json.", $json);
}
if ($json[0] == '{') {
@@ -231,8 +231,7 @@ class BoostLibraries
BoostVersion::from($update_version) :
BoostVersion::unreleased();
if (!$update_version->is_release()) {
throw new BoostLibraries_exception(
"No version info for {$details['key']}");
throw new BoostLibraries_Exception("No version info for {$details['key']}");
}
if (isset($details['update-version'])) {
unset($details['update-version']);
@@ -285,7 +284,6 @@ class BoostLibraries
* Update the libraries from an array of BoostLibrary.
*
* @param array $update
* @throws BoostLibraries_exception
*/
public function update($update_version = null, $update = null) {
$this->update_start($update_version);
@@ -626,8 +624,7 @@ class BoostLibraries
foreach($libs as $key => $library) {
foreach($library['category'] as $category) {
if(!isset($this->categories[$category])) {
echo 'Unknown category: ', html_encode($category), "\n";
exit(0);
throw new BoostLibraries_Exception('Unknown category: '.$category);
}
$categories[$category]['libraries'][] = $library;
}
@@ -752,8 +749,8 @@ class BoostLibraries_XMLWriter {
}
}
class BoostLibraries_exception extends RuntimeException {}
class library_decode_exception extends BoostLibraries_exception {
class BoostLibraries_Exception extends BoostException {}
class BoostLibraries_DecodeException extends BoostLibraries_Exception {
private $content = '';
function __construct($message, $content) {

View File

@@ -21,7 +21,7 @@ class BoostLibrary
$json = trim($json);
$libs = json_decode($json, true);
if (!is_array($libs)) {
throw new library_decode_exception("Error decoding json.", $json);
throw new BoostLibraries_DecodeException("Error decoding json.", $json);
}
if ($json[0] == '{') {
$libs = array($libs);

View File

@@ -37,8 +37,7 @@ class BoostMaintainers
$matches = null;
if (!preg_match('@^([^\s]+)\s*(.*)$@', $line, $matches)) {
echo "Unable to parse line: {$line}\n";
exit(1);
throw new BoostException("Unable to parse line: {$line}");
}
$key = trim($matches[1]);

View File

@@ -19,9 +19,7 @@ class BoostPages {
$this->release_data = json_decode(
file_get_contents($this->release_file), true);
if (is_null($this->release_data)) {
// Q: Exception? Fallback?
echo "Error decoding release data.\n";
exit(0);
throw new BoostException("Error decoding release data.");
}
}
@@ -101,7 +99,7 @@ class BoostPages {
$record->last_modified = new DateTime();
if (!in_array($record->type, array('release', 'page'))) {
throw new RuntimeException("Unknown record type: ".$record->type);
throw new BoostException("Unknown record type: ".$record->type);
}
}
@@ -242,8 +240,7 @@ class BoostPages_Page {
: ($this->pub_date ? 'released' : 'dev');
if (!preg_match('@^(released|dev|beta) *(\d*)$@', $release_status, $release_parts)) {
echo "Error: Unknown release status: {$this->array_get($release_data, 'release_status')}.\n";
exit(0);
throw new BoostException("Error: Unknown release status: {$this->array_get($release_data, 'release_status')}.");
}
if ($release_parts[2] && $release_parts[1] != 'beta') {

View File

@@ -16,7 +16,7 @@ class BoostSimpleTemplate {
$nodes = $context->partial_loader->load($path);
if (!$nodes) {
throw new BoostSimpleTemplateException("File not found: {$path}");
throw new BoostSimpleTemplate_Exception("File not found: {$path}");
}
return self::interpret($context, $nodes);
@@ -71,7 +71,7 @@ class BoostSimpleTemplate {
);
if (self::match_exists($match, 'error')) {
throw new BoostSimpleTemplateException("Invalid/unsupported tag", $match['tag'][1]);
throw new BoostSimpleTemplate_Exception("Invalid/unsupported tag", $match['tag'][1]);
}
else if (self::match_exists($match, 'unescaped')) {
$node['type'] = '&';
@@ -129,7 +129,7 @@ class BoostSimpleTemplate {
case '/':
$top = array_pop($stack);
if (!$top || $top['node']['symbol'] !== $node['symbol']) {
throw new BoostSimpleTemplateException("Mismatched close tag", $node['offset']);
throw new BoostSimpleTemplate_Exception("Mismatched close tag", $node['offset']);
}
$node = $top['node'];
$node['contents'] = $nodes;
@@ -153,7 +153,7 @@ class BoostSimpleTemplate {
if ($stack) {
$top = end($stack);
throw new BoostSimpleTemplateException("Unclosed tag: {$top['node']['symbol']}", $top['node']['offset']);
throw new BoostSimpleTemplate_Exception("Unclosed tag: {$top['node']['symbol']}", $top['node']['offset']);
}
$end = substr($template, $offset);
@@ -382,7 +382,7 @@ class BoostSimpleTemplate_Context {
}
}
class BoostSimpleTemplateException extends \RuntimeException {
class BoostSimpleTemplate_Exception extends BoostException {
var $offset;
function __construct($message, $offset = null) {

View File

@@ -252,7 +252,7 @@ class BoostSiteTools_Upgrades {
$filename = $site_tools->root.'/generated/state/version.txt';
$file_contents = trim(file_get_contents($filename));
if (!preg_match('@^[0-9]+$@', $file_contents)) {
throw new RuntimeException("Error reading state version");
throw new BoostException("Error reading state version");
}
$current_version = intval($file_contents);
foreach (self::$versions as $version => $upgrade_function) {
@@ -265,6 +265,6 @@ class BoostSiteTools_Upgrades {
}
static function old_upgrade() {
throw new RuntimeException("Old unsupported data version.");
throw new BoostException("Old unsupported data version.");
}
}

View File

@@ -4,7 +4,7 @@
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt || http://www.boost.org/LICENSE_1_0.txt)
class BoostStateParseError extends RuntimeException {}
class BoostState_ParseError extends BoostException {}
class BoostState {
static function load($file_path) {
@@ -21,12 +21,12 @@ class BoostState {
$record_key = rtrim(fgets($file));
if (!$record_key) {
fclose($file);
throw new BoostStateParseError();
throw new BoostState_ParseError();
}
$state[$record_key] = self::read_record($file);
} else {
fclose($file);
throw new BoostStateParseError();
throw new BoostState_ParseError();
}
}
fclose($file);
@@ -43,14 +43,14 @@ class BoostState {
$c = fgetc($file);
while (true) {
if (!$c) { throw new BoostStateParseError(); }
if (!$c) { throw new BoostState_ParseError(); }
if ($c == ')') {
if (fgets($file) != "\n") { throw new BoostStateParseError(); }
if (fgets($file) != "\n") { throw new BoostState_ParseError(); }
return $record;
}
if ($c != '-') { throw new BoostStateParseError(); }
if ($c != '-') { throw new BoostState_ParseError(); }
$key = rtrim(fgets($file));
$c = fgetc($file);
@@ -80,7 +80,7 @@ class BoostState {
$record["$key"] = new DateTime(fgets($file));
$c = fgetc($file);
} else {
throw new BoostStateParseError();
throw new BoostState_ParseError();
}
}
}

View File

@@ -49,7 +49,7 @@ class BoostSuperProject {
$modules[$matches[1]][$matches[2]] = $matches[3];
}
else {
throw new RuntimeException("Unsupported config line: {$line}");
throw new BoostException("Unsupported config line: {$line}");
}
}
@@ -88,7 +88,7 @@ class BoostSuperProject {
}
}
class ProcessError extends RuntimeException {
class ProcessError extends BoostException {
public $error_code;
function __construct($error_code) {

View File

@@ -113,7 +113,7 @@ class BoostVersion {
}
}
else {
die("Can't convert to BoostVersion.\n");
throw new BoostVersion_Exception("Can't convert to BoostVersion.");
}
}
@@ -123,7 +123,7 @@ class BoostVersion {
*/
static function current() {
if (BoostVersion::$current == null)
die("Version not set.");
throw new BoostVersion_Exception("Version not set.");
return BoostVersion::$current;
}
@@ -255,9 +255,9 @@ class BoostVersion {
static function set_current($major, $minor, $point) {
if (self::$current != null)
die("Setting current version twice.");
throw new BoostVersion_Exception("Setting current version twice.");
self::$current = self::release($major, $minor, $point);
}
}
class BoostVersion_Exception extends RuntimeException {}
class BoostVersion_Exception extends BoostException {}

View File

@@ -8,6 +8,7 @@ require_once(__DIR__.'/../common/code/bootstrap.php');
function main($args) {
if (!array_key_exists('page', $args)) {
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Missing page argument', true, 400);
echo "Missing page argument.\n";
exit(1);
}
@@ -18,6 +19,7 @@ function main($args) {
$page->display();
break;
default:
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Page not found', true, 404);
echo "Unknown page: ", htmlentities($args['page']), "\n";
exit(1);
}

View File

@@ -1,12 +1,13 @@
<?php
require_once(__DIR__.'/../common/code/boost.php');
require_once(__DIR__.'/../common/code/bootstrap.php');
if (isset($_GET['version'])) {
try {
$version = BoostVersion::from($_GET['version']);
}
catch (BoostVersion_Exception $e) {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
echo json_encode(Array(
'error' => $e->getMessage(),
));

View File

@@ -53,7 +53,7 @@ class LibraryPage {
$filter_value = '';
if (!preg_match('@^[-_a-zA-Z0-9]+$@', $view_value)) {
die('Invalid view value.');
throw new BoostException('Invalid view value.');
}
if (strpos($view_value, 'filtered_') === 0) {

View File

@@ -5,7 +5,9 @@
require_once(__DIR__.'/../common/code/boost_config.php');
if (!defined('BOOST_TASKS_DIR')) {
die("Tasks directory not set.");
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
echo "Tasks directory not set.";
exit(1);
}
require_once(BOOST_TASKS_DIR.'/webhook/webhook.php');