Skip to content

Commit d045979

Browse files
ArtemHoruzhenkopawelangelow
authored andcommitted
RI-7119 handle resisearch endpoints errors (#4572)
* RI-7119 handle resisearch endpoints errors * RI-7119 resolve PR comments
1 parent 971ae5d commit d045979

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

redisinsight/api/src/constants/error-messages.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/* eslint-disable max-len */
2+
import { numberWithSpaces } from 'src/utils/base.helper';
3+
24
export default {
35
UNAUTHORIZED: 'Authorization failed',
46
FORBIDDEN: 'Access denied',
@@ -102,8 +104,10 @@ export default {
102104
`Required ${module} module is not loaded.`,
103105
APP_SETTINGS_NOT_FOUND: () => 'Could not find application settings.',
104106
SERVER_INFO_NOT_FOUND: () => 'Could not find server info.',
105-
INCREASE_MINIMUM_LIMIT: (count: string) =>
106-
`Set MAXSEARCHRESULTS to at least ${count}.`,
107+
INCREASE_MINIMUM_LIMIT: (count?: number) =>
108+
count
109+
? `Set MAXSEARCHRESULTS to at least ${numberWithSpaces(count)}.`
110+
: 'Increase MAXSEARCHRESULTS value to search more.',
107111
INVALID_WINDOW_ID: 'Invalid window id.',
108112
UNDEFINED_WINDOW_ID: 'Undefined window id.',
109113
LIBRARY_NOT_EXIST: 'This library does not exist.',

redisinsight/api/src/modules/browser/redisearch/redisearch.service.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { isUndefined, toNumber, uniq } from 'lodash';
22
import {
3-
BadRequestException,
43
ConflictException,
5-
HttpException,
64
Injectable,
75
Logger,
86
} from '@nestjs/common';
97
import ERROR_MESSAGES from 'src/constants/error-messages';
10-
import { catchAclError } from 'src/utils';
8+
import { catchRedisSearchError } from 'src/utils';
119
import { ClientMetadata } from 'src/common/models';
1210
import {
1311
CreateRedisearchIndexDto,
@@ -17,9 +15,8 @@ import {
1715
SearchRedisearchDto,
1816
} from 'src/modules/browser/redisearch/dto';
1917
import { GetKeysWithDetailsResponse } from 'src/modules/browser/keys/dto';
20-
import { DEFAULT_MATCH, RedisErrorCodes } from 'src/constants';
18+
import { DEFAULT_MATCH } from 'src/constants';
2119
import { plainToInstance } from 'class-transformer';
22-
import { numberWithSpaces } from 'src/utils/base.helper';
2320
import { BrowserHistoryMode, RedisString } from 'src/common/constants';
2421
import { CreateBrowserHistoryDto } from 'src/modules/browser/browser-history/dto';
2522
import { BrowserHistoryService } from 'src/modules/browser/browser-history/browser-history.service';
@@ -68,7 +65,8 @@ export class RedisearchService {
6865
});
6966
} catch (e) {
7067
this.logger.error('Failed to get redisearch indexes', e, clientMetadata);
71-
throw catchAclError(e);
68+
69+
throw catchRedisSearchError(e);
7270
}
7371
}
7472

@@ -142,7 +140,8 @@ export class RedisearchService {
142140
return undefined;
143141
} catch (e) {
144142
this.logger.error('Failed to create redisearch index', e, clientMetadata);
145-
throw catchAclError(e);
143+
144+
throw catchRedisSearchError(e);
146145
}
147146
}
148147

@@ -174,7 +173,8 @@ export class RedisearchService {
174173
return plainToInstance(IndexInfoDto, convertIndexInfoReply(infoReply));
175174
} catch (e) {
176175
this.logger.error('Failed to get index info', e, clientMetadata);
177-
throw catchAclError(e);
176+
177+
throw catchRedisSearchError(e);
178178
}
179179
}
180180

@@ -264,15 +264,8 @@ export class RedisearchService {
264264
e,
265265
clientMetadata,
266266
);
267-
if (e instanceof HttpException) {
268-
throw e;
269-
}
270-
if (e.message?.includes(RedisErrorCodes.RedisearchLimit)) {
271-
throw new BadRequestException(
272-
ERROR_MESSAGES.INCREASE_MINIMUM_LIMIT(numberWithSpaces(dto.limit)),
273-
);
274-
}
275-
throw catchAclError(e);
267+
268+
throw catchRedisSearchError(e, { searchLimit: dto.limit });
276269
}
277270
}
278271

redisinsight/api/src/utils/catch-redis-errors.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,24 @@ export const catchMultiTransactionError = (
170170
if (err) throw err;
171171
});
172172
};
173+
174+
export const catchRedisSearchError = (
175+
error: ReplyError,
176+
options?: { searchLimit?: number },
177+
): HttpException => {
178+
if (error instanceof HttpException) {
179+
throw error;
180+
}
181+
182+
if (error.message?.includes(RedisErrorCodes.RedisearchLimit)) {
183+
throw new BadRequestException(
184+
ERROR_MESSAGES.INCREASE_MINIMUM_LIMIT(options?.searchLimit),
185+
);
186+
}
187+
188+
if (error.message?.includes('Unknown index')) {
189+
throw new NotFoundException(error.message);
190+
}
191+
192+
throw catchAclError(error);
193+
};

0 commit comments

Comments
 (0)