@@ -3,48 +3,80 @@ import { EuiButton } from '@elastic/eui'
3
3
4
4
import { EXTERNAL_LINKS } from 'uiSrc/constants/links'
5
5
import { CustomErrorCodes } from 'uiSrc/constants'
6
- import { AiChatErrors } from 'uiSrc/constants/apiErrors'
6
+ import { AI_CHAT_ERRORS } from 'uiSrc/constants/apiErrors'
7
7
import ApiStatusCode from 'uiSrc/constants/apiStatusCode'
8
8
9
9
import RestartChat from '../restart-chat'
10
10
11
11
import styles from './styles.module.scss'
12
12
13
13
export interface Props {
14
- error ?: { statusCode : number , errorCode ?: number }
14
+ error ?: {
15
+ statusCode : number
16
+ errorCode ?: number
17
+ details ?: Record < string , any >
18
+ }
15
19
onRestart : ( ) => void
16
20
}
17
21
22
+ const ERROR_CODES_WITHOUT_RESTART = [
23
+ CustomErrorCodes . CloudApiUnauthorized ,
24
+ CustomErrorCodes . GeneralAiUnexpectedError ,
25
+ CustomErrorCodes . AiQueryRateLimitRequest ,
26
+ CustomErrorCodes . AiQueryRateLimitToken ,
27
+ ]
28
+
29
+ const ERROR_CODES_WITHOUT_REPORT_ISSUE = [
30
+ CustomErrorCodes . AiQueryRateLimitRequest ,
31
+ CustomErrorCodes . AiQueryRateLimitToken ,
32
+ CustomErrorCodes . AiQueryRateLimitMaxTokens ,
33
+ ]
34
+
18
35
const ErrorMessage = ( props : Props ) => {
19
36
const { error, onRestart } = props
20
37
21
- const getErrorMessage = ( error ?: { statusCode : number , errorCode ?: number } ) : string => {
22
- if ( error ?. statusCode === ApiStatusCode . Timeout ) return AiChatErrors . Timeout
23
- if ( error ?. errorCode === CustomErrorCodes . GeneralAiUnexpectedError ) return AiChatErrors . DefaultUnexpected
38
+ const getErrorMessage = (
39
+ error ?: {
40
+ statusCode : number ,
41
+ errorCode ?: number ,
42
+ details ?: Record < string , any > }
43
+ ) : string => {
44
+ const { statusCode, errorCode, details } = error || { }
24
45
25
- return AiChatErrors . Default
46
+ if ( statusCode === ApiStatusCode . Timeout ) return AI_CHAT_ERRORS . timeout ( )
47
+ if ( errorCode === CustomErrorCodes . GeneralAiUnexpectedError ) return AI_CHAT_ERRORS . unexpected ( )
48
+ if ( errorCode === CustomErrorCodes . AiQueryRateLimitRequest
49
+ || errorCode === CustomErrorCodes . AiQueryRateLimitToken
50
+ ) return AI_CHAT_ERRORS . rateLimit ( details ?. limiterSeconds )
51
+ if ( errorCode === CustomErrorCodes . AiQueryRateLimitMaxTokens ) return AI_CHAT_ERRORS . tokenLimit ( )
52
+
53
+ return AI_CHAT_ERRORS . default ( )
26
54
}
27
55
28
56
if ( ! error ) return null
29
57
30
- const isShowRestart = error . errorCode !== CustomErrorCodes . CloudApiUnauthorized
31
- && error . errorCode !== CustomErrorCodes . GeneralAiUnexpectedError
58
+ const isShowRestart = ! ( error . errorCode && ERROR_CODES_WITHOUT_RESTART . includes ( error . errorCode ) )
32
59
&& error . statusCode !== ApiStatusCode . Timeout
60
+ const isShowReportIssue = ! ( error . errorCode && ERROR_CODES_WITHOUT_REPORT_ISSUE . includes ( error . errorCode ) )
33
61
34
62
return (
35
63
< >
36
64
< div className = { styles . errorMessage } data-testid = "ai-chat-error-message" >
37
65
{ getErrorMessage ( error ) }
38
- { ' ' }
39
- < a
40
- className = "link-underline"
41
- href = { EXTERNAL_LINKS . githubIssues }
42
- data-testid = "ai-chat-error-report-link"
43
- target = "_blank"
44
- rel = "noreferrer"
45
- >
46
- report the issue
47
- </ a >
66
+ { isShowReportIssue && (
67
+ < >
68
+ { ' ' }
69
+ < a
70
+ className = "link-underline"
71
+ href = { EXTERNAL_LINKS . githubIssues }
72
+ data-testid = "ai-chat-error-report-link"
73
+ target = "_blank"
74
+ rel = "noreferrer"
75
+ >
76
+ report the issue
77
+ </ a >
78
+ </ >
79
+ ) }
48
80
</ div >
49
81
{ isShowRestart && (
50
82
< RestartChat
0 commit comments