Skip to content

Handling Errors

Adrian edited this page Apr 1, 2021 · 1 revision

JsonException

Encoding or decoding errors throw PHP's native JsonException. See the PHP Manual for more information. For reference, possible error codes are:

  • JSON_ERROR_NONE: No error has occurred
  • JSON_ERROR_DEPTH: The maximum stack depth has been exceeded
  • JSON_ERROR_STATE_MISMATCH: Invalid or malformed JSON
  • JSON_ERROR_CTRL_CHAR: Control character error, possibly incorrectly encoded
  • JSON_ERROR_SYNTAX: Syntax error
  • JSON_ERROR_UTF8: Malformed UTF-8 characters, possibly incorrectly encoded
  • JSON_ERROR_RECURSION: One or more recursive references in the value to be encoded
  • JSON_ERROR_INF_OR_NAN: One or more NAN or INF values in the value to be encoded
  • JSON_ERROR_UNSUPPORTED_TYPE: A value of a type that cannot be encoded was given
  • JSON_ERROR_INVALID_PROPERTY_NAME: A property name that cannot be encoded was given
  • JSON_ERROR_UTF16: Malformed UTF-16 characters, possibly incorrectly encoded

InvalidArgumentException

When attempting to set an option to an invalid value, an InvalidArgumentException will be thrown. This exception extends from PHP's native InvalidArgumentException class and implements the Exceptable interface. Possible error codes are:

  • INVALID_ASSOC: The Json::ASSOC option must be boolean
  • INVALID_DEPTH: The Json::DEPTH option must be a non-negative integer
  • INVALID_DECODE_FLAGS: The Json::DECODE_FLAGS option must be a non-negative integer
  • INVALID_ENCODE_FLAGS: The Json::ENCODE_FLAGS option must be a non-negative integer

An InvalidArgumentException will also be thrown when attempting to encode a non-json-able object with the $strict flag set. In this case, the InvalidArgumentException will not be thrown directly, but will be set as the $previous exception on a JsonException with the code JSON_ERROR_UNSUPPORTED_TYPE.

  • UNSUPPORTED_TYPE: Objects other than stdClass that do not implement JsonSerializable cannot be encoded under "strict" mode
Clone this wiki locally