Glider
Loading...
Searching...
No Matches
ErrorHandler.py
Go to the documentation of this file.
1import os
2from datetime import datetime as dt
3from pymongo import MongoClient
4import ssl
5from dotenv import load_dotenv, dotenv_values
6from MongoConnection import mongo_connection
7load_dotenv()
8
10 """Updates file info in our database with the error found in it (if it's applicable)
11 """
12
13 def handle(self, e, message, file_db_id):
14 """Add status_cause and error_message fields to file info
15
16 Args:
17 e (str): error found
18 message (str): error message generated by python
19 file_db_id (str): file id assigned in mongodb
20 Returns: error_cause (str)
21 """
22 mongo_conn = mongo_connection()
23 collection = mongo_conn.mongo_conn_snapshots()
24 error_cause = self.get_human_error(e)
25 collection.update_one({"file_db_id":file_db_id},{"$set": {"status": "failed", "status_cause": error_cause, "error_message": message, "updated_at": dt.now()}})
26 # collection.update_one({"file_db_id":file_db_id},{"$set": {"updated_at": dt.now().replace(hour=0, minute=0, second=0, microsecond=0)}})
27 return error_cause
28
29 def get_human_error(self, e):
30 """Gets a message error according to king of issue
31
32 Args:
33 e (str): error found
34 Returns: error (str)
35 """
36 error_type = type(e)
37 if str(error_type) == "<class 'botocore.exceptions.ClientError'>":
38 return ("S3: File does not exist. Are you sure this is the correct file")
39 elif str(error_type) == "<class 'botocore.exceptions.PartialCredentialsError'>":
40 return ("S3: Partial credentials found in explicit. aws_access_key_id or aws_secret_access_key no founded")
41 elif str(e) == "Path invalid.":
42 return ("S3: Input path does not exist in s3 Bucket. Are you sure this is the correct path?")
43 elif str(error_type) == "<class 'pymongo.errors.ServerSelectionTimeoutError'>":
44 return ("MDB: Saving the information is taking too long. Connection refused")
45 elif str(error_type) == "<class 'IndexError'>":
46 return ("MDB: No formats matches with your file")
47 elif str(e) == "Error in currency. Execution aborted":
48 return ("PD: Currency not found in your file")
49 elif str(e) == "Validation Error. Execution aborted":
50 return ("PD: Something was wrong. Data validation failed.")
51 elif str(error_type) == "<class 'NameError'>":
52 return ("Python: There is a word not defined")
53 elif str(error_type) == "<class 'KeyError'>":
54 return ("Python: There is a wrong word")
55 elif "Expecting value: line 1 column" in str(e):
56 return ("Maybe there is a malformed format")
57 elif "cannot unpack non-iterable" in str(e):
58 return "Maybe something is wrong in headers"
59 return str(e)
60
61# class DataBuildFailed(Exception):
62# # # def __init__(self):
63# # # return None
64# # def __str__():
65# print("Data Build Failed. Please review data")
66
67class FileNotSupported(Exception):
68 """
69 It's launched when a not allowed file is ingested
70 """
71 def __init__(self, message="File extension is not supported. Verify if it is the correct file"):
72 self.message = message
73 super().__init__(self.message)
74
75class FileNotLoaded(Exception):
76 """
77 It's launched when a file could not be loaded or it has a undefined encoding
78 """
79 def __init__(self, message="File couldn't be loaded. Verify that file is an accepted format"):
80 self.message = message
81 super().__init__(self.message)
82
83class CurrencyNotFound(Exception):
84 """
85 It's launched when a file contains one or more not defined currencies
86 """
87 def __init__(self, message="There are currencies not found in your file. Please check currencies"):
88 self.message = message
89 super().__init__(self.message)
90
91class ValidationFailed(Exception):
92 """
93 It's launched when something's wrong in the data validation procedure
94 """
95 def __init__(self, message="Something's wrong in validation step. Verify your data"):
96 self.message = message
97 super().__init__(self.message)
98
99
__init__(self, message="There are currencies not found in your file. Please check currencies")
handle(self, e, message, file_db_id)
__init__(self, message="File couldn't be loaded. Verify that file is an accepted format")
__init__(self, message="File extension is not supported. Verify if it is the correct file")
__init__(self, message="Something's wrong in validation step. Verify your data")