Skip to content
This repository was archived by the owner on May 21, 2020. It is now read-only.

Commit

Permalink
provide a HTTP 409 error code on conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Söndergaard committed Mar 22, 2015
1 parent 3e8068f commit f755b41
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions couch/couch.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,16 @@ def _parse_response(self, resp):
# decode the JSON body and check for errors
obj = json_decode(resp.body)

def to_code(err):
return {'not_found': 404, 'conflict': 409}.get(err, 400)

if isinstance(obj, list):
# check if there is an error in the list of dicts,
# raise the first error seen
for item in obj:
if 'error' in item:
raise relax_exception(httpclient.HTTPError(
resp.code if item['error'] != 'not_found' else 404,
item['reason'], resp))
to_code(item['error']), item['reason'], resp))

elif 'error' in obj:
raise relax_exception(httpclient.HTTPError(
Expand All @@ -428,8 +430,7 @@ def _parse_response(self, resp):
for row in obj['rows']:
if 'error' in row:
raise relax_exception(httpclient.HTTPError(
resp.code if row['error'] != 'not_found' else 404,
row['error'], resp))
to_code(row['error']), row['error'], resp))
return obj

def _parse_headers(self, resp):
Expand Down

0 comments on commit f755b41

Please sign in to comment.