Catch JsonException and rethrow in JsonEncode
This commit is contained in:
parent
eb438a48d6
commit
9c76790ee8
@ -35,14 +35,19 @@ class JsonEncode implements EncoderInterface
|
||||
public function encode($data, $format, array $context = [])
|
||||
{
|
||||
$context = $this->resolveContext($context);
|
||||
$options = $context['json_encode_options'];
|
||||
|
||||
$encodedJson = json_encode($data, $context['json_encode_options']);
|
||||
try {
|
||||
$encodedJson = json_encode($data, $options);
|
||||
} catch (\JsonException $e) {
|
||||
throw new NotEncodableValueException($e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $context['json_encode_options'])) {
|
||||
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $options)) {
|
||||
return $encodedJson;
|
||||
}
|
||||
|
||||
if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($context['json_encode_options'] & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
|
||||
if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($options & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
|
||||
throw new NotEncodableValueException(json_last_error_msg());
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user