rbw / pysnow

ServiceNow API Client Library
MIT License
203 stars 89 forks source link

more detailed error messages on POST operations #12

Closed sandwormusmc closed 7 years ago

sandwormusmc commented 7 years ago

Currently the code seems to perform a POST operation, and only check the HTTP response code. In Request::_get_content, moving up the content_json retrieval and checking for the existence of error, we can get more detailed errors as well, pointing out data formatting errors and column requirements and so on.

These kind of things are probably second nature for others with extensive experience, but for a beginner like me it was not apparent what I was doing wrong, until I checked the API Explorer and realized there were more detailed errors available in the response.

Maybe some additional error checking is necessary, for existence of content_json['error']['message'] and detail, I'm not sure though...

246a247,248
>         content_json = response.json()
> 
255,257c257,262
<             raise UnexpectedResponse("Unexpected HTTP response code. Expected: 201, got %d" % response.status_code)
< 
<         content_json = response.json()

>       if 'error' in content_json:
>                 raise UnexpectedResponse("Unexpected HTTP response code. Expected: 201, got %d. \nError: %s\nDetails: %s" % (response.status_code,
>                                                                content_json['error']['message'],
>                                                                content_json['error']['detail']))
>       else:
>                 raise UnexpectedResponse("Unexpected HTTP response code. Expected: 201, got %d." % response.status_code)
rbw commented 7 years ago

Hello!

Are you getting a 400 back? Do you have an example?

I'm pretty sure the SN REST API previously, when this library was first built, sent a 2xx response back upon those types of errors (bad request / malformed body), instead of a 400, and included the error in the OK-response.

Perhaps this behavior has changed in more recent versions of the SN API. The documentation still doesn't mention anything about 400 errors on POST requests, as far as I can see.

Anyway - I'll do some testing and look into this. Thanks for reporting :)

sandwormusmc commented 7 years ago

Hey there. I was kind of stumbling in the dark without knowing the data policy for the payload, so I was getting quite a few of these errors in my initial attempts, before I got access to their API Explorer and realized there was more to the error than I originally thought.

To start with, I didn't know what was necessary in the payload to open an incident in ServiceNow. After some poking, I was just getting back the default errors from the API about unexpected response codes (403 in my case).

As an example, if I comment out some required values from my payload, with the updates I mentioned above I get some useful details back, rather than just the UnexpectedResponse message.

With the script as is, you can possibly reproduce the error by calling insert(table='incident',payload=payloadWithMissingRequiredData) and get a 403 back.

In my case, the following are required fields to open an incident:

category description caller_id u_impacted_service cmdb_ci subcategory impact severity contact_type priority location assignment_group short_description urgency u_affected_user

Here is the output when I include all of the required fields:

[me@somewhere ~]$ python openone.py 
{'impact': 3, 'subcategory': 'Service Request', 'caller_id': u'33115a0d3d2d0600f4a09f32609319a2', 'u_impacted_service': u'1b66b8ed6f0a46007dce01dfde3ee4a0', 'cmdb_ci': u'1b66b8ed6f0a46007dce01dfde3ee4a0', 'description': 'A much longer description', 'category': 'Inquiry/Help', 'severity': 3, 'contact_type': 'Other', 'priority': 3, 'location': u'4e472b3f6f6102007dce01dfde3ee4cd', 'assignment_group': u'630c048e6ff94200cec3f941be3ee4c2', 'short_description': 'some request from someone with some team', 'urgency': 3, 'u_affected_user': u'33115a0d3d2d0600f4a09f32609319a2'}
INC0697332

And below is the output when I omit short description and affected user. Without the updates above all I get is the Expected: 201, got 403.

[me@somewhere ~]$ python openone.py 
{'impact': 3, 'subcategory': 'Service Request', 'caller_id': u'33115a0d3d2d0600f4a09f32609319a2', 'u_impacted_service': u'1b66b8ed6f0a46007dce01dfde3ee4a0', 'cmdb_ci': u'1b66b8ed6f0a46007dce01dfde3ee4a0', 'description': 'A much longer description', 'category': 'Inquiry/Help', 'severity': 3, 'contact_type': 'Other', 'priority': 3, 'location': u'4e472b3f6f6102007dce01dfde3ee4cd', 'assignment_group': u'630c048e6ff94200cec3f941be3ee4c2'}
Traceback (most recent call last):
  File "openone.py", line 53, in <module>
    response = snobj.insert(table='incident',payload=ourpayload)
  File "/usr/lib/python2.7/site-packages/pysnow.py", line 99, in insert
    return r.insert(payload)
  File "/usr/lib/python2.7/site-packages/pysnow.py", line 165, in insert
    return self._get_content(response)   # @TODO - update to return first key (API breakage)
  File "/usr/lib/python2.7/site-packages/pysnow.py", line 260, in _get_content
    content_json['error']['detail']))
pysnow.UnexpectedResponse: Unexpected HTTP response code. Expected: 201, got 403. 
Error: Operation Failed
Details: Data Policy Exception:  Short description is mandatory, Affected User is mandatory
rbw commented 7 years ago

I've been away for awhile but now looking into this again. And thanks for the really good input. I'll have this implemented asap.

sandwormusmc commented 7 years ago

No worries, thanks for following up! So far we're still using pysnow, and it's working well with the additional workaround for what you'll be updating. I'll keep an eye out for updates.

On Thu, Feb 23, 2017 at 9:40 AM Robert Wikman notifications@github.com wrote:

I've been away for awhile but now looking into this again. And thanks for the really good input. I'll have this implemented asap.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rbw0/pysnow/issues/12#issuecomment-282008569, or mute the thread https://github.com/notifications/unsubscribe-auth/ADrXr2oYf1LePZ_TDg6bapMopZvswrTmks5rfZpggaJpZM4Lb3dC .

rbw commented 7 years ago

Implemented in d1131373769281dd08a379954fe02b8349ae48c5 , which will be released in version 0.2.2.

message and detail will be accessible in the UnexpectedResponse exception object using error_summary and error_details.

Also, documentation and examples coming up.

rbw commented 7 years ago

Fixed.