|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { handleError } from "../../core/handle-errors"; |
| 3 | +import { mockConsoleMethods } from "../helpers/mock-console"; |
| 4 | + |
| 5 | +describe("handleError", () => { |
| 6 | + const std = mockConsoleMethods(); |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + vi.clearAllMocks(); |
| 10 | + }); |
| 11 | + |
| 12 | + describe("SSL/TLS certificate errors", () => { |
| 13 | + it("should log a warning for self-signed certificate errors", async () => { |
| 14 | + const error = new Error("self-signed certificate in certificate chain"); |
| 15 | + |
| 16 | + await handleError(error, {}, []); |
| 17 | + |
| 18 | + expect(std.warn).toContain( |
| 19 | + "Wrangler detected that a corporate proxy or VPN might be enabled" |
| 20 | + ); |
| 21 | + expect(std.warn).toContain("certificate mismatch"); |
| 22 | + expect(std.warn).toContain("install the missing system roots"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should log a warning for unable to verify first certificate errors", async () => { |
| 26 | + const error = new Error("unable to verify the first certificate"); |
| 27 | + |
| 28 | + await handleError(error, {}, []); |
| 29 | + |
| 30 | + expect(std.warn).toContain( |
| 31 | + "Wrangler detected that a corporate proxy or VPN might be enabled" |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should log a warning for unable to get local issuer certificate errors", async () => { |
| 36 | + const error = new Error("unable to get local issuer certificate"); |
| 37 | + |
| 38 | + await handleError(error, {}, []); |
| 39 | + |
| 40 | + expect(std.warn).toContain( |
| 41 | + "Wrangler detected that a corporate proxy or VPN might be enabled" |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should log a warning when certificate error is in error cause", async () => { |
| 46 | + const cause = new Error("self-signed certificate in certificate chain"); |
| 47 | + const error = new Error("fetch failed", { cause }); |
| 48 | + |
| 49 | + await handleError(error, {}, []); |
| 50 | + |
| 51 | + expect(std.warn).toContain( |
| 52 | + "Wrangler detected that a corporate proxy or VPN might be enabled" |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should log a warning when certificate error is part of a longer message", async () => { |
| 57 | + const error = new Error( |
| 58 | + "Request failed: self-signed certificate in certificate chain (details: some info)" |
| 59 | + ); |
| 60 | + |
| 61 | + await handleError(error, {}, []); |
| 62 | + |
| 63 | + expect(std.warn).toContain( |
| 64 | + "Wrangler detected that a corporate proxy or VPN might be enabled" |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should not log a warning for unrelated errors", async () => { |
| 69 | + const error = new Error("Connection refused"); |
| 70 | + |
| 71 | + await handleError(error, {}, []); |
| 72 | + |
| 73 | + expect(std.warn).not.toContain( |
| 74 | + "Wrangler detected that a corporate proxy or VPN might be enabled" |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should still log the original error after the warning", async () => { |
| 79 | + const error = new Error("self-signed certificate in certificate chain"); |
| 80 | + |
| 81 | + await handleError(error, {}, []); |
| 82 | + |
| 83 | + // The warning should appear |
| 84 | + expect(std.warn).toContain( |
| 85 | + "Wrangler detected that a corporate proxy or VPN might be enabled" |
| 86 | + ); |
| 87 | + // The original error should also be logged |
| 88 | + expect(std.err).toContain("self-signed certificate in certificate chain"); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments