From aaa9115cfd4096a4fb6cf0ffbc2c07c75ed3a751 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Wed, 22 Mar 2017 16:34:15 -0300 Subject: [feature] use 409 status code for existing blob id Raising was generating 500, which is a generic status code for server side errors. This commit adds proper status code of 409 while handling the error on client side by translating the code into a proper exception class. --- client/src/leap/soledad/client/_blobs.py | 14 +++++++++++++- client/src/leap/soledad/client/_http.py | 3 +-- 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'client') diff --git a/client/src/leap/soledad/client/_blobs.py b/client/src/leap/soledad/client/_blobs.py index eb09d69f..0d25702c 100644 --- a/client/src/leap/soledad/client/_blobs.py +++ b/client/src/leap/soledad/client/_blobs.py @@ -36,6 +36,7 @@ import treq from leap.soledad.client.sqlcipher import SQLCipherOptions from leap.soledad.client import pragmas +from leap.soledad.common.errors import SoledadError from _crypto import DocInfo, BlobEncryptor, BlobDecryptor from _http import HTTPClient @@ -44,6 +45,10 @@ from _http import HTTPClient logger = Logger() +class BlobAlreadyExistsError(SoledadError): + pass + + class ConnectionPool(adbapi.ConnectionPool): def insertAndGetLastRowid(self, *args, **kwargs): @@ -90,6 +95,13 @@ class ConnectionPool(adbapi.ConnectionPool): return handle +def check_http_status(code): + if code == 409: + raise BlobAlreadyExistsError() + elif code != 200: + raise SoledadError("Server Error") + + class DecrypterBuffer(object): def __init__(self, doc_id, rev, secret, tag): @@ -217,7 +229,7 @@ class BlobManager(object): armor=False) fd = yield crypter.encrypt() response = yield self._client.put(uri, data=fd) - assert response.code == 200 + check_http_status(response.code) logger.info("Finished upload: %s" % (blob_id,)) @defer.inlineCallbacks diff --git a/client/src/leap/soledad/client/_http.py b/client/src/leap/soledad/client/_http.py index ba6f9a27..2a6b9e39 100644 --- a/client/src/leap/soledad/client/_http.py +++ b/client/src/leap/soledad/client/_http.py @@ -67,8 +67,7 @@ class PinnedTokenAgent(Agent): def request(self, method, uri, headers=None, bodyProducer=None): # authenticate the request - if not headers: - headers = Headers() + headers = headers or Headers() headers.addRawHeader('Authorization', self._creds) # perform the authenticated request return Agent.request( -- cgit v1.2.3