diff --git a/app/src/main/java/com/crossbowffs/quotelock/app/configs/openai/OpenAIConfigsViewModel.kt b/app/src/main/java/com/crossbowffs/quotelock/app/configs/openai/OpenAIConfigsViewModel.kt index b01ec39..d4db2c7 100644 --- a/app/src/main/java/com/crossbowffs/quotelock/app/configs/openai/OpenAIConfigsViewModel.kt +++ b/app/src/main/java/com/crossbowffs/quotelock/app/configs/openai/OpenAIConfigsViewModel.kt @@ -101,23 +101,33 @@ class OpenAIConfigsViewModel @Inject constructor( openAIRepository.fetchAccountInfo() }.onFailure { Xlog.e(TAG, "Failed to validate API", it) - when (it) { - is OpenAIException.RegionNotSupportedException -> { - _uiEvent.emit(SnackBarEvent(message = AndroidString.StringRes(R.string.module_openai_not_support_region))) - } - - is OpenAIException.ApiKeyNotSetException -> { - _uiEvent.emit(SnackBarEvent(message = AndroidString.StringRes(R.string.module_openai_api_key_not_set))) - } - - is OpenAIException.ApiKeyInvalidException -> { - _uiEvent.emit(SnackBarEvent(message = AndroidString.StringRes(R.string.module_openai_api_key_invalid))) - } - - else -> { - _uiEvent.emit(SnackBarEvent(message = AndroidString.StringRes(R.string.module_openai_api_connect_error))) - } - } + _uiEvent.emit( + SnackBarEvent( + message = AndroidString.StringRes( + when (it) { + is OpenAIException.RegionNotSupportedException -> + R.string.module_openai_not_support_region + + is OpenAIException.ApiKeyNotSetException -> + R.string.module_openai_api_key_not_set + + is OpenAIException.ApiKeyInvalidException -> + R.string.module_openai_api_key_invalid + + is OpenAIException.RequestLimitException -> + R.string.module_openai_api_request_limit + + is OpenAIException.ServerError -> + R.string.module_openai_api_server_error + + is OpenAIException.EngineOverloadedError -> + R.string.module_openai_api_engine_overloaded + + else -> R.string.module_openai_api_connect_error + } + ) + ) + ) _uiState.value = _uiState.value.copy( validateResult = AsyncResult.Error.Message(AndroidString.StringText("")) ) diff --git a/app/src/main/java/com/crossbowffs/quotelock/data/modules/openai/OpenAIRepository.kt b/app/src/main/java/com/crossbowffs/quotelock/data/modules/openai/OpenAIRepository.kt index 45286cb..1553c44 100644 --- a/app/src/main/java/com/crossbowffs/quotelock/data/modules/openai/OpenAIRepository.kt +++ b/app/src/main/java/com/crossbowffs/quotelock/data/modules/openai/OpenAIRepository.kt @@ -281,13 +281,16 @@ class OpenAIRepository @Inject internal constructor( } return request(host) }.onFailure { - when { - it is HttpException && it.status == HttpStatusCode.Unauthorized -> { - throw OpenAIException.ApiKeyInvalidException + when (it) { + is HttpException -> throw when (it.status) { + HttpStatusCode.Unauthorized -> OpenAIException.ApiKeyInvalidException + HttpStatusCode.TooManyRequests -> OpenAIException.RequestLimitException + HttpStatusCode.InternalServerError -> OpenAIException.ServerError + HttpStatusCode.ServiceUnavailable -> OpenAIException.EngineOverloadedError + else -> OpenAIException.ConnectException } - it is OpenAIException -> throw it - + is OpenAIException -> throw it else -> throw OpenAIException.ConnectException } }.getOrNull() @@ -302,5 +305,8 @@ sealed class OpenAIException : IOException() { data class RegionNotSupportedException(val region: String? = null) : OpenAIException() object ApiKeyNotSetException : OpenAIException() object ApiKeyInvalidException : OpenAIException() + object RequestLimitException : OpenAIException() + object ServerError : OpenAIException() + object EngineOverloadedError : OpenAIException() object ConnectException : OpenAIException() } diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 3692394..b2e8e4f 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -179,6 +179,9 @@ OpenAI 在您的 IP 所在地区不受支持 OpenAI API key 未设置 OpenAI API Key 无效 + 达到 OpenAI API 请求限制,请检查您的账户或稍后重试 + OpenAI 服务错误 + OpenAI服务当前超载,请稍后重试 OpenAI API 连接错误 使用 OpenAI 服务可能会产生一定的费用 GPT 有时会提供带有错误来源的格言 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 641cde3..ee2d6b4 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -154,6 +154,9 @@ OpenAI 在您的 IP 所在地區不受支持 OpenAI API key 未設置 OpenAI API Key 無效 + 達到 OpenAI API 請求限制,請檢查您的賬戶或稍後重試 + OpenAI 服務錯誤 + OpenAI服務當前超載,請稍後重試 OpenAI API 連接錯誤 使用 OpenAI 服務可能會產生一定的費用 GPT 有時會提供帶有錯誤來源的格言 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9958b96..2980987 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -165,6 +165,9 @@ OpenAI is not supported in your IP\'s region OpenAI API key not set Invalid OpenAI API key + OpenAI API request limit reached, please check your account or try again later + OpenAI server error + OpenAI server is currently overloaded, please try again later OpenAI API connect error Using OpenAI provider may potentially result in certain expenditures GPT sometimes provides quotes with erroneous sources