<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def check_perm(user_id, permission_code):
 """ Checks whether a user has permission to perform an action. The permission_code parameter should be a permission contained in tPerm. If the user does not have permission to perfom an action, a permission error is thrown. """

    try:
        perm = db.DBSession.query(Perm).filter(Perm.code==permission_code).one()
    except NoResultFound:
        raise PermissionError("Nonexistent permission type: %s"%(permission_code))


    try:
        res = db.DBSession.query(User).join(RoleUser, RoleUser.user_id==User.id).\
            join(Perm, Perm.id==perm.id).\
            join(RolePerm, RolePerm.perm_id==Perm.id).filter(User.id==user_id).one()
    except NoResultFound:
        raise PermissionError("Permission denied. User %s does not have permission %s"%
                        (user_id, permission_code))