##
# Copyright (c) 2021, Xilinx Inc.
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
# associated documentation files (the "Software"), to deal in the Software without restriction, including 
# without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
# copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to 
# the following conditions: 
# 
# The above copyright notice and this permission notice shall be included in all copies or substantial 
# portions of the Software. 
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 
#
##
from flask import jsonify
from app import logger

exception_hash = {
"ArithmeticError":{"message":"Invalid Logical Expression","error_code":500},
"AssertionError":{"message":"Internal Server Error","error_code":500},
"AttributeError":{"message":"Attribute Missing","error_code":500},
"BufferError":{"message":"Internal Buffer Error","error_code":500},
"EOFError":{"message":"Internal Server Error","error_code":500},
"ModuleNotFoundError":{"message":"Module not found","error_code":500},
"IndexError":{"message":"Key Not Found","error_code":500},
"KeyError":{"message":"Key Not Found","error_code":500},
"MemoryError":{"message":"Memory Exceed","error_code":500},
"NameError":{"message":"Internal Server Error","error_code":500},
"OSError":{"message":"Internal Server Error","error_code":500},
"FileExistsError":{"message":"Internal Server Error","error_code":500},
"FileNotFoundError":{"message":"File Not Found","error_code":500},
"InterruptedError":{"message":"Keyboard interrupt","error_code":500},
"IsADirectoryError":{"message":"Keyboard interrupt","error_code":500},
"NotADirectoryError":{"message":"Keyboard interrupt","error_code":500},
"PermissionError":{"message":"Permission Required","error_code":400},
"ProcessLookupError":{"message":"Keyboard interrupt","error_code":500},
"TimeoutError":{"message":"Time Out","error_code":400},
"ReferenceError":{"message":"Refference Not Found","error_code":500},
"NotImplementedError":{"message":"Method Not Found","error_code":500},
"RecursionError":{"message":"Internal Server Error","error_code":500},
"StopIteration":{"message":"Internal Server Error","error_code":500},
"StopAsyncIteration":{"message":"Internal Server Error","error_code":500},
"SyntaxError":{"message":"Syntax Error","error_code":1},
"SystemError":{"message":"Internal Server Error","error_code":500},
"TypeError":{"message":"Internal Server Error","error_code":500},
"ValueError":{"message":"Internal Server Error","error_code":500},
"Warning":{"message":"Warning","error_code":200},
"KeyboardInterrupt":{"message":"Keyboard Interrupt","error_code":500},
"SystemExit":{"message":"Internal Server Error","error_code":500},
}

def exception_handler(msg):
    print("msg args",msg.args)
    import traceback
    tb = traceback.format_exc()
    print("tb------>>", tb)
    logger.error("%s",msg.__class__.__name__)
    logger.error("%s", msg.args[0])
    logger.error("%s", tb)
    if msg.__class__.__name__ == "KeyError" and msg.args[0] == "HTTP_UID":
            return jsonify({'error': 'Authentication Error, Uid is missing'}), 400
    else:
        error = exception_hash.get(msg.__class__.__name__ ,{"message":"Internal Server Error","error_code":500})
        return jsonify(error["message"]), error["error_code"]
