cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
553
Views
1
Helpful
2
Replies

signalling error from a callback as return vs throwing an exception

atreyulovesyou
Level 1
Level 1

petty greivance if that's the accepted means, but here a thing...

my team has a pattern of indicating a failure state by raising exception that isn't caught, flows out to the ncs wrapper running a given callback, which causes whatever message put upon the exception to output in the cli (and I presume be included in a response body or error field via restconf, dunno yet).

Question: is there a way to SIGNAL to ncs that an error state was found - and to return with a message in that state, versus throwing an exception at the sky the way we currently do? 

I can't find anything about such, so it might be the recommended way to just to throw. 

if so, I'll probably rework the returns to something that can assure consistency in formatting across codebases and then throw that... but I'd rather tell the user 'no, logic error, details' in a manner that does not involve creating an exception class instance and inducing crash-like perspective.   I want to be able to see instances of exception of type as valid of system in failure, versus user input of logical error.  i suppose I can type them differently.... but I'm going in circles... 

can you signal and return from a callback explicitly (no, i failed, here's why, details. return that to the user with status code 123??) - rather than invoking language panic/stacktrace gather/otherwise expensive behaviors?

1 Accepted Solution

Accepted Solutions

huayyang
Cisco Employee
Cisco Employee

some simple testing: with examples.ncs/getting-started/developing-with-ncs/2-actions-py, adding the error reporting in handling of /action-test/system/restart:

...

elif name == ns.a_restart_:
mode = input.mode
self.log.debug("action: restart mode={0}".format(mode))
if input.debug:
_ncs.dp.action_seterr_extended(uinfo, _ncs.ERRCODE_APPLICATION,
0, 0, "Description of what went wrong")
return _ncs.CONFD_ERR
output.time = time.strftime("%H:%M:%S")
else:
self.log.debug("got bad operation: {0}".format(name))
return _ncs.CONFD_ERR

and run the action in cli:

admin@ncs> request action-test system restart mode blah debug
Error: Description of what went wrong

View solution in original post

2 Replies 2

huayyang
Cisco Employee
Cisco Employee

I haven't tested this using pyapi, but what's described in "EXTENDED ERROR REPORTING" in https://developer.cisco.com/docs/nso/guides/#!ncs-man-pages-volume-3/man.3.confd_lib_lib.extended_error_reporting is exposed to the low-level pyapi in _ncs.dp, is this something you're looking for?

huayyang
Cisco Employee
Cisco Employee

some simple testing: with examples.ncs/getting-started/developing-with-ncs/2-actions-py, adding the error reporting in handling of /action-test/system/restart:

...

elif name == ns.a_restart_:
mode = input.mode
self.log.debug("action: restart mode={0}".format(mode))
if input.debug:
_ncs.dp.action_seterr_extended(uinfo, _ncs.ERRCODE_APPLICATION,
0, 0, "Description of what went wrong")
return _ncs.CONFD_ERR
output.time = time.strftime("%H:%M:%S")
else:
self.log.debug("got bad operation: {0}".format(name))
return _ncs.CONFD_ERR

and run the action in cli:

admin@ncs> request action-test system restart mode blah debug
Error: Description of what went wrong