# [RFC] [Discussion] JSON_THROW_ON_ERROR

- Thread: https://externals.io/message/100494
- Original archive: https://news-web.php.net/php.internals/100494
- Started by Andrea Faulds on 2017-09-10T01:24:04+00:00
- Messages: 4
- RFC: JSON_THROW_ON_ERROR (Implemented (PHP 7.3)) — https://externals.io/rfc/json_throw_on_error.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #100494 — Andrea Faulds — 2017-09-10T01:24:04+00:00

URL: https://externals.io/message/100494#100494

Hi everyone,

Craig Duncan previously sparked discussion here about JSON's 
error-handling behaviour. Unfortunately, his attempt to change it seems 
not to have gone anywhere, but I have his blessing to try this other 
approach instead.

So here's an RFC: https://wiki.php.net/rfc/json_throw_on_error

The feature can be described in a single paragraph (in fact, the title 
is pretty much enough, the patch is just detail) but it's better to go 
through the proper RFC process.

Please tell me what you think.

Thanks!

-- 
Andrea Faulds
https://ajf.me/

---

## #100495 — Bradley Weston — 2017-09-10T07:42:40+00:00

URL: https://externals.io/message/100494#100495 · In reply to: #100494

I definitely hate having to test the return value and then getting the 
error message via a function call. +1 for ability to throw exceptions 
instead.



> Hi everyone,
>
> Craig Duncan previously sparked discussion here about JSON's 
> error-handling behaviour. Unfortunately, his attempt to change it 
> seems not to have gone anywhere, but I have his blessing to try this 
> other approach instead.
>
> So here's an RFC: https://wiki.php.net/rfc/json_throw_on_error
>
> The feature can be described in a single paragraph (in fact, the title 
> is pretty much enough, the patch is just detail) but it's better to go 
> through the proper RFC process.
>
> Please tell me what you think.
>
> Thanks!

---

## #100546 — Jakub Zelenka — 2017-09-12T16:46:57+00:00

URL: https://externals.io/message/100494#100546 · In reply to: #100494

> Hi everyone,
>
> Craig Duncan previously sparked discussion here about JSON's
> error-handling behaviour. Unfortunately, his attempt to change it seems not
> to have gone anywhere, but I have his blessing to try this other approach
> instead.
>
> So here's an RFC: https://wiki.php.net/rfc/json_throw_on_error
>
> The feature can be described in a single paragraph (in fact, the title is
> pretty much enough, the patch is just detail) but it's better to go through
> the proper RFC process.
>
> Please tell me what you think.
>
>
I think that it makes sense in general. There are just couple of things
that are not described in RFC.

Currently it has higher priority than JSON_PARTIAL_OUTPUT_ON_ERROR. I think
that if someone specify this constant, then the partial output is required
and it should be preferred. In any case I think it should be in the RFC
what the behavior is.

Another thing is the logic of setting global json error. Currently it just
reset the error value so the next call to `json_last_error()` will return 0
(no error) even though there was an error which is in the exception code. I
think that the behavior should be either not resetting the global error
value at all (the previous error should be kept) or setting the global
error value so the json_last_error returns the same value as exception code
or 0 if no error. Again it would be good to mention the behavior in the RFC
so we prevent a new discussion in the PR... :)

Cheers

---

## #100569 — Andrea Faulds — 2017-09-13T21:34:29+00:00

URL: https://externals.io/message/100494#100569 · In reply to: #100546

Hi Jakub,

I already replied to you off-list, but for the benefit of people on-list…

Jakub Zelenka wrote:
> 
>
> > […]
> I think that it makes sense in general. There are just couple of things
> that are not described in RFC.
>
> Currently it has higher priority than JSON_PARTIAL_OUTPUT_ON_ERROR. I think
> that if someone specify this constant, then the partial output is required
> and it should be preferred. In any case I think it should be in the RFC
> what the behavior is.

Per your suggestion, this has been changed so 
 `JSON_PARTIAL_OUTPUT_ON_ERROR` takes precedence. This means if you have a 
wrapper function that just adds a `JSON_THROW_ON_ERROR` flag, you can 
still use JSON_PARTIAL_OUTPUT_ON_ERROR. And the RFC mentions this.

>
> Another thing is the logic of setting global json error. Currently it just
> reset the error value so the next call to `json_last_error()` will return 0
> (no error) even though there was an error which is in the exception code. I
> think that the behavior should be either not resetting the global error
> value at all (the previous error should be kept) or setting the global
> error value so the json_last_error returns the same value as exception code
> or 0 if no error. Again it would be good to mention the behavior in the RFC
> so we prevent a new discussion in the PR... :)

I'm not sure what to do here. I want code to use one error mechanism or 
the other, not both, so I don't want `JSON_THROW_ON_ERROR` to also set the 
global error code. I decided to reset it to no error because there's no 
previous error that needs handling by error-checking code, the exception 
takes care of that. But I can see where you're coming from with the idea 
of not touching it at all.

Thanks.

>
> Cheers
>


-- 
Andrea Faulds
https://ajf.me/
