Created
May 20, 2014 16:16
-
-
Save nottrobin/cbb79b905b1cc3510882 to your computer and use it in GitHub Desktop.
Convert IOErrors to Flask response for HTTP errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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