feat(gemini): 增强 API 授权错误处理,自动提取并显示激活 URL
当 Gemini for Google Cloud API 未启用时(SERVICE_DISABLED 错误), 系统现在会: - 自动检测 403 PERMISSION_DENIED 错误 - 从错误响应中提取 API 激活 URL - 向用户显示清晰的错误消息和可点击的激活链接 - 提供操作指引(启用后等待几分钟) 新增文件: - internal/pkg/googleapi/error.go: Google API 错误解析器 - internal/pkg/googleapi/error_test.go: 完整的测试覆盖 - GEMINI_API_ERROR_HANDLING.md: 实现文档 修改文件: - internal/repository/geminicli_codeassist_client.go: 在 LoadCodeAssist 和 OnboardUser 中增强错误处理 这大大改善了用户体验,用户不再需要手动从错误日志中查找激活 URL。
This commit is contained in:
109
backend/internal/pkg/googleapi/error.go
Normal file
109
backend/internal/pkg/googleapi/error.go
Normal file
@@ -0,0 +1,109 @@
|
||||
// Package googleapi provides helpers for Google-style API responses.
|
||||
package googleapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErrorResponse represents a Google API error response
|
||||
type ErrorResponse struct {
|
||||
Error ErrorDetail `json:"error"`
|
||||
}
|
||||
|
||||
// ErrorDetail contains the error details from Google API
|
||||
type ErrorDetail struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Status string `json:"status"`
|
||||
Details []json.RawMessage `json:"details,omitempty"`
|
||||
}
|
||||
|
||||
// ErrorDetailInfo contains additional error information
|
||||
type ErrorDetailInfo struct {
|
||||
Type string `json:"@type"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
Domain string `json:"domain,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
// ErrorHelp contains help links
|
||||
type ErrorHelp struct {
|
||||
Type string `json:"@type"`
|
||||
Links []HelpLink `json:"links,omitempty"`
|
||||
}
|
||||
|
||||
// HelpLink represents a help link
|
||||
type HelpLink struct {
|
||||
Description string `json:"description"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// ParseError parses a Google API error response and extracts key information
|
||||
func ParseError(body string) (*ErrorResponse, error) {
|
||||
var errResp ErrorResponse
|
||||
if err := json.Unmarshal([]byte(body), &errResp); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse error response: %w", err)
|
||||
}
|
||||
return &errResp, nil
|
||||
}
|
||||
|
||||
// ExtractActivationURL extracts the API activation URL from error details
|
||||
func ExtractActivationURL(body string) string {
|
||||
var errResp ErrorResponse
|
||||
if err := json.Unmarshal([]byte(body), &errResp); err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Check error details for activation URL
|
||||
for _, detailRaw := range errResp.Error.Details {
|
||||
// Parse as ErrorDetailInfo
|
||||
var info ErrorDetailInfo
|
||||
if err := json.Unmarshal(detailRaw, &info); err == nil {
|
||||
if info.Metadata != nil {
|
||||
if activationURL, ok := info.Metadata["activationUrl"]; ok && activationURL != "" {
|
||||
return activationURL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse as ErrorHelp
|
||||
var help ErrorHelp
|
||||
if err := json.Unmarshal(detailRaw, &help); err == nil {
|
||||
for _, link := range help.Links {
|
||||
if strings.Contains(link.Description, "activation") ||
|
||||
strings.Contains(link.Description, "API activation") ||
|
||||
strings.Contains(link.URL, "/apis/api/") {
|
||||
return link.URL
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// IsServiceDisabledError checks if the error is a SERVICE_DISABLED error
|
||||
func IsServiceDisabledError(body string) bool {
|
||||
var errResp ErrorResponse
|
||||
if err := json.Unmarshal([]byte(body), &errResp); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if it's a 403 PERMISSION_DENIED with SERVICE_DISABLED reason
|
||||
if errResp.Error.Code != 403 || errResp.Error.Status != "PERMISSION_DENIED" {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, detailRaw := range errResp.Error.Details {
|
||||
var info ErrorDetailInfo
|
||||
if err := json.Unmarshal(detailRaw, &info); err == nil {
|
||||
if info.Reason == "SERVICE_DISABLED" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
143
backend/internal/pkg/googleapi/error_test.go
Normal file
143
backend/internal/pkg/googleapi/error_test.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package googleapi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExtractActivationURL(t *testing.T) {
|
||||
// Test case from the user's error message
|
||||
errorBody := `{
|
||||
"error": {
|
||||
"code": 403,
|
||||
"message": "Gemini for Google Cloud API has not been used in project project-6eca5881-ab73-4736-843 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudaicompanion.googleapis.com/overview?project=project-6eca5881-ab73-4736-843 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
|
||||
"status": "PERMISSION_DENIED",
|
||||
"details": [
|
||||
{
|
||||
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
|
||||
"reason": "SERVICE_DISABLED",
|
||||
"domain": "googleapis.com",
|
||||
"metadata": {
|
||||
"service": "cloudaicompanion.googleapis.com",
|
||||
"activationUrl": "https://console.developers.google.com/apis/api/cloudaicompanion.googleapis.com/overview?project=project-6eca5881-ab73-4736-843",
|
||||
"consumer": "projects/project-6eca5881-ab73-4736-843",
|
||||
"serviceTitle": "Gemini for Google Cloud API",
|
||||
"containerInfo": "project-6eca5881-ab73-4736-843"
|
||||
}
|
||||
},
|
||||
{
|
||||
"@type": "type.googleapis.com/google.rpc.LocalizedMessage",
|
||||
"locale": "en-US",
|
||||
"message": "Gemini for Google Cloud API has not been used in project project-6eca5881-ab73-4736-843 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudaicompanion.googleapis.com/overview?project=project-6eca5881-ab73-4736-843 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry."
|
||||
},
|
||||
{
|
||||
"@type": "type.googleapis.com/google.rpc.Help",
|
||||
"links": [
|
||||
{
|
||||
"description": "Google developers console API activation",
|
||||
"url": "https://console.developers.google.com/apis/api/cloudaicompanion.googleapis.com/overview?project=project-6eca5881-ab73-4736-843"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}`
|
||||
|
||||
activationURL := ExtractActivationURL(errorBody)
|
||||
expectedURL := "https://console.developers.google.com/apis/api/cloudaicompanion.googleapis.com/overview?project=project-6eca5881-ab73-4736-843"
|
||||
|
||||
if activationURL != expectedURL {
|
||||
t.Errorf("Expected activation URL %s, got %s", expectedURL, activationURL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsServiceDisabledError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
body string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "SERVICE_DISABLED error",
|
||||
body: `{
|
||||
"error": {
|
||||
"code": 403,
|
||||
"status": "PERMISSION_DENIED",
|
||||
"details": [
|
||||
{
|
||||
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
|
||||
"reason": "SERVICE_DISABLED"
|
||||
}
|
||||
]
|
||||
}
|
||||
}`,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Other 403 error",
|
||||
body: `{
|
||||
"error": {
|
||||
"code": 403,
|
||||
"status": "PERMISSION_DENIED",
|
||||
"details": [
|
||||
{
|
||||
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
|
||||
"reason": "OTHER_REASON"
|
||||
}
|
||||
]
|
||||
}
|
||||
}`,
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "404 error",
|
||||
body: `{
|
||||
"error": {
|
||||
"code": 404,
|
||||
"status": "NOT_FOUND"
|
||||
}
|
||||
}`,
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Invalid JSON",
|
||||
body: `invalid json`,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := IsServiceDisabledError(tt.body)
|
||||
if result != tt.expected {
|
||||
t.Errorf("Expected %v, got %v", tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseError(t *testing.T) {
|
||||
errorBody := `{
|
||||
"error": {
|
||||
"code": 403,
|
||||
"message": "API not enabled",
|
||||
"status": "PERMISSION_DENIED"
|
||||
}
|
||||
}`
|
||||
|
||||
errResp, err := ParseError(errorBody)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse error: %v", err)
|
||||
}
|
||||
|
||||
if errResp.Error.Code != 403 {
|
||||
t.Errorf("Expected code 403, got %d", errResp.Error.Code)
|
||||
}
|
||||
|
||||
if errResp.Error.Status != "PERMISSION_DENIED" {
|
||||
t.Errorf("Expected status PERMISSION_DENIED, got %s", errResp.Error.Status)
|
||||
}
|
||||
|
||||
if errResp.Error.Message != "API not enabled" {
|
||||
t.Errorf("Expected message 'API not enabled', got %s", errResp.Error.Message)
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/geminicli"
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/googleapi"
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
|
||||
"github.com/imroc/req/v3"
|
||||
@@ -38,9 +39,20 @@ func (c *geminiCliCodeAssistClient) LoadCodeAssist(ctx context.Context, accessTo
|
||||
return nil, fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
if !resp.IsSuccessState() {
|
||||
body := geminicli.SanitizeBodyForLogs(resp.String())
|
||||
fmt.Printf("[CodeAssist] LoadCodeAssist failed: status %d, body: %s\n", resp.StatusCode, body)
|
||||
return nil, fmt.Errorf("loadCodeAssist failed: status %d, body: %s", resp.StatusCode, body)
|
||||
body := resp.String()
|
||||
sanitizedBody := geminicli.SanitizeBodyForLogs(body)
|
||||
fmt.Printf("[CodeAssist] LoadCodeAssist failed: status %d, body: %s\n", resp.StatusCode, sanitizedBody)
|
||||
|
||||
// Check if this is a SERVICE_DISABLED error and extract activation URL
|
||||
if googleapi.IsServiceDisabledError(body) {
|
||||
activationURL := googleapi.ExtractActivationURL(body)
|
||||
if activationURL != "" {
|
||||
return nil, fmt.Errorf("Gemini for Google Cloud API is not enabled for this project. Please enable it by visiting: %s\n\nAfter enabling the API, wait a few minutes for the changes to propagate, then try again", activationURL)
|
||||
}
|
||||
return nil, fmt.Errorf("Gemini for Google Cloud API is not enabled for this project. Please enable it in the Google Cloud Console at: https://console.cloud.google.com/apis/library/cloudaicompanion.googleapis.com")
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("loadCodeAssist failed: status %d, body: %s", resp.StatusCode, sanitizedBody)
|
||||
}
|
||||
fmt.Printf("[CodeAssist] LoadCodeAssist success: status %d, response: %+v\n", resp.StatusCode, out)
|
||||
return &out, nil
|
||||
@@ -67,9 +79,20 @@ func (c *geminiCliCodeAssistClient) OnboardUser(ctx context.Context, accessToken
|
||||
return nil, fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
if !resp.IsSuccessState() {
|
||||
body := geminicli.SanitizeBodyForLogs(resp.String())
|
||||
fmt.Printf("[CodeAssist] OnboardUser failed: status %d, body: %s\n", resp.StatusCode, body)
|
||||
return nil, fmt.Errorf("onboardUser failed: status %d, body: %s", resp.StatusCode, body)
|
||||
body := resp.String()
|
||||
sanitizedBody := geminicli.SanitizeBodyForLogs(body)
|
||||
fmt.Printf("[CodeAssist] OnboardUser failed: status %d, body: %s\n", resp.StatusCode, sanitizedBody)
|
||||
|
||||
// Check if this is a SERVICE_DISABLED error and extract activation URL
|
||||
if googleapi.IsServiceDisabledError(body) {
|
||||
activationURL := googleapi.ExtractActivationURL(body)
|
||||
if activationURL != "" {
|
||||
return nil, fmt.Errorf("Gemini for Google Cloud API is not enabled for this project. Please enable it by visiting: %s\n\nAfter enabling the API, wait a few minutes for the changes to propagate, then try again", activationURL)
|
||||
}
|
||||
return nil, fmt.Errorf("Gemini for Google Cloud API is not enabled for this project. Please enable it in the Google Cloud Console at: https://console.cloud.google.com/apis/library/cloudaicompanion.googleapis.com")
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("onboardUser failed: status %d, body: %s", resp.StatusCode, sanitizedBody)
|
||||
}
|
||||
fmt.Printf("[CodeAssist] OnboardUser success: status %d, response: %+v\n", resp.StatusCode, out)
|
||||
return &out, nil
|
||||
|
||||
Reference in New Issue
Block a user