diff options
author | Jesús <heckyel@hyperbola.info> | 2022-03-22 00:39:40 +0800 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2022-03-22 00:39:40 +0800 |
commit | 3fca03988b42adaf8e67cc7137dd1fdba327e197 (patch) | |
tree | ad39a5bc12a03c82b9a4fd980c1b92ca0a1dac93 /utils/errors.py | |
download | heroeapi-3fca03988b42adaf8e67cc7137dd1fdba327e197.tar.lz heroeapi-3fca03988b42adaf8e67cc7137dd1fdba327e197.tar.xz heroeapi-3fca03988b42adaf8e67cc7137dd1fdba327e197.zip |
initial commit
Diffstat (limited to 'utils/errors.py')
-rw-r--r-- | utils/errors.py | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/utils/errors.py b/utils/errors.py new file mode 100644 index 0000000..ab6f1d5 --- /dev/null +++ b/utils/errors.py @@ -0,0 +1,155 @@ +""" +Code Errors reference: + - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status + - https://developer.mozilla.org/es/docs/Web/HTTP/Status +""" +import json +from flask import ( + Response +) + + +def internal_server_error(e): + response = Response( + response=json.dumps({ + "msg": "Something went wrong", + "_desc": str(e) + }), + status=500, + mimetype="applications/json" + ) + return response + + +def schema_validation_error(e): + response = Response( + response=json.dumps({ + "msg": "Request is missing required fields", + "_desc": str(e) + }), + status=400, + mimetype="applications/json" + ) + return response + + +def unauthorized_error(e): + response = Response( + response=json.dumps({ + "msg": "Invalid username or password", + "_desc": str(e) + }), + status=401, + mimetype="applications/json" + ) + return response + + +def disable_register_error(e): + response = Response( + response=json.dumps({ + "msg": "Unauthorized create user", + "_desc": str(e) + }), + status=401, + mimetype="applications/json" + ) + return response + + +def error_method(): + response = Response( + response=json.dumps({ + "msg": "Method unauthorized for your user" + }), + status=401, + mimetype="applications/json" + ) + return response + + +# Status 204 does not show any msg +def not_data_found(): + response = Response( + status=204, + mimetype="applications/json" + ) + return response + + +# ##### +# Heroe +# ##### +def heroe_already_exists_error(e): + response = Response( + response=json.dumps({ + "msg": "Heroe with given full-name already exists", + "_desc": str(e) + }), + status=400, + mimetype="applications/json" + ) + return response + + +def updating_heroe_error(e): + response = Response( + response=json.dumps({ + "msg": "Updating heroe added by other is forbidden", + "_desc": str(e) + }), + status=403, + mimetype="applications/json" + ) + return response + + +def deleting_heroe_error(e): + response = Response( + response=json.dumps({ + "msg": "Deleting heroe added by other is forbidden", + "_desc": str(e) + }), + status=403, + mimetype="applications/json" + ) + return response + + +# ##### +# User +# ##### +def user_already_exists_error(e): + response = Response( + response=json.dumps({ + "msg": "User with given username or email already exists", + "_desc": str(e) + }), + status=400, + mimetype="applications/json" + ) + return response + + +def updating_user_error(e): + response = Response( + response=json.dumps({ + "msg": "Updating user added by other is forbidden", + "_desc": str(e) + }), + status=403, + mimetype="applications/json" + ) + return response + + +def deleting_user_error(e): + response = Response( + response=json.dumps({ + "msg": "Deleting user added by other is forbidden", + "_desc": str(e) + }), + status=403, + mimetype="applications/json" + ) + return response |