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

Update NXOS response processing for more informative error messaging #1461

Merged
merged 5 commits into from
Feb 12, 2022

Conversation

dkaplan1
Copy link
Contributor

@dkaplan1 dkaplan1 commented Jun 8, 2021

This PR addresses improvements that can be made to how nxapi_plumbing processes JSON-RPC responses. The issue stems from different behavior in JSON-RPC vs XML responses, specifically return codes. JSON-RPC returns a 500 for an erroneous command, whereas XML returns a 200.

JSON

(Pdb) print(response)
<Response [500]>
(Pdb) print(response.json())
{'jsonrpc': '2.0', 'error': {'code': -32602, 'message': 'Invalid params', 'data': {'msg': 'SNMP community entry not found.\n'}}, 'id': 1}

XML

(Pdb) print(response)
<Response [200]>
(Pdb) print(response.text)
<?xml version="1.0"?>
<ins_api>
  <type>cli_conf</type>
  <version>1.0</version>
  <sid>eoc</sid>
  <outputs>
    <output>
      <clierror>SNMP community entry not found.
</clierror>
      <code>400</code>
      <msg>CLI execution error</msg>
    </output>
  </outputs>
</ins_api>

This means that erroneous commands using JSON-RPC will always raise NXAPIPostError with a generic error message including all commands with no indication of the underlying issue.

if response.status_code not in [200]:
msg = """Invalid status code returned on NX-API POST
commands: {}
status_code: {}""".format(
commands, response.status_code
)
raise NXAPIPostError(msg)

napalm.nxapi_plumbing.errors.NXAPIPostError: Invalid status code returned on NX-API POST
commands: [...]
status_code: 500

If we move non-200 response raising to XMLClient's _process_api_response, erroneous JSON-RPC requests will be returned and passed to _error_check, which will give better insight into what the problematic command was and information on the issue.

napalm.nxapi_plumbing.errors.NXAPICommandError: The command "..." gave the error "..."

In order to test this I had to make a few changes to plumb exposure of the status code.

Also, while going through the NXOS tests I noticed that we weren't raising when mocked_data included exception directives, so things like

{
"exception": "nxapi_plumbing.api_client.NXAPIPostError"
}
were not working as expected.

@mirceaulinic mirceaulinic added this to the 3.4.0 milestone Feb 12, 2022
@mirceaulinic mirceaulinic merged commit e331a1f into napalm-automation:develop Feb 12, 2022
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.

None yet

2 participants