Skip to content
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

Add new JSONThrowOnError tool #40

Merged
merged 3 commits into from
Dec 23, 2018
Merged

Add new JSONThrowOnError tool #40

merged 3 commits into from
Dec 23, 2018

Conversation

wata727
Copy link
Owner

@wata727 wata727 commented Dec 22, 2018

PHP 7.3 makes it to easy to handle json_* function errors easily.
Previously, these function didn't throw an exception when an error occurred. But now, it throws JsonException if passed JSON_THROW_ON_ERROR as a option. This is a better way to handle errors.

Before

json_decode("{");
if (json_last_error() !== JSON_ERROR_NONE) {
    echo "An error occurred";
}

After

try {
    json_decode("{", false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $exn) {
    echo "An error occurred";
}

Reference

https://secure.php.net/manual/en/function.json-decode.php
https://secure.php.net/manual/en/function.json-encode.php

@wata727 wata727 force-pushed the json_throw_on_error branch 2 times, most recently from 37ecb25 to 6890851 Compare December 22, 2018 13:55
@wata727 wata727 merged commit fbbd6f0 into master Dec 23, 2018
@wata727 wata727 deleted the json_throw_on_error branch December 23, 2018 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant