Пример #1
0
    def _wrap_func(*args, **kwargs):
        self = args[0]
        session = sessionmaker(bind=self.engine, expire_on_commit=False)

        # new session.   no connections are in use.
        self.session = session()
        try:
            # execute transaction statements.
            res = func(*args, **kwargs)
            # commit.  The pending changes above
            # are flushed via flush(), the Transaction
            # is committed, the Connection object closed
            # and discarded, the underlying DBAPI connection
            # returned to the connection pool.
            self.session.commit()
        except Exception as err:
            LOGGER.critical(err)
            # on rollback, the same closure of state
            # as that of commit proceeds.
            self.session.rollback()
            raise
        finally:
            # close the Session.  This will expunge any remaining
            # objects as well as reset any existing SessionTransaction
            # state.  Neither of these steps are usually essential.
            # However, if the commit() or rollback() itself experienced
            # an unanticipated internal failure (such as due to a mis-behaved
            # user-defined event handler), .close() will ensure that
            # invalid state is removed.
            self.session.close()
        return res
Пример #2
0
 async def on_command_error(self, ctx, error):
     if isinstance(error, commands.NoPrivateMessage):
         await ctx.author.send(
             'This command cannot be used in private messages.')
     elif isinstance(error, commands.DisabledCommand):
         await ctx.author.send(
             'Sorry. This command is disabled and cannot be used.')
     elif isinstance(error, commands.CommandInvokeError):
         LOGGER.critical(f'In {ctx.command.qualified_name}:')
         traceback.print_tb(error.original.__traceback__)
         LOGGER.critical(
             f'{error.original.__class__.__name__}: {error.original}')
     elif isinstance(error, commands.MissingRequiredArgument):
         await ctx.author.send(
             'Sorry. This command is not how this command works, !help <command_name> to display usage'
         )
     else:
         LOGGER.critical(error)