Skip to content

Instantly share code, notes, and snippets.

@nottrobin
Created May 20, 2014 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nottrobin/cbb79b905b1cc3510882 to your computer and use it in GitHub Desktop.
Save nottrobin/cbb79b905b1cc3510882 to your computer and use it in GitHub Desktop.
Convert IOErrors to Flask response for HTTP errors
import errno
def error_response(error, filename):
status = 500 # Default to "server error"
if hasattr(error, 'errno'):
if error.errno in [errno.EPERM, errno.EACCES]:
status = 403 # Forbidden
if error.errno in [errno.ENOENT, errno.ENXIO]:
status = 404 # Not found
if error.errno in [errno.EEXIST]:
status = 409 # Conflict
if error.errno in [errno.E2BIG]:
status = 413 # Request Entity Too Large
message = error.message
if not message and hasattr(error, "strerror"):
message = error.strerror
elif not message and hasattr(error, "log_message"):
message = error.log_message
return {
"filename": filename,
"error_class": error.__class__.__name__,
"message": message,
"code": status
}, status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment