* better error handling for monsters and spells loader

* check if file exist before loading
* save errors to system/logs/error.log
This commit is contained in:
slawkens1
2017-10-16 23:31:28 +02:00
parent c914a73352
commit 66a3c46aaf
9 changed files with 119 additions and 22 deletions

View File

@@ -36,6 +36,8 @@ class OTS_MonstersList implements Iterator, Countable, ArrayAccess
*/
private $monsters = array();
private $lastMonsterFile = '';
private $hasErrors = false;
/**
* Loads monsters mapping file.
*
@@ -57,9 +59,18 @@ class OTS_MonstersList implements Iterator, Countable, ArrayAccess
$this->monstersPath .= '/';
}
// check if monsters.xml exist
if(!@file_exists($this->monstersPath . 'monsters.xml')) {
log_append('error.log', '[OTS_MonstersList.php] Fatal error: Cannot load monsters.xml. File does not exist. (' . $this->monstersPath . 'monsters.xml' . '). Error: ' . print_r(error_get_last(), true));
throw new Exception('Error: Cannot load monsters.xml. File not found. More info in system/logs/error.log file.');
}
// loads monsters mapping file
$monsters = new DOMDocument();
$monsters->load($this->monstersPath . 'monsters.xml');
if(!@$monsters->load($this->monstersPath . 'monsters.xml')) {
log_append('error.log', '[OTS_MonstersList.php] Fatal error: Cannot load monsters.xml (' . $this->monstersPath . 'monsters.xml' . '). Error: ' . print_r(error_get_last(), true));
throw new Exception('Error: Cannot load monsters.xml. File is invalid. More info in system/logs/error.log file.');
}
foreach( $monsters->getElementsByTagName('monster') as $monster)
{
@@ -101,6 +112,16 @@ class OTS_MonstersList implements Iterator, Countable, ArrayAccess
return isset($this->monsters[$name]);
}
function xmlErrorHandler($errno, $errstr, $errfile, $errline)
{
if($errno==E_WARNING && (substr_count($errstr,"DOMDocument::loadXML()")>0)) {
//throw new DOMException($errstr);
log_append('error.log', '[OTS_MonstersList.php] Fatal error: Cannot load ' . $this->lastMonsterFile . ' - ' . $errstr);
$this->hasErrors = true;
}
else
return false;
}
/**
* Returns loaded data of given monster.
*
@@ -112,21 +133,31 @@ class OTS_MonstersList implements Iterator, Countable, ArrayAccess
*/
public function getMonster($name)
{
global $lastMonsterFile;
// checks if monster exists
if( isset($this->monsters[$name]) )
{
// loads file
$monster = new OTS_Monster();
//echo $this->monstersPath . $this->monsters[$name];
// check if monster file exist
if(file_exists($this->monstersPath . $this->monsters[$name])) {
$monster->loadXML(trim(file_get_contents($this->monstersPath . $this->monsters[$name])));
set_error_handler(array($this, 'xmlErrorHandler'));
$this->lastMonsterFile = $this->monstersPath . $this->monsters[$name];
@$monster->loadXML(trim(file_get_contents($this->monstersPath . $this->monsters[$name])));
restore_error_handler();
}
return $monster;
}
throw new OutOfBoundsException();
}
public function hasErrors() {
return $this->hasErrors;
}
/**
* Returns amount of monsters loaded.
*

View File

@@ -93,9 +93,18 @@ class OTS_SpellsList implements IteratorAggregate, Countable
*/
public function __construct($file)
{
// loads DOM document
// check if spells.xml exist
if(!@file_exists($file)) {
log_append('error.log', '[OTS_SpellsList.php] Fatal error: Cannot load spells.xml. File does not exist. (' . $file . '). Error: ' . print_r(error_get_last(), true));
throw new Exception('Error: Cannot load spells.xml. File not found. More info in system/logs/error.log file.');
}
// loads monsters mapping file
$spells = new DOMDocument();
$spells->load($file);
if(!@$spells->load($file)) {
log_append('error.log', '[OTS_SpellsList.php] Fatal error: Cannot load spells.xml (' . $file . '). Error: ' . print_r(error_get_last(), true));
throw new Exception('Error: Cannot load spells.xml. File is invalid. More info in system/logs/error.log file.');
}
// loads runes
foreach( $spells->getElementsByTagName('rune') as $rune)