import pecan
import collections


class NotFound(Exception):
    code = 404
    title = "Not Found"
    pass

class InvalidJSON(Exception):
    code = 400
    pass

class NotJSONHeader(Exception):
    code = "400"
    title = "Bad Request"
    message = "Invalid Content Type in Header"
    pass

class BadData(Exception):
    code = 400
    title = "Bad Data"
    pass

class IndexError(Exception):
    code = 500
    title ="Index Error"
    pass


class Conflict(Exception):
    code = 409
    title = "Conflict"
    pass

class Forbidden(Exception):
    code = 403
    title = "Forbidden"
    
def exception_handle(e):
    """
    # | Function to handel the exception
    # |
    # | Arguments: None
    # |
    # | Returns: error
    """
    
    print type(e)
    print e.code
    print e.message
    # check for error code
    if hasattr(e, 'code'):
        code = e.code
    else:
        code = 500
        
    # check for error title
    if hasattr(e, 'title'):
        title = e.title
    else:
        title = 'Internal Server Error'
    
    # check for error messages
    if hasattr(e, 'message'):
        message = e.message
    else:
        message = 'Unknown error occured'
        
    return send_error(code, title, message)
    

def send_error(code, title, message):
    """
    # | Functon to retun the exception
    # |
    # | Arguments: 
    # | <code>: Error code  
    # | <title>: Http tile
    # | <message>: Exception message
    # |
    # | Returns: Dictionary
    """
    
    pecan.response.status = code
    error = collections.OrderedDict()
    error["code"] = code
    error["title"] = title
    error["message"] = message
    return { "error": error }