提交 2ed76a98 authored 作者: Oleg Polyakov's avatar Oleg Polyakov

feat: add custom error handler

上级 7bb40209
......@@ -61,6 +61,13 @@ export default {
url: 'redis://:authpassword@127.0.0.1:6380/4',
}
```
With custom error handler
```typescript
export default {
url: 'redis://:authpassword@127.0.0.1:6380/4',
errorHandler: (err, client) => {},
}
```
With multi client
```typescript
export default [
......@@ -197,4 +204,4 @@ interface RedisOptions {
showFriendlyErrorStack?: boolean;
}
```
That's it!
\ No newline at end of file
That's it!
......@@ -11,40 +11,35 @@ export interface RedisClient {
size: number;
}
function getClient(options: RedisModuleOptions, key: string): Redis.Redis {
const { errorHandler, url, ...opt } = options;
const client = url ? new Redis(url) : new Redis(opt);
if (errorHandler) {
client.on('error', err => errorHandler(err, client));
}
return client;
}
export const createClient = () => ({
provide: REDIS_CLIENT,
useFactory: (options: RedisModuleOptions | RedisModuleOptions[]) => {
const clients = new Map<string, Redis.Redis>();
const defaultKey = uuid();
if (Array.isArray(options)) {
for (const o of options) {
if (o.name) {
if (clients.has(o.name)) {
throw new RedisClientError(`client ${o.name} is exists`);
}
if (o.url) {
clients.set(o.name, new Redis(o.url));
} else {
clients.set(o.name, new Redis(o));
}
} else {
if (clients.has(defaultKey)) {
throw new RedisClientError('default client is exists');
}
if (o.url) {
clients.set(defaultKey, new Redis(o.url));
} else {
clients.set(defaultKey, new Redis(o));
}
const key = o.name || defaultKey;
if (clients.has(key)) {
throw new RedisClientError(`client ${o.name} or default client is exists`);
}
clients.set(key, getClient(o, key));
}
} else {
if (options.url) {
clients.set(defaultKey, new Redis(options.url));
} else {
clients.set(defaultKey, new Redis(options));
}
clients.set(defaultKey, getClient(options, defaultKey));
}
return {
defaultKey,
clients,
......
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { RedisOptions } from 'ioredis';
import * as IORedis from 'ioredis';
export interface RedisModuleOptions extends RedisOptions {
name?: string;
url?: string;
errorHandler?(err, client: IORedis.Redis): void;
}
export interface RedisModuleAsyncOptions
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论