|
| 1 | +import { expect } from "chai"; |
| 2 | +import { FirebaseError } from "../../error"; |
| 3 | +import { enhanceProvisioningError } from "./errorHandler"; |
| 4 | + |
| 5 | +describe("errorHandler", () => { |
| 6 | + describe("enhanceProvisioningError", () => { |
| 7 | + it("should include ErrorInfo details in error message", () => { |
| 8 | + const originalError = new FirebaseError("Permission denied", { |
| 9 | + context: { |
| 10 | + body: { |
| 11 | + error: { |
| 12 | + code: 403, |
| 13 | + message: "The user has not accepted the terms of service.", |
| 14 | + status: "PERMISSION_DENIED", |
| 15 | + details: [ |
| 16 | + { |
| 17 | + "@type": "type.googleapis.com/google.rpc.ErrorInfo", |
| 18 | + reason: |
| 19 | + "TOS_REQUIRED: The following ToS's must be accepted: [generative-language-api].", |
| 20 | + domain: "firebase.googleapis.com", |
| 21 | + }, |
| 22 | + ], |
| 23 | + }, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }); |
| 27 | + |
| 28 | + const result = enhanceProvisioningError(originalError, "Failed to provision Firebase app"); |
| 29 | + |
| 30 | + expect(result).to.be.instanceOf(FirebaseError); |
| 31 | + expect(result.message).to.include("Failed to provision Firebase app: Permission denied"); |
| 32 | + expect(result.message).to.include("Error details:"); |
| 33 | + expect(result.message).to.include( |
| 34 | + "Reason: TOS_REQUIRED: The following ToS's must be accepted: [generative-language-api].", |
| 35 | + ); |
| 36 | + expect(result.message).to.include("Domain: firebase.googleapis.com"); |
| 37 | + expect(result.exit).to.equal(2); |
| 38 | + expect(result.original).to.equal(originalError); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should include HelpLinks in error message", () => { |
| 42 | + const originalError = new FirebaseError("Permission denied", { |
| 43 | + context: { |
| 44 | + body: { |
| 45 | + error: { |
| 46 | + code: 403, |
| 47 | + message: "The user has not accepted the terms of service.", |
| 48 | + status: "PERMISSION_DENIED", |
| 49 | + details: [ |
| 50 | + { |
| 51 | + "@type": "type.googleapis.com/google.rpc.Help", |
| 52 | + links: [ |
| 53 | + { |
| 54 | + description: "Link to accept Generative Language terms of service", |
| 55 | + url: "https://console.cloud.google.com/apis/library/generativelanguage.googleapis.com?authuser=0&forceCheckTos=true", |
| 56 | + }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + const result = enhanceProvisioningError(originalError, "Failed to provision Firebase app"); |
| 66 | + |
| 67 | + expect(result.message).to.include("For help resolving this issue:"); |
| 68 | + expect(result.message).to.include("Link to accept Generative Language terms of service"); |
| 69 | + expect(result.message).to.include( |
| 70 | + "https://console.cloud.google.com/apis/library/generativelanguage.googleapis.com?authuser=0&forceCheckTos=true", |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should include both ErrorInfo and HelpLinks in error message", () => { |
| 75 | + const originalError = new FirebaseError("Permission denied", { |
| 76 | + context: { |
| 77 | + body: { |
| 78 | + error: { |
| 79 | + code: 403, |
| 80 | + message: "The user has not accepted the terms of service.", |
| 81 | + status: "PERMISSION_DENIED", |
| 82 | + details: [ |
| 83 | + { |
| 84 | + "@type": "type.googleapis.com/google.rpc.ErrorInfo", |
| 85 | + reason: |
| 86 | + "TOS_REQUIRED: The following ToS's must be accepted: [generative-language-api].", |
| 87 | + domain: "firebase.googleapis.com", |
| 88 | + }, |
| 89 | + { |
| 90 | + "@type": "type.googleapis.com/google.rpc.Help", |
| 91 | + links: [ |
| 92 | + { |
| 93 | + description: "Link to accept Generative Language terms of service", |
| 94 | + url: "https://console.cloud.google.com/apis/library/generativelanguage.googleapis.com?authuser=0&forceCheckTos=true", |
| 95 | + }, |
| 96 | + ], |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }); |
| 103 | + |
| 104 | + const result = enhanceProvisioningError(originalError, "Failed to provision Firebase app"); |
| 105 | + |
| 106 | + // Verify ErrorInfo is included |
| 107 | + expect(result.message).to.include("Error details:"); |
| 108 | + expect(result.message).to.include( |
| 109 | + "Reason: TOS_REQUIRED: The following ToS's must be accepted: [generative-language-api].", |
| 110 | + ); |
| 111 | + expect(result.message).to.include("Domain: firebase.googleapis.com"); |
| 112 | + |
| 113 | + // Verify HelpLinks are included |
| 114 | + expect(result.message).to.include("For help resolving this issue:"); |
| 115 | + expect(result.message).to.include("Link to accept Generative Language terms of service"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should include ErrorInfo with metadata in error message", () => { |
| 119 | + const originalError = new FirebaseError("Invalid request", { |
| 120 | + context: { |
| 121 | + body: { |
| 122 | + error: { |
| 123 | + code: 400, |
| 124 | + message: "Invalid request", |
| 125 | + status: "INVALID_ARGUMENT", |
| 126 | + details: [ |
| 127 | + { |
| 128 | + "@type": "type.googleapis.com/google.rpc.ErrorInfo", |
| 129 | + reason: "INVALID_FIELD", |
| 130 | + domain: "firebase.googleapis.com", |
| 131 | + metadata: { |
| 132 | + field: "displayName", |
| 133 | + constraint: "max_length", |
| 134 | + }, |
| 135 | + }, |
| 136 | + ], |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }); |
| 141 | + |
| 142 | + const result = enhanceProvisioningError(originalError, "Operation failed"); |
| 143 | + |
| 144 | + expect(result.message).to.include("Reason: INVALID_FIELD"); |
| 145 | + expect(result.message).to.include("Domain: firebase.googleapis.com"); |
| 146 | + expect(result.message).to.include("Additional Info:"); |
| 147 | + expect(result.message).to.include("field"); |
| 148 | + }); |
| 149 | + |
| 150 | + it("should include multiple help links in error message", () => { |
| 151 | + const originalError = new FirebaseError("Multiple help links", { |
| 152 | + context: { |
| 153 | + body: { |
| 154 | + error: { |
| 155 | + code: 403, |
| 156 | + message: "Permission denied", |
| 157 | + status: "PERMISSION_DENIED", |
| 158 | + details: [ |
| 159 | + { |
| 160 | + "@type": "type.googleapis.com/google.rpc.Help", |
| 161 | + links: [ |
| 162 | + { |
| 163 | + description: "First help link", |
| 164 | + url: "https://example.com/help1", |
| 165 | + }, |
| 166 | + { |
| 167 | + description: "Second help link", |
| 168 | + url: "https://example.com/help2", |
| 169 | + }, |
| 170 | + ], |
| 171 | + }, |
| 172 | + ], |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + }); |
| 177 | + |
| 178 | + const result = enhanceProvisioningError(originalError, "Operation failed"); |
| 179 | + |
| 180 | + expect(result.message).to.include("First help link"); |
| 181 | + expect(result.message).to.include("https://example.com/help1"); |
| 182 | + expect(result.message).to.include("Second help link"); |
| 183 | + expect(result.message).to.include("https://example.com/help2"); |
| 184 | + }); |
| 185 | + |
| 186 | + it("should handle errors without details gracefully", () => { |
| 187 | + const originalError = new FirebaseError("Firebase error without details", { |
| 188 | + context: { |
| 189 | + body: { |
| 190 | + error: { |
| 191 | + code: 400, |
| 192 | + message: "Bad Request", |
| 193 | + status: "INVALID_ARGUMENT", |
| 194 | + }, |
| 195 | + }, |
| 196 | + }, |
| 197 | + }); |
| 198 | + |
| 199 | + const result = enhanceProvisioningError(originalError, "Operation failed"); |
| 200 | + |
| 201 | + expect(result).to.be.instanceOf(FirebaseError); |
| 202 | + expect(result.message).to.equal("Operation failed: Firebase error without details"); |
| 203 | + expect(result.exit).to.equal(2); |
| 204 | + }); |
| 205 | + |
| 206 | + it("should handle non-Error types gracefully", () => { |
| 207 | + const result = enhanceProvisioningError("String error", "Operation failed"); |
| 208 | + |
| 209 | + expect(result).to.be.instanceOf(FirebaseError); |
| 210 | + expect(result.message).to.equal("Operation failed: String error"); |
| 211 | + expect(result.exit).to.equal(2); |
| 212 | + expect(result.original).to.be.instanceOf(Error); |
| 213 | + }); |
| 214 | + |
| 215 | + it("should handle regular Error without context", () => { |
| 216 | + const regularError = new Error("Regular error"); |
| 217 | + |
| 218 | + const result = enhanceProvisioningError(regularError, "Context message"); |
| 219 | + |
| 220 | + expect(result).to.be.instanceOf(FirebaseError); |
| 221 | + expect(result.message).to.equal("Context message: Regular error"); |
| 222 | + expect(result.original).to.equal(regularError); |
| 223 | + }); |
| 224 | + |
| 225 | + it("should ignore unknown detail types", () => { |
| 226 | + const originalError = new FirebaseError("Unknown detail type", { |
| 227 | + context: { |
| 228 | + body: { |
| 229 | + error: { |
| 230 | + code: 500, |
| 231 | + message: "Internal error", |
| 232 | + status: "INTERNAL", |
| 233 | + details: [ |
| 234 | + { |
| 235 | + "@type": "type.googleapis.com/google.rpc.UnknownType", |
| 236 | + someField: "someValue", |
| 237 | + }, |
| 238 | + ], |
| 239 | + }, |
| 240 | + }, |
| 241 | + }, |
| 242 | + }); |
| 243 | + |
| 244 | + const result = enhanceProvisioningError(originalError, "Operation failed"); |
| 245 | + |
| 246 | + expect(result.message).to.equal("Operation failed: Unknown detail type"); |
| 247 | + expect(result.message).to.not.include("Error details:"); |
| 248 | + expect(result.message).to.not.include("For help"); |
| 249 | + }); |
| 250 | + }); |
| 251 | +}); |
0 commit comments