1
- import { AxiosError } from 'axios' ;
2
1
import { get } from 'lodash' ;
3
2
import { HttpException } from '@nestjs/common' ;
4
3
import {
@@ -7,13 +6,29 @@ import {
7
6
AiQueryBadRequestException ,
8
7
AiQueryNotFoundException ,
9
8
AiQueryInternalServerErrorException ,
9
+ AiQueryRateLimitRequestException , AiQueryRateLimitTokenException , AiQueryRateLimitMaxTokensException ,
10
10
} from 'src/modules/ai/query/exceptions' ;
11
+ import { AiQueryServerErrors } from 'src/modules/ai/query/models' ;
11
12
12
- export const wrapAiQueryError = ( error : AxiosError , message ?: string ) : HttpException => {
13
+ export const wrapAiQueryError = ( error : any , message ?: string ) : HttpException => {
13
14
if ( error instanceof HttpException ) {
14
15
return error ;
15
16
}
16
17
18
+ // ai errors to handle
19
+ if ( error . error ) {
20
+ switch ( error . error ) {
21
+ case AiQueryServerErrors . RateLimitRequest :
22
+ return new AiQueryRateLimitRequestException ( error . message , { details : error . data } ) ;
23
+ case AiQueryServerErrors . RateLimitToken :
24
+ return new AiQueryRateLimitTokenException ( error . message , { details : error . data } ) ;
25
+ case AiQueryServerErrors . MaxTokens :
26
+ return new AiQueryRateLimitMaxTokensException ( error . message , { details : error . data } ) ;
27
+ default :
28
+ // go further
29
+ }
30
+ }
31
+
17
32
// TransportError or Axios error
18
33
const response = get ( error , [ 'description' , 'target' , '_req' , 'res' ] , error . response ) ;
19
34
0 commit comments