@@ -3,48 +3,80 @@ import { EuiButton } from '@elastic/eui'
33
44import { EXTERNAL_LINKS } from 'uiSrc/constants/links'
55import { CustomErrorCodes } from 'uiSrc/constants'
6- import { AiChatErrors } from 'uiSrc/constants/apiErrors'
6+ import { AI_CHAT_ERRORS } from 'uiSrc/constants/apiErrors'
77import ApiStatusCode from 'uiSrc/constants/apiStatusCode'
88
99import RestartChat from '../restart-chat'
1010
1111import styles from './styles.module.scss'
1212
1313export interface Props {
14- error ?: { statusCode : number , errorCode ?: number }
14+ error ?: {
15+ statusCode : number
16+ errorCode ?: number
17+ details ?: Record < string , any >
18+ }
1519 onRestart : ( ) => void
1620}
1721
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+
1835const ErrorMessage = ( props : Props ) => {
1936 const { error, onRestart } = props
2037
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 || { }
2445
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 ( )
2654 }
2755
2856 if ( ! error ) return null
2957
30- const isShowRestart = error . errorCode !== CustomErrorCodes . CloudApiUnauthorized
31- && error . errorCode !== CustomErrorCodes . GeneralAiUnexpectedError
58+ const isShowRestart = ! ( error . errorCode && ERROR_CODES_WITHOUT_RESTART . includes ( error . errorCode ) )
3259 && error . statusCode !== ApiStatusCode . Timeout
60+ const isShowReportIssue = ! ( error . errorCode && ERROR_CODES_WITHOUT_REPORT_ISSUE . includes ( error . errorCode ) )
3361
3462 return (
3563 < >
3664 < div className = { styles . errorMessage } data-testid = "ai-chat-error-message" >
3765 { 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+ ) }
4880 </ div >
4981 { isShowRestart && (
5082 < RestartChat
0 commit comments