-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] error handling inconsistencies fixed in the serializer decoders #9586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1404c36
ef32d3c
a329358
30c80cb
0d9f16f
ad8a50f
31b94b0
0c380b6
3d2e7ff
0574236
ebf1ece
e50b0f4
76fd6cb
8e87d01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ interface EncoderInterface | |
* @param array $context options that normalizers/encoders have access to. | ||
* | ||
* @return scalar | ||
* | ||
* @throws UnexpectedValueException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't you need also here the use statement? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. Sorry guys, first code PR, still learning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doing great! ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am learning too 👶 |
||
*/ | ||
public function encode($data, $format, array $context = array()); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,4 +87,41 @@ public function supportsDecoding($format) | |
{ | ||
return self::FORMAT === $format; | ||
} | ||
|
||
/** | ||
* Resolves json_last_error message | ||
* | ||
* @param $error | ||
* | ||
* @return string | ||
*/ | ||
public static function getErrorMessage($error) | ||
{ | ||
if (!function_exists('json_last_error_msg')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would even reduce the indentation of the whole method code, by switching the condition: if (function_exists('json_last_error_msg')) {
return json_last_error_msg();
}
switch (json_last_error()) {
// ...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I totally changed this function, preserving only error message resolution based on a error parameter, no matter if last or not. It can be usefull for PHP versions < 5.5.0. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No sense. Updated |
||
switch (json_last_error()) { | ||
default: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
$message = 'Unknown error'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using variable, you can use if (!function_exists('json_last_error_msg')) {
switch (json_last_error()) {
default:
return 'Unknown error';
}
}
return json_last_error_msg(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, default should probably be the last case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
case JSON_ERROR_DEPTH: | ||
$message = 'Maximum stack depth exceeded'; | ||
break; | ||
case JSON_ERROR_STATE_MISMATCH: | ||
$message = 'Underflow or the modes mismatch'; | ||
break; | ||
case JSON_ERROR_CTRL_CHAR: | ||
$message = 'Unexpected control character found'; | ||
break; | ||
case JSON_ERROR_SYNTAX: | ||
$message = 'Syntax error, malformed JSON'; | ||
break; | ||
case JSON_ERROR_UTF8: | ||
$message = 'Malformed UTF-8 characters, possibly incorrectly encoded'; | ||
break; | ||
} | ||
} else { | ||
$message = json_last_error_msg(); | ||
} | ||
|
||
return $error; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to add the use statement as the exception is not in the same namespace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated