Skip to content

Commit

Permalink
#16 - Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gfolego committed Oct 14, 2017
1 parent 4d6f264 commit aa3035e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
34 changes: 30 additions & 4 deletions faas/faas/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from process import pipeline, FMT_STR

import os
from flask import request, redirect, send_from_directory, after_this_request, render_template
from flask import request, redirect, send_from_directory, after_this_request, render_template, jsonify
from werkzeug.utils import secure_filename

import tempfile
Expand Down Expand Up @@ -86,9 +86,12 @@ def upload_file():

srcpath = os.path.dirname(os.path.realpath(__file__))

# Ready to accept new arguments
options = parse_form(request.form)
pipeline(infile, outfile, **options)
# Ready to accept new arguments
try:
options = parse_form(request.form)
pipeline(infile, outfile, **options)
except Exception as e:
raise InvalidUsage(str(e), 400)

@after_this_request
def cleanup(response):
Expand All @@ -102,3 +105,26 @@ def cleanup(response):
def web_interface():
return render_template('index.html')



class InvalidUsage(Exception):
status_code = 400

def __init__(self, message, status_code=None, payload=None):
Exception.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload

def to_dict(self):
rv = dict(self.payload or ())
rv['error'] = self.message
return rv

@app.errorhandler(InvalidUsage)
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response

12 changes: 10 additions & 2 deletions faas/faas/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ def find_positions(infile):
shell=True).rstrip('\n').splitlines()

pos = [float(i) for i in pos]

if len(pos) == 0:
raise RuntimeError('Error processing PDF')

return pos


Expand All @@ -210,14 +214,18 @@ def pipeline(infile, outfile,
# Convert PDF to PS
ps_infile = os.path.join(os.path.dirname(outfile), 'infile.ps')
ps_outfile = os.path.join(os.path.dirname(outfile), 'outfile.ps')
subprocess.call(['pdf2ps', infile, ps_infile])
out = subprocess.check_output(['pdf2ps', infile, ps_infile], stderr=subprocess.STDOUT)
if out != "":
raise RuntimeError('Error processing PDF')

# generate a new PostScript file
process_ps(ps_infile, ps_outfile, slots,
start, end, variation, dashed, debug)

# Convert PS to PDF
subprocess.call(['ps2pdf', ps_outfile, outfile])
out = subprocess.check_output(['ps2pdf', ps_outfile, outfile], stderr=subprocess.STDOUT)
if out != "":
raise RuntimeError('Error processing PDF')

# Cleanup
os.unlink(ps_infile)
Expand Down

0 comments on commit aa3035e

Please sign in to comment.