Flask custom error handling for upload function
I have developed an upload form to take specific .xlsx file as upload. The requirement is to handle any exceptions for upload of non xlsx (for e.g. zip, exe file). I am using pyexcel library for reading the upload. I tried creating following code to handle this exception:
enter image description here
enter image description here
The error handling code is as follows:
class FILE_TYPE_NOT_SUPPORTED_FMT(Exception):
pass
@app.errorhandler(FILE_TYPE_NOT_SUPPORTED_FMT)
def custom_handler(errrors):
app.logger.error('Unhandled Exception: %s', (errrors))
return render_template('400.html'), 400
and the upload code is as follows:
@users.route("/oisdate_upload", methods=['GET', 'POST'])
@login_required
def doimport_ois_date():
msg=None
if request.method == 'POST':
def OIS_date_init_func(row):
#c.id = row['id']
c = Ois_date(row['date'],row['on'],row['m1'],row['m2'],row['m3'],row['m6'],row['m9'],row['y1'],row['y2'],row['y3'],row['y4'],row['y5'],row['y7'],row['y10'])
return c
request.save_book_to_database(
field_name='file', session=db.session,
tables=[Ois_date],
initializers=[OIS_date_init_func])
msg = "Successfully uploaded"
#return redirect(url_for('users.doimport_ois_date'), code=302)
if((Ois_date.query.order_by(Ois_date.date.desc()).first()) is not None):
date_query = Ois_date.query.order_by(Ois_date.date.desc()).first()
start_date = date_query.date
date_query1 = Ois_date.query.order_by(Ois_date.date.asc()).first()
end_date = date_query1.date
return render_template('OISdate_upload.html',msg=msg, start_date=start_date,end_date=end_date)
I am unable to figure out how to correctly capture the error and handle it, any feedback would be appreciated.
flask error-handling pyexcel
add a comment |
I have developed an upload form to take specific .xlsx file as upload. The requirement is to handle any exceptions for upload of non xlsx (for e.g. zip, exe file). I am using pyexcel library for reading the upload. I tried creating following code to handle this exception:
enter image description here
enter image description here
The error handling code is as follows:
class FILE_TYPE_NOT_SUPPORTED_FMT(Exception):
pass
@app.errorhandler(FILE_TYPE_NOT_SUPPORTED_FMT)
def custom_handler(errrors):
app.logger.error('Unhandled Exception: %s', (errrors))
return render_template('400.html'), 400
and the upload code is as follows:
@users.route("/oisdate_upload", methods=['GET', 'POST'])
@login_required
def doimport_ois_date():
msg=None
if request.method == 'POST':
def OIS_date_init_func(row):
#c.id = row['id']
c = Ois_date(row['date'],row['on'],row['m1'],row['m2'],row['m3'],row['m6'],row['m9'],row['y1'],row['y2'],row['y3'],row['y4'],row['y5'],row['y7'],row['y10'])
return c
request.save_book_to_database(
field_name='file', session=db.session,
tables=[Ois_date],
initializers=[OIS_date_init_func])
msg = "Successfully uploaded"
#return redirect(url_for('users.doimport_ois_date'), code=302)
if((Ois_date.query.order_by(Ois_date.date.desc()).first()) is not None):
date_query = Ois_date.query.order_by(Ois_date.date.desc()).first()
start_date = date_query.date
date_query1 = Ois_date.query.order_by(Ois_date.date.asc()).first()
end_date = date_query1.date
return render_template('OISdate_upload.html',msg=msg, start_date=start_date,end_date=end_date)
I am unable to figure out how to correctly capture the error and handle it, any feedback would be appreciated.
flask error-handling pyexcel
add a comment |
I have developed an upload form to take specific .xlsx file as upload. The requirement is to handle any exceptions for upload of non xlsx (for e.g. zip, exe file). I am using pyexcel library for reading the upload. I tried creating following code to handle this exception:
enter image description here
enter image description here
The error handling code is as follows:
class FILE_TYPE_NOT_SUPPORTED_FMT(Exception):
pass
@app.errorhandler(FILE_TYPE_NOT_SUPPORTED_FMT)
def custom_handler(errrors):
app.logger.error('Unhandled Exception: %s', (errrors))
return render_template('400.html'), 400
and the upload code is as follows:
@users.route("/oisdate_upload", methods=['GET', 'POST'])
@login_required
def doimport_ois_date():
msg=None
if request.method == 'POST':
def OIS_date_init_func(row):
#c.id = row['id']
c = Ois_date(row['date'],row['on'],row['m1'],row['m2'],row['m3'],row['m6'],row['m9'],row['y1'],row['y2'],row['y3'],row['y4'],row['y5'],row['y7'],row['y10'])
return c
request.save_book_to_database(
field_name='file', session=db.session,
tables=[Ois_date],
initializers=[OIS_date_init_func])
msg = "Successfully uploaded"
#return redirect(url_for('users.doimport_ois_date'), code=302)
if((Ois_date.query.order_by(Ois_date.date.desc()).first()) is not None):
date_query = Ois_date.query.order_by(Ois_date.date.desc()).first()
start_date = date_query.date
date_query1 = Ois_date.query.order_by(Ois_date.date.asc()).first()
end_date = date_query1.date
return render_template('OISdate_upload.html',msg=msg, start_date=start_date,end_date=end_date)
I am unable to figure out how to correctly capture the error and handle it, any feedback would be appreciated.
flask error-handling pyexcel
I have developed an upload form to take specific .xlsx file as upload. The requirement is to handle any exceptions for upload of non xlsx (for e.g. zip, exe file). I am using pyexcel library for reading the upload. I tried creating following code to handle this exception:
enter image description here
enter image description here
The error handling code is as follows:
class FILE_TYPE_NOT_SUPPORTED_FMT(Exception):
pass
@app.errorhandler(FILE_TYPE_NOT_SUPPORTED_FMT)
def custom_handler(errrors):
app.logger.error('Unhandled Exception: %s', (errrors))
return render_template('400.html'), 400
and the upload code is as follows:
@users.route("/oisdate_upload", methods=['GET', 'POST'])
@login_required
def doimport_ois_date():
msg=None
if request.method == 'POST':
def OIS_date_init_func(row):
#c.id = row['id']
c = Ois_date(row['date'],row['on'],row['m1'],row['m2'],row['m3'],row['m6'],row['m9'],row['y1'],row['y2'],row['y3'],row['y4'],row['y5'],row['y7'],row['y10'])
return c
request.save_book_to_database(
field_name='file', session=db.session,
tables=[Ois_date],
initializers=[OIS_date_init_func])
msg = "Successfully uploaded"
#return redirect(url_for('users.doimport_ois_date'), code=302)
if((Ois_date.query.order_by(Ois_date.date.desc()).first()) is not None):
date_query = Ois_date.query.order_by(Ois_date.date.desc()).first()
start_date = date_query.date
date_query1 = Ois_date.query.order_by(Ois_date.date.asc()).first()
end_date = date_query1.date
return render_template('OISdate_upload.html',msg=msg, start_date=start_date,end_date=end_date)
I am unable to figure out how to correctly capture the error and handle it, any feedback would be appreciated.
flask error-handling pyexcel
flask error-handling pyexcel
asked Nov 26 '18 at 9:33
Zedi10Zedi10
105
105
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have two options to handle this exception.
1) you import the exception directly from the pyexcel package and use it as the error:
e.g.
from pyexcel.exceptions import FileTypeNotSupported
...
@app.errorhandler(FileTypeNotSupported)
...
2) Or, you can wrap the code where you want to load the spreadsheet in a try-except block and throw a custom error.
from pyexcel.exceptions import FileTypeNotSupported
class CustomError(Exception)
pass
@app.errorhandler(CustomError)
# do something
pass
@app.route('/upload_excel')
def upload_excel():
try:
function_where_you_load_excel()
except FileTypeNotSupported:
raise CustomError
Thanks for your feedback. I'll try this and revert. Cheers!
– Zedi10
Nov 26 '18 at 12:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478181%2fflask-custom-error-handling-for-upload-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have two options to handle this exception.
1) you import the exception directly from the pyexcel package and use it as the error:
e.g.
from pyexcel.exceptions import FileTypeNotSupported
...
@app.errorhandler(FileTypeNotSupported)
...
2) Or, you can wrap the code where you want to load the spreadsheet in a try-except block and throw a custom error.
from pyexcel.exceptions import FileTypeNotSupported
class CustomError(Exception)
pass
@app.errorhandler(CustomError)
# do something
pass
@app.route('/upload_excel')
def upload_excel():
try:
function_where_you_load_excel()
except FileTypeNotSupported:
raise CustomError
Thanks for your feedback. I'll try this and revert. Cheers!
– Zedi10
Nov 26 '18 at 12:45
add a comment |
You have two options to handle this exception.
1) you import the exception directly from the pyexcel package and use it as the error:
e.g.
from pyexcel.exceptions import FileTypeNotSupported
...
@app.errorhandler(FileTypeNotSupported)
...
2) Or, you can wrap the code where you want to load the spreadsheet in a try-except block and throw a custom error.
from pyexcel.exceptions import FileTypeNotSupported
class CustomError(Exception)
pass
@app.errorhandler(CustomError)
# do something
pass
@app.route('/upload_excel')
def upload_excel():
try:
function_where_you_load_excel()
except FileTypeNotSupported:
raise CustomError
Thanks for your feedback. I'll try this and revert. Cheers!
– Zedi10
Nov 26 '18 at 12:45
add a comment |
You have two options to handle this exception.
1) you import the exception directly from the pyexcel package and use it as the error:
e.g.
from pyexcel.exceptions import FileTypeNotSupported
...
@app.errorhandler(FileTypeNotSupported)
...
2) Or, you can wrap the code where you want to load the spreadsheet in a try-except block and throw a custom error.
from pyexcel.exceptions import FileTypeNotSupported
class CustomError(Exception)
pass
@app.errorhandler(CustomError)
# do something
pass
@app.route('/upload_excel')
def upload_excel():
try:
function_where_you_load_excel()
except FileTypeNotSupported:
raise CustomError
You have two options to handle this exception.
1) you import the exception directly from the pyexcel package and use it as the error:
e.g.
from pyexcel.exceptions import FileTypeNotSupported
...
@app.errorhandler(FileTypeNotSupported)
...
2) Or, you can wrap the code where you want to load the spreadsheet in a try-except block and throw a custom error.
from pyexcel.exceptions import FileTypeNotSupported
class CustomError(Exception)
pass
@app.errorhandler(CustomError)
# do something
pass
@app.route('/upload_excel')
def upload_excel():
try:
function_where_you_load_excel()
except FileTypeNotSupported:
raise CustomError
answered Nov 26 '18 at 12:18
JoostJoost
2,0971417
2,0971417
Thanks for your feedback. I'll try this and revert. Cheers!
– Zedi10
Nov 26 '18 at 12:45
add a comment |
Thanks for your feedback. I'll try this and revert. Cheers!
– Zedi10
Nov 26 '18 at 12:45
Thanks for your feedback. I'll try this and revert. Cheers!
– Zedi10
Nov 26 '18 at 12:45
Thanks for your feedback. I'll try this and revert. Cheers!
– Zedi10
Nov 26 '18 at 12:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478181%2fflask-custom-error-handling-for-upload-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown