Skip to content

Commit

Permalink
feat: Added more request error types to the OpenAI module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yubyf committed Jul 22, 2023
1 parent 554acfe commit 7d3f5b5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(""))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
}
3 changes: 3 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@
<string name="module_openai_not_support_region">OpenAI 在您的 IP 所在地区不受支持</string>
<string name="module_openai_api_key_not_set">OpenAI API key 未设置</string>
<string name="module_openai_api_key_invalid">OpenAI API Key 无效</string>
<string name="module_openai_api_request_limit">达到 OpenAI API 请求限制,请检查您的账户或稍后重试</string>
<string name="module_openai_api_server_error">OpenAI 服务错误</string>
<string name="module_openai_api_engine_overloaded">OpenAI服务当前超载,请稍后重试</string>
<string name="module_openai_api_connect_error">OpenAI API 连接错误</string>
<string name="module_openai_expenditure_tooltips">使用 OpenAI 服务可能会产生一定的费用</string>
<string name="module_openai_incorrect_content_tooltips">GPT 有时会提供带有错误来源的格言</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@
<string name="module_openai_not_support_region">OpenAI 在您的 IP 所在地區不受支持</string>
<string name="module_openai_api_key_not_set">OpenAI API key 未設置</string>
<string name="module_openai_api_key_invalid">OpenAI API Key 無效</string>
<string name="module_openai_api_request_limit">達到 OpenAI API 請求限制,請檢查您的賬戶或稍後重試</string>
<string name="module_openai_api_server_error">OpenAI 服務錯誤</string>
<string name="module_openai_api_engine_overloaded">OpenAI服務當前超載,請稍後重試</string>
<string name="module_openai_api_connect_error">OpenAI API 連接錯誤</string>
<string name="module_openai_expenditure_tooltips">使用 OpenAI 服務可能會產生一定的費用</string>
<string name="module_openai_incorrect_content_tooltips">GPT 有時會提供帶有錯誤來源的格言</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@
<string name="module_openai_not_support_region">OpenAI is not supported in your IP\'s region</string>
<string name="module_openai_api_key_not_set">OpenAI API key not set</string>
<string name="module_openai_api_key_invalid">Invalid OpenAI API key</string>
<string name="module_openai_api_request_limit">OpenAI API request limit reached, please check your account or try again later</string>
<string name="module_openai_api_server_error">OpenAI server error</string>
<string name="module_openai_api_engine_overloaded">OpenAI server is currently overloaded, please try again later</string>
<string name="module_openai_api_connect_error">OpenAI API connect error</string>
<string name="module_openai_expenditure_tooltips">Using OpenAI provider may potentially result in certain expenditures</string>
<string name="module_openai_incorrect_content_tooltips">GPT sometimes provides quotes with erroneous sources</string>
Expand Down

0 comments on commit 7d3f5b5

Please sign in to comment.