Skip to content

Commit

Permalink
add json encoding exception
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 2, 2016
1 parent 9667bcb commit 0bda866
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Eloquent/JsonEncodingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Illuminate\Database\Eloquent;

use RuntimeException;

class JsonEncodingException extends RuntimeException
{
/**
* Create a new JSON encoding exception for the model.
*
* @param mixed $model
* @param string $message
* @return static
*/
public static function forModel($model, $message)
{
return new static('Error encoding model ['.get_class($model).'] with ID ['.$model->getKey().'] to JSON: '.$message);
}
}
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2415,14 +2415,14 @@ public function setIncrementing($value)
* @param int $options
* @return string
*
* @throws \InvalidArgumentException
* @throws JsonEncodingException
*/
public function toJson($options = 0)
{
$json = json_encode($this->jsonSerialize(), $options);

if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException(json_last_error_msg());
throw JsonEncodingException::forModel($this, json_last_error_msg());
}

return $json;
Expand Down

0 comments on commit 0bda866

Please sign in to comment.