Skip to content

Commit bc44eca

Browse files
committed
exception: handle duplicate keywords in YOURLS 1.9
- API returns HTTP 400 so we take a different code path.
1 parent 7168455 commit bc44eca

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

yourls/data.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __eq__(self, other):
100100
return NotImplemented
101101

102102

103-
def _handle_api_error_with_json(http_exc, jsondata, response):
103+
def _handle_api_error_with_json(http_exc, jsondata, response, request_data):
104104
"""Handle YOURLS API errors.
105105
106106
requests' raise_for_status doesn't show the user the YOURLS json response,
@@ -110,7 +110,11 @@ def _handle_api_error_with_json(http_exc, jsondata, response):
110110
code = jsondata['code']
111111
message = jsondata['message']
112112

113-
if code == 'error:noloop':
113+
if code == 'error:keyword':
114+
# In YOURLS 1.9 or later, we get HTTP 400, so we should handle duplicate
115+
# keyword here.
116+
raise YOURLSKeywordExistsError(message, keyword=request_data['keyword'])
117+
elif code == 'error:noloop':
114118
raise YOURLSNoLoopError(message, response=response)
115119
elif code == 'error:nourl':
116120
raise YOURLSNoURLError(message, response=response)
@@ -142,7 +146,7 @@ def _validate_yourls_response(response, data):
142146
else:
143147
logger.debug('Received error {response} with JSON {json}',
144148
response=response, json=jsondata)
145-
_handle_api_error_with_json(http_exc, jsondata, response)
149+
_handle_api_error_with_json(http_exc, jsondata, response, data)
146150

147151
if reraise:
148152
six.reraise(*http_error_info)

0 commit comments

Comments
 (0)