back-end error logging

This commit is contained in:
Davide Alberani 2018-12-12 21:32:25 +01:00
parent 3f31fbe67d
commit 7cec835d88
2 changed files with 24 additions and 9 deletions

View file

@ -86,7 +86,7 @@ function sendData(data) {
console.log(msg);
M.toast({"html": msg});
} else {
msg = "something wrong on the backend";
msg = "😭 😭 😭 something wrong on the backend 😭 😭 😭";
console.log(msg);
M.toast({"html": msg});
msg = "the server says: " + json.message;

View file

@ -34,8 +34,9 @@ API_VERSION = '1.0'
class Socialite:
def __init__(self, options):
def __init__(self, options, logger=None):
self.options = options
self.logger = logger
self.init()
with_mastodon = property(lambda self: self.options.mastodon_token and
@ -53,14 +54,25 @@ class Socialite:
api_base_url=self.options.mastodon_api_url)
def post_image(self, img, mime_type='image/jpeg', message=None, description=None):
errors = []
if message is None:
message = self.options.default_message
if description is None:
description = self.options.default_image_description
if self.with_store:
self.store_image(img, mime_type, message, description)
try:
self.store_image(img, mime_type, message, description)
except Exception as e:
errors.append(str(e))
if self.with_mastodon:
self.mastodon_post_image(img, mime_type, message, description)
try:
self.mastodon_post_image(img, mime_type, message, description)
except Exception as e:
errors.append(str(e))
if errors and self.logger:
for err in errors:
self.logger.error("ERROR: %s" % err)
return errors
def mastodon_post_image(self, img, mime_type, message, description):
mdict = self.mastodon.media_post(media_file=img, mime_type=mime_type, description=description)
@ -75,7 +87,6 @@ class Socialite:
suffix = '.' + ms[1]
prefix = str(datetime.datetime.now()).replace(' ', 'T') + '-'
fd, fname = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=self.options.store_dir)
print(fd, fname)
os.write(fd, img)
os.close(fd)
txt_fname = '%s.info' % fname
@ -168,9 +179,13 @@ class PublishHandler(BaseHandler):
body = info['body']
b64_image = body.split(b',')[1]
image = base64.decodestring(b64_image)
with open('/tmp/selfie.jpeg', 'wb') as fd:
fd.write(image)
self.socialite.post_image(image)
try:
errors = self.socialite.post_image(image)
if errors:
reply['success'] = False
reply['message'] = '<br>\n'.join(errors)
except Exception as e:
reply = {'success': False, 'message': 'something wrong sharing the image'}
self.write(reply)
@ -208,7 +223,7 @@ def run():
if os.path.isfile(options.ssl_key) and os.path.isfile(options.ssl_cert):
ssl_options = dict(certfile=options.ssl_cert, keyfile=options.ssl_key)
socialite = Socialite(options)
socialite = Socialite(options, logger=logger)
init_params = dict(global_options=options, socialite=socialite)
_publish_path = r"/publish/?"