Skip to content

Commit

Permalink
Better XML error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Eklund committed May 8, 2017
1 parent 871b208 commit 7f94b66
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
21 changes: 14 additions & 7 deletions src/Engines/XML.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use XMLReader;
use ErrorException;
use pandaac\Exporter\Output;
use InvalidArgumentException;
use UnexpectedValueException;
Expand Down Expand Up @@ -119,9 +120,11 @@ public function __construct(array $attributes = [], array $plural = [])
*/
public function open($source)
{
libxml_use_internal_errors(true);

$this->source = $source;

($source instanceof Source) ? $this->openSource($source) : $this->openFile($source);
$document = $source instanceof Source ? $this->openSource($source) : $this->openFile($source);

$this->reader->setParserProperty(XMLReader::VALIDATE, true);

Expand All @@ -140,9 +143,7 @@ public function open($source)
*/
protected function openSource(Source $source)
{
if (! $this->reader->xml($source->content(), null, LIBXML_NOWARNING | LIBXML_NOERROR)) {
throw new UnexpectedValueException('Unable to open source data.');
}
return $this->reader->xml($source->content(), null);
}

/**
Expand All @@ -157,9 +158,7 @@ protected function openFile($source)
throw new InvalidArgumentException('The first argument must be a valid file.');
}

if (! $this->reader->open($source, null, LIBXML_NOWARNING | LIBXML_NOERROR)) {
throw new UnexpectedValueException('Unable to open file.');
}
return $this->reader->open($source, null);
}

/**
Expand All @@ -177,6 +176,12 @@ public function output()
$this->stopVirtualElement();
}

if (libxml_get_last_error() !== false) {
$error = libxml_get_last_error();

throw new ErrorException($error->message, $error->code, $error->level, $error->file, $error->line);
}

unset($this->references);

return new Output($this->elements);
Expand All @@ -189,6 +194,8 @@ public function output()
*/
public function close()
{
libxml_clear_errors();

$this->reader->close();
}

Expand Down
12 changes: 4 additions & 8 deletions src/Parsers/Monsters.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,10 @@ public function parse(Exporter $exporter, Output $output, array $attributes)
}

return $monsters->each(function ($monster) use ($exporter) {
try {
if ($monster->has('file')) {
$response = $exporter->parse(new Monster, [], $monster->get('file'));

$monster->put('details', $response);
}
} catch (Exception $e) {
//
if ($monster->has('file')) {
$response = $exporter->parse(new Monster, [], $monster->get('file'));

$monster->put('details', $response);
}
});
}
Expand Down
12 changes: 4 additions & 8 deletions src/Parsers/Raids.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ public function parse(Exporter $exporter, Output $output, array $attributes)
}

return $raids->each(function ($monster) use ($exporter) {
try {
if ($monster->has('file')) {
$response = $exporter->parse(new Raid, [], $monster->get('file'));

$monster->put('details', $response);
}
} catch (Exception $e) {
//
if ($monster->has('file')) {
$response = $exporter->parse(new Raid, [], $monster->get('file'));

$monster->put('details', $response);
}
});
}
Expand Down

0 comments on commit 7f94b66

Please sign in to comment.