Commit e522b32db540ab60301a2257a74d127934754fa0

Better exception handling.
timApp/ephemeralclient.py
(6 / 0)
  
88
99new_contract('bytes', bytes)
1010
11class EphemeralException(Exception):
12 pass
13
1114class EphemeralClient(object):
1215
1316 @contract
9797 req = urllib.request.Request(url=self.server_path + '/json-html/{}'.format(document_id), method='GET')
9898 response = urllib.request.urlopen(req)
9999 responseStr = str(response.read(), encoding='utf-8')
100 print(responseStr)
101 if responseStr == '{"Error":"No doc found"}':
102 raise EphemeralException('No document found with id %d' % document_id)
100103 return json.loads(responseStr)
101104
102105 @contract
timApp/tim.py
(7 / 3)
  
99import os
1010from containerLink import callPlugin
1111from werkzeug.utils import secure_filename
12from timdb.timdb2 import TimDb
12from timdb.timdb2 import TimDb, TimDbException
1313from flask import Response
1414
1515app = Flask(__name__)
8383 timdb = getTimDb()
8484 try:
8585 blocks = timdb.getDocumentAsHtmlBlocks(int(doc_id))
86 return json.dumps(blocks)
86 doc = timdb.getDocument(int(doc_id))
87 return jsonify({"name" : doc['name'], "text" : blocks})
8788 except ValueError as err:
8889 print(err)
8990 return "[]"
91 except TimDbException as err:
92 print(err)
93 return "[]"
9094
9195@app.route("/postParagraph/", methods=['POST'])
9296def postParagraph():
157157 timdb = getTimDb()
158158 try:
159159 #notes = []
160 notes = timdb.getNotes(0, doc_id) #TODO: Needs timdb2
160 notes = timdb.getNotes(0, int(doc_id)) #TODO: Needs timdb2
161161 return json.dumps(notes)
162162 except ValueError:
163163 return redirect(url_for('goat'))
timApp/timdb/timdb2.py
(11 / 2)
  
1515import gitpylib.sync
1616import gitpylib.file
1717import gitpylib.common
18from ephemeralclient import EphemeralClient
18from ephemeralclient import EphemeralClient, EphemeralException
1919import collections
2020
21class TimDbException(Exception):
22 pass
23
2124BLOCKTYPES=collections.namedtuple('blocktypes', ('DOCUMENT', 'COMMENT', 'NOTE', 'ANSWER', 'IMAGE'))
2225blocktypes=BLOCKTYPES(0,1,2,3,4)
2326# import timeit
392392 """Gets the specified document in HTML form."""
393393
394394 ec = EphemeralClient(EPHEMERAL_URL)
395 return ec.getDocumentAsHtmlBlocks(document_id)
395
396 try:
397 blocks = ec.getDocumentAsHtmlBlocks(document_id)
398 except EphemeralException as e:
399 raise TimDbException(str(e))
400
401 return blocks
396402
397403 @contract
398404 def getDocumentPath(self, document_id : 'int') -> 'str':