Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105653 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 79716 invoked from network); 9 May 2019 19:02:20 -0000 Received: from unknown (HELO mail-pg1-f177.google.com) (209.85.215.177) by pb1.pair.com with SMTP; 9 May 2019 19:02:20 -0000 Received: by mail-pg1-f177.google.com with SMTP id e6so1448647pgc.4 for ; Thu, 09 May 2019 09:06:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=basereality-com.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=/p+jsdrKPLIPv1E823w4Q45O9LYLr2hz0vNke8mV1b4=; b=u12cLCLz1kurxGmmMzBOrMSOSr3nOQ4wLHmBhn/rgEHaCtFAK+rHArRH5pvzRzgN9B /ypMf+4WjMc1p5pj6QCJCOE1nbptJTdLFTldklDoA7DYs4ZP4Rmn9atpqeOGAhYGpTkL KuvgFMBIZF9jGtZ6jFu80hHRgHnCoPSm+QxXZTMabt1fxkpjCh71utjUWLxIzzLGkBQ/ r0+CqdJ33ZFSjxTpVA8lhqQ3GAVxUbfB2vKOWgRZmUi/aSjZG5oHYASqqAGL0kf5GfN/ W0bvHekcEfgwfvwJfudPmdnVsm5ypQ2oDOLOdNUlGrPN+Qczg+vJKo5KWfZ+Qb5JfInw 5l1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=/p+jsdrKPLIPv1E823w4Q45O9LYLr2hz0vNke8mV1b4=; b=F9aPSG4L/Re8LPd58I6fiPYXttKdLyu44FncpftijARz+l9n24LQ5V7cSiXrPdNdDN 87zpJvJ5OTo8H5fjNWULsaD74jCuzTfwMlUXRs6N8d3/pq/2Uuf9dC79ELBbrSs3IhR+ l4HaoY8ClGrfxiE5EMnx9u5sdq8dv40DCnWmSqAxRzTBnTIKDLvH8FtiwDVEb0F1WoY2 g3jigmlP4hvwlLnAwZIDxCLWuh/7rTcodSoxf3JgXivwCrxFC56J2iohQ70ts9HN50LZ WH+O95DX0dp19EJ6FhO7XMHUhRnibhURPwNsvgKJ+KGvhrnE6UNKwgE5wgoyk7M0xg62 E7oA== X-Gm-Message-State: APjAAAW5lzUzumEWS/78EiZTIxYXPEbBUwA4Hm9UTmD6v72021klbw7t YSDwAG2jpPyhgh9jGqT17hCWAf37xvUwdyE3QjiseBzfV5s= X-Google-Smtp-Source: APXvYqxmIzQSkRDBZRpjiPHXLCH+g8uUz6skNZWuU+nyxO555/wA5AN6JTm8UefZrsQfTBtgLIdt59U4TIhbf7VQchI= X-Received: by 2002:a63:7146:: with SMTP id b6mr630948pgn.426.1557418005508; Thu, 09 May 2019 09:06:45 -0700 (PDT) MIME-Version: 1.0 Date: Thu, 9 May 2019 17:06:34 +0100 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: JSON_THROW_ON_ERROR implementation detail From: Danack@basereality.com (Dan Ackroyd) Hi, Apparently there is an implementation detail in JSON_THROW_ON_ERROR that differs in the RFC text, from the discussion on list http://news.php.net/php.internals/100569: > 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. RFC text: > When passed this flag, the error behaviour of these functions is changed. > The global error state is left untouched, and if an error occurs that would > otherwise set it, these functions instead throw a JsonException with the > message and code set to whatever json_last_error() and json_last_error_msg() > would otherwise be respectively. The implementation is highly surprising and can lead to bugs. Imagine this code exists in a code base. --------------------- $foo = json_decode('[bar'); // many lines of code. // many lines of code. // many lines of code. $bar = json_encode('Hello world!'); if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception("encoding went wrong!"); } echo "All is fine"; // output is "All is fine" --------------------- And to start moving to have json_encode throw on error, they start using the flag on the json_encode ---------------------- $foo = json_decode('[bar'); // many lines of code. // many lines of code. // many lines of code. $bar = json_encode('Hello world!', JSON_THROW_ON_ERROR); if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception("encoding went wrong!"); } echo "All is fine"; ------------------- The result of this is that the user throw exception will be thrown even though the encode worked correctly. https://3v4l.org/JJD5g This is highly surprising*, and doesn't seem the right thing to be doing. Does anyone have any objection to changing this, so that json_last_error() + json_last_error_msg() always refer to the most recent json_encode()/json_decode() function, as per the discussion. Or does this need an RFC? I've logged a bug for this regardless - https://bugs.php.net/bug.php?id=77997 cheers Dan Ack * https://en.wikipedia.org/wiki/Principle_of_least_astonishment