error_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package googleapi
  2. import (
  3. "testing"
  4. )
  5. func TestExtractActivationURL(t *testing.T) {
  6. // Test case from the user's error message
  7. errorBody := `{
  8. "error": {
  9. "code": 403,
  10. "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.",
  11. "status": "PERMISSION_DENIED",
  12. "details": [
  13. {
  14. "@type": "type.googleapis.com/google.rpc.ErrorInfo",
  15. "reason": "SERVICE_DISABLED",
  16. "domain": "googleapis.com",
  17. "metadata": {
  18. "service": "cloudaicompanion.googleapis.com",
  19. "activationUrl": "https://console.developers.google.com/apis/api/cloudaicompanion.googleapis.com/overview?project=project-6eca5881-ab73-4736-843",
  20. "consumer": "projects/project-6eca5881-ab73-4736-843",
  21. "serviceTitle": "Gemini for Google Cloud API",
  22. "containerInfo": "project-6eca5881-ab73-4736-843"
  23. }
  24. },
  25. {
  26. "@type": "type.googleapis.com/google.rpc.LocalizedMessage",
  27. "locale": "en-US",
  28. "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."
  29. },
  30. {
  31. "@type": "type.googleapis.com/google.rpc.Help",
  32. "links": [
  33. {
  34. "description": "Google developers console API activation",
  35. "url": "https://console.developers.google.com/apis/api/cloudaicompanion.googleapis.com/overview?project=project-6eca5881-ab73-4736-843"
  36. }
  37. ]
  38. }
  39. ]
  40. }
  41. }`
  42. activationURL := ExtractActivationURL(errorBody)
  43. expectedURL := "https://console.developers.google.com/apis/api/cloudaicompanion.googleapis.com/overview?project=project-6eca5881-ab73-4736-843"
  44. if activationURL != expectedURL {
  45. t.Errorf("Expected activation URL %s, got %s", expectedURL, activationURL)
  46. }
  47. }
  48. func TestIsServiceDisabledError(t *testing.T) {
  49. tests := []struct {
  50. name string
  51. body string
  52. expected bool
  53. }{
  54. {
  55. name: "SERVICE_DISABLED error",
  56. body: `{
  57. "error": {
  58. "code": 403,
  59. "status": "PERMISSION_DENIED",
  60. "details": [
  61. {
  62. "@type": "type.googleapis.com/google.rpc.ErrorInfo",
  63. "reason": "SERVICE_DISABLED"
  64. }
  65. ]
  66. }
  67. }`,
  68. expected: true,
  69. },
  70. {
  71. name: "Other 403 error",
  72. body: `{
  73. "error": {
  74. "code": 403,
  75. "status": "PERMISSION_DENIED",
  76. "details": [
  77. {
  78. "@type": "type.googleapis.com/google.rpc.ErrorInfo",
  79. "reason": "OTHER_REASON"
  80. }
  81. ]
  82. }
  83. }`,
  84. expected: false,
  85. },
  86. {
  87. name: "404 error",
  88. body: `{
  89. "error": {
  90. "code": 404,
  91. "status": "NOT_FOUND"
  92. }
  93. }`,
  94. expected: false,
  95. },
  96. {
  97. name: "Invalid JSON",
  98. body: `invalid json`,
  99. expected: false,
  100. },
  101. }
  102. for _, tt := range tests {
  103. t.Run(tt.name, func(t *testing.T) {
  104. result := IsServiceDisabledError(tt.body)
  105. if result != tt.expected {
  106. t.Errorf("Expected %v, got %v", tt.expected, result)
  107. }
  108. })
  109. }
  110. }
  111. func TestParseError(t *testing.T) {
  112. errorBody := `{
  113. "error": {
  114. "code": 403,
  115. "message": "API not enabled",
  116. "status": "PERMISSION_DENIED"
  117. }
  118. }`
  119. errResp, err := ParseError(errorBody)
  120. if err != nil {
  121. t.Fatalf("Failed to parse error: %v", err)
  122. }
  123. if errResp.Error.Code != 403 {
  124. t.Errorf("Expected code 403, got %d", errResp.Error.Code)
  125. }
  126. if errResp.Error.Status != "PERMISSION_DENIED" {
  127. t.Errorf("Expected status PERMISSION_DENIED, got %s", errResp.Error.Status)
  128. }
  129. if errResp.Error.Message != "API not enabled" {
  130. t.Errorf("Expected message 'API not enabled', got %s", errResp.Error.Message)
  131. }
  132. }