TWSD-1065 - Fatal error while accessing AG webui || UPSDC|| AS-24417

Review Request #1222 — Created Dec. 11, 2025 and submitted

shuinvy
AG
rel_ag_9_4_5
TWSD-1065
austin, evalin, jasonchou, lucille, milliechou, peteryeh

Prevent exception cause error message for json string parsing

Ticket Link: https://arraynetworks.atlassian.net/browse/TWSD-1065
The root cause is our json parsing failed, so we output error message in the web page.
It is difficult to reproduce the issue, but I guess there is special character return from CLI due to different environment of the device.
So I do some changes for exception preventing.
Besides,
As the result:
Fatal error executing command: ; The abnormal response is "{"content":["Enable password:"],"result":1,"reason":3}{"content":[],"result":1,"reason":0}"
There are two json string,
Fist is:
{"content":["Enable password:"],"result":1,"reason":3}
Second is:
{"content":[],"result":1,"reason":0}
The PHP may think the second one is invalid json string because content is empty array [].
Thus, we find the last valid JSON string and return it.
Otherwise, print error message.
In this case,
because the string:
{"content":["Enable password:"],"result":1,"reason":3}
is valid string,
and {"content":[],"result":1,"reason":0} will be skip (continue),
it will return {"content":["Enable password:"],"result":1,"reason":3} after this code changes.

To sum up the changes:
1. add static to function exec because we use cli::exec (static will let you don't need to create instance such as $cli = new cli(); $cli->exec("enable");)
2. add static to function check_multiple_json_format because we use self::check_multiple_json_format (static will let you don't need to create instance such as $cli = new cli(); $cli->exec("enable");)
3. Enhancement the function check_multiple_json_format

For enhancement the function check_multiple_json_format,
What I do is:
1. Remove special characters of $output first (Such as BOM header, control characters or $f_eop as the CLI end character)
2. If $output is empty, then return (Default format)
3. We manually parse the JSON string from the $output, because there may not only one JSON string in $output, then put JSON string in the array $jsons. For example,
$jsons = array('{"content":["Enable password:"],"result":1,"reason":3}', '{"content":[],"result":1,"reason":0}');
That is:
$jsons[0] = '{"content":["Enable password:"],"result":1,"reason":3}';
$jsons[1] = '{"content":[],"result":1,"reason":0}';
So if $jsons is empty, then we just return(Default format)
4. Then we try to get last JSON string(That is, there should be at least one valid JSON string in $output).
The function json_decode() will return null if the given parameter cannot be parsed to JSON string, and it won't raise exception. so we check "is null" and use json_last_error function to see the error message. There are many result of json_last_error, such as JSON_ERROR_NONE, JSON_ERROR_DEPTH, JSON_ERROR_STATE_MISMATCH (Both are constant variable).
While JSON_ERROR_NONE means "There is no error occurred", that means the string can be parsed successfully. That is, we should skip result of json_last_error is not JSON_ERROR_NONE.
5. If the last valid JSON string is null (initial value), then we print the error message(as original operation).
6. Return the last valid JSON string.
7. Add function to handle default format to return.
The default format of response is like:
$response = array("content": array(), "result": 0, "reason": -1);
and it should be object,
so we can check result like this:
$result = cli::exec("show version"); if (count($result->content) > 0 && $result->result == 1 && $result->reason == 0) { // The CLI is correct. }

You can check attachment for the flowchart of the function


shuinvy
shuinvy
jasonchou
  1. Ship It!
  2. 
      
austin
  1. Ship It!
  2. 
      
milliechou
  1. Ship It!
  2. 
      
shuinvy
Review request changed

Status: Closed (submitted)

Loading...