forked from aws/aws-sdk-php
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw a ParserException when content is unparseable
- Loading branch information
Showing
8 changed files
with
72 additions
and
9 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,4 @@ | ||
<?php | ||
namespace Aws\Api\Parser\Exception; | ||
|
||
class ParserException extends \RuntimeException {} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace Aws\Api\Parser; | ||
|
||
use Aws\Api\Parser\Exception\ParserException; | ||
|
||
trait PayloadParserTrait | ||
{ | ||
/** | ||
* @param string $json | ||
* | ||
* @throws ParserException | ||
* | ||
* @return array | ||
*/ | ||
private function parseJson($json) | ||
{ | ||
$jsonPayload = json_decode($json, true); | ||
|
||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new ParserException('Error parsing JSON: ' | ||
. json_last_error_msg()); | ||
} | ||
|
||
return $jsonPayload; | ||
} | ||
|
||
/** | ||
* @param string $xml | ||
* | ||
* @throws ParserException | ||
* | ||
* @return \SimpleXMLElement | ||
*/ | ||
private function parseXml($xml) | ||
{ | ||
$priorSetting = libxml_use_internal_errors(true); | ||
try { | ||
libxml_clear_errors(); | ||
$xmlPayload = new \SimpleXMLElement($xml); | ||
if ($error = libxml_get_last_error()) { | ||
throw new \RuntimeException($error->message); | ||
} | ||
} catch (\Exception $e) { | ||
throw new ParserException("Error parsing XML: {$e->getMessage()}", 0, $e); | ||
} finally { | ||
libxml_use_internal_errors($priorSetting); | ||
} | ||
|
||
return $xmlPayload; | ||
} | ||
} |
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
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
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