mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-25 15:34:38 +03:00
feat(): disabling global catch support
This commit is contained in:
parent
3d7ecbd7f2
commit
03b17a0cdb
@ -11,6 +11,8 @@ export * from './interfaces';
|
|||||||
export * from './telegraf.module';
|
export * from './telegraf.module';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Backward compatibility with versions < 1.4.0
|
* Backward compatibility with versions < 1.4.0,
|
||||||
|
* after removing TelegrafProvider service
|
||||||
|
* TODO: remove that on next major release
|
||||||
*/
|
*/
|
||||||
export { Telegraf as TelegrafProvider } from 'telegraf';
|
export { Telegraf as TelegrafProvider } from 'telegraf';
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { TelegrafContext } from 'telegraf/typings/context';
|
import { TelegrafContext } from 'telegraf/typings/context';
|
||||||
|
|
||||||
export interface Context extends TelegrafContext {
|
export interface Context extends TelegrafContext {
|
||||||
[key: string]: any;
|
[key: string]: any; // TBD
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -5,6 +5,7 @@ import {
|
|||||||
LaunchWebhookOptions,
|
LaunchWebhookOptions,
|
||||||
} from 'telegraf/typings/telegraf';
|
} from 'telegraf/typings/telegraf';
|
||||||
import { Middleware } from 'telegraf/typings/composer';
|
import { Middleware } from 'telegraf/typings/composer';
|
||||||
|
import { Context } from './context.interface';
|
||||||
|
|
||||||
export interface TelegrafModuleOptions {
|
export interface TelegrafModuleOptions {
|
||||||
token: string;
|
token: string;
|
||||||
@ -15,7 +16,8 @@ export interface TelegrafModuleOptions {
|
|||||||
};
|
};
|
||||||
botName?: string;
|
botName?: string;
|
||||||
include?: Function[];
|
include?: Function[];
|
||||||
middlewares?: ReadonlyArray<Middleware<any>>;
|
middlewares?: ReadonlyArray<Middleware<Context>>;
|
||||||
|
disableGlobalCatch?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TelegrafOptionsFactory {
|
export interface TelegrafOptionsFactory {
|
||||||
|
@ -7,11 +7,13 @@ import {
|
|||||||
Global,
|
Global,
|
||||||
Inject,
|
Inject,
|
||||||
OnApplicationShutdown,
|
OnApplicationShutdown,
|
||||||
|
Logger,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
TelegrafModuleOptions,
|
TelegrafModuleOptions,
|
||||||
TelegrafModuleAsyncOptions,
|
TelegrafModuleAsyncOptions,
|
||||||
TelegrafOptionsFactory,
|
TelegrafOptionsFactory,
|
||||||
|
Context,
|
||||||
} from './interfaces';
|
} from './interfaces';
|
||||||
import {
|
import {
|
||||||
TELEGRAF_BOT_NAME,
|
TELEGRAF_BOT_NAME,
|
||||||
@ -28,6 +30,8 @@ import { defer } from 'rxjs';
|
|||||||
providers: [UpdatesExplorerService, MetadataAccessorService],
|
providers: [UpdatesExplorerService, MetadataAccessorService],
|
||||||
})
|
})
|
||||||
export class TelegrafCoreModule implements OnApplicationShutdown {
|
export class TelegrafCoreModule implements OnApplicationShutdown {
|
||||||
|
private static logger = new Logger(TelegrafCoreModule.name);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(TELEGRAF_BOT_NAME) private readonly botName: string,
|
@Inject(TELEGRAF_BOT_NAME) private readonly botName: string,
|
||||||
private readonly moduleRef: ModuleRef,
|
private readonly moduleRef: ModuleRef,
|
||||||
@ -40,8 +44,22 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
provide: telegrafBotName,
|
provide: telegrafBotName,
|
||||||
useFactory: async (): Promise<any> =>
|
useFactory: async (): Promise<any> =>
|
||||||
await defer(async () => {
|
await defer(async () => {
|
||||||
const bot = new Telegraf(options.token);
|
const bot = new Telegraf<Context>(options.token);
|
||||||
this.applyBotMiddlewares(bot, options.middlewares);
|
this.applyBotMiddlewares(bot, options.middlewares);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Backward compatibility with versions < 1.4.0,
|
||||||
|
* TODO: remove that on next major release,
|
||||||
|
* after exception filters has been added
|
||||||
|
*/
|
||||||
|
if (!options.disableGlobalCatch) {
|
||||||
|
bot.catch((err, ctx: Context) => {
|
||||||
|
this.logger.error(
|
||||||
|
`Encountered an error for ${ctx.updateType} update type`,
|
||||||
|
err,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
await bot.launch(options.launchOptions);
|
await bot.launch(options.launchOptions);
|
||||||
return bot;
|
return bot;
|
||||||
}).toPromise(),
|
}).toPromise(),
|
||||||
@ -77,8 +95,22 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
const { botName, ...telegrafOptions } = telegrafModuleOptions;
|
const { botName, ...telegrafOptions } = telegrafModuleOptions;
|
||||||
|
|
||||||
return await defer(async () => {
|
return await defer(async () => {
|
||||||
const bot = new Telegraf(telegrafOptions.token);
|
const bot = new Telegraf<Context>(telegrafOptions.token);
|
||||||
this.applyBotMiddlewares(bot, telegrafOptions.middlewares);
|
this.applyBotMiddlewares(bot, telegrafOptions.middlewares);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Backward compatibility with versions < 1.4.0,
|
||||||
|
* TODO: remove that on next major release,
|
||||||
|
* after exception filters has been added
|
||||||
|
*/
|
||||||
|
if (!telegrafOptions.disableGlobalCatch) {
|
||||||
|
bot.catch((err, ctx: Context) => {
|
||||||
|
this.logger.error(
|
||||||
|
`Encountered an error for ${ctx.updateType} update type`,
|
||||||
|
err,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
await bot.launch(telegrafOptions.launchOptions);
|
await bot.launch(telegrafOptions.launchOptions);
|
||||||
return bot;
|
return bot;
|
||||||
}).toPromise();
|
}).toPromise();
|
||||||
|
Loading…
Reference in New Issue
Block a user