Skip to content

Commit

Permalink
refactor: update messages (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoliaots committed Mar 13, 2022
1 parent da6eb26 commit d702be7
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 14 deletions.
4 changes: 3 additions & 1 deletion lib/health/indicators/redis.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
CANNOT_BE_READ,
MISSING_CLIENT,
NOT_RESPONSIVE,
ABNORMALLY_MEMORY_USAGE
ABNORMALLY_MEMORY_USAGE,
MISSING_TYPE
} from '@/messages';
import { isError, promiseTimeout, removeLineBreaks, parseUsedMemory, isNullish } from '@/utils';
import { RedisCheckSettings } from './redis-check-settings.interface';
Expand Down Expand Up @@ -39,6 +40,7 @@ export class RedisHealthIndicator extends HealthIndicator {
let isHealthy = false;

try {
if (!type) throw new Error(MISSING_TYPE);
if (!options.client) throw new Error(MISSING_CLIENT);

if (type === 'redis') {
Expand Down
10 changes: 8 additions & 2 deletions lib/messages/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import * as allExports from '.';

const { CLIENT_NOT_FOUND, ...messages } = allExports;
const { CLIENT_NOT_FOUND, OPERATIONS_TIMEOUT, ...messages } = allExports;

describe('CLIENT_NOT_FOUND', () => {
test('should return a string', () => {
const namespace = 'client';
const namespace = 'name';
expect(CLIENT_NOT_FOUND(namespace)).toContain(namespace);
expect(CLIENT_NOT_FOUND(namespace)).toContain('Redis');
expect(CLIENT_NOT_FOUND(namespace, false)).toContain(namespace);
expect(CLIENT_NOT_FOUND(namespace, false)).toContain('Cluster');
});
});

describe('OPERATIONS_TIMEOUT', () => {
test('should return a string', () => {
expect(OPERATIONS_TIMEOUT(100)).toContain('100');
});
});

test('should be a string', () => {
Object.values(messages).forEach(value => expect(typeof value).toBe('string'));
});
15 changes: 9 additions & 6 deletions lib/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export const MISSING_CONFIGURATION = `Asynchronous configurations are missing, you must provide one of 'useFactory', 'useClass', and 'useExisting'.`;
export const MISSING_CONFIGURATION =
'Asynchronous configurations are missing. Expected one of: `useFactory`, `useClass`, `useExisting`.';
export const CONNECTED_SUCCESSFULLY = `Connected successfully to the server`;
export const CLIENT_NOT_FOUND = (namespace: string, isRedis = true): string =>
`${isRedis ? 'Redis' : 'Cluster'} client '${namespace}' not found in the application context.`;
`${isRedis ? 'Redis' : 'Cluster'} client \`${namespace}\` not found in the application context.`;

export const MISSING_CLIENT = `This client does not exist.`;
export const NOT_RESPONSIVE = `This client is not responsive.`;
export const ABNORMALLY_MEMORY_USAGE = `This client is using abnormally high memory.`;
export const MISSING_CLIENT = 'Argument `client` is missing.';
export const MISSING_TYPE = 'Argument `type` is missing.';
export const NOT_RESPONSIVE = `The client is not responsive.`;
export const ABNORMALLY_MEMORY_USAGE = `The client is using abnormally high memory.`;
export const CANNOT_BE_READ = `Info cluster cannot be read.`;
export const FAILED_CLUSTER_STATE = `Info cluster is not on OK state.`;
export const FAILED_CLUSTER_STATE = `Info cluster is not in OK state.`;
export const OPERATIONS_TIMEOUT = (timeout: number) => `Operations timed out after ${String(timeout)}ms.`;
3 changes: 2 additions & 1 deletion lib/utils/promise-timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { promiseTimeout } from './promise-timeout';
import { OPERATIONS_TIMEOUT } from '@/messages';

describe('promiseTimeout', () => {
let timer: NodeJS.Timeout;
Expand All @@ -17,6 +18,6 @@ describe('promiseTimeout', () => {
});

test('should throw an error', async () => {
await expect(promiseTimeout(10, getPromise(20))).rejects.toThrow();
await expect(promiseTimeout(10, getPromise(20))).rejects.toThrow(OPERATIONS_TIMEOUT(10));
});
});
4 changes: 3 additions & 1 deletion lib/utils/promise-timeout.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { OPERATIONS_TIMEOUT } from '@/messages';

/**
* Executes a promise in the given timeout. If the promise does not finish in the given timeout, it will throw an Error.
*
Expand All @@ -10,7 +12,7 @@ export const promiseTimeout = (ms: number, promise: Promise<unknown>): Promise<u
return Promise.race([
promise,
new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error(`timeout of ${ms}ms exceeded`)), ms);
timer = setTimeout(() => reject(new Error(OPERATIONS_TIMEOUT(ms))), ms);
})
]).finally(() => {
clearTimeout(timer);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@liaoliaots/nestjs-redis",
"version": "7.0.0-rc.2",
"version": "7.0.0-rc.3",
"description": "Redis(ioredis) module for NestJS framework",
"author": "LiaoLiao",
"main": "dist/index.js",
Expand Down

0 comments on commit d702be7

Please sign in to comment.