|
16 | 16 |
|
17 | 17 | import { MySQLErrorHandler } from "../../mysql/lib/mysql_error_handler"; |
18 | 18 | import { PgErrorHandler } from "../../pg/lib/pg_error_handler"; |
| 19 | +import { AwsWrapperError } from "../../common/lib/utils/errors"; |
19 | 20 |
|
20 | 21 | function errorWith(props: Record<string, any>): Error { |
21 | 22 | return Object.assign(new Error("test"), props); |
22 | 23 | } |
23 | 24 |
|
| 25 | +function asAwsWrapperError(error: Error): Error { |
| 26 | + return new AwsWrapperError(error.message, error); |
| 27 | +} |
| 28 | + |
24 | 29 | describe("test read only connection error", () => { |
25 | | - const pgHandler = new PgErrorHandler(); |
26 | | - const mysqlHandler = new MySQLErrorHandler(); |
| 30 | + // mysql2 errors reach the plugins wrapped by ClientUtils, keeping the driver error as `cause`. |
| 31 | + describe("mysql", () => { |
| 32 | + const handler = new MySQLErrorHandler(); |
27 | 33 |
|
28 | | - it("test pg read only detected by sqlstate 25006", () => { |
29 | | - expect(pgHandler.isReadOnlyConnectionError(errorWith({ code: "25006" }))).toBe(true); |
30 | | - }); |
| 34 | + it("test read only errno 1290 detected", () => { |
| 35 | + expect(handler.isReadOnlyConnectionError(asAwsWrapperError(errorWith({ errno: 1290 })))).toBe(true); |
| 36 | + }); |
31 | 37 |
|
32 | | - it("test pg unrelated sqlstate not detected", () => { |
33 | | - expect(pgHandler.isReadOnlyConnectionError(errorWith({ code: "42601" }))).toBe(false); |
34 | | - }); |
| 38 | + it("test read only errno 1836 detected", () => { |
| 39 | + expect(handler.isReadOnlyConnectionError(asAwsWrapperError(errorWith({ errno: 1836 })))).toBe(true); |
| 40 | + }); |
35 | 41 |
|
36 | | - it("test pg error without code not detected", () => { |
37 | | - expect(pgHandler.isReadOnlyConnectionError(new Error("cannot execute in a read-only transaction"))).toBe(false); |
38 | | - }); |
| 42 | + it("test unrelated errno not detected", () => { |
| 43 | + expect(handler.isReadOnlyConnectionError(asAwsWrapperError(errorWith({ errno: 1064 })))).toBe(false); |
| 44 | + }); |
39 | 45 |
|
40 | | - it("test mysql read only detected by errno 1290", () => { |
41 | | - expect(mysqlHandler.isReadOnlyConnectionError(errorWith({ errno: 1290 }))).toBe(true); |
42 | | - }); |
| 46 | + it("test error without errno not detected", () => { |
| 47 | + expect(handler.isReadOnlyConnectionError(asAwsWrapperError(new Error("read only")))).toBe(false); |
| 48 | + }); |
43 | 49 |
|
44 | | - it("test mysql read only detected by errno 1836", () => { |
45 | | - expect(mysqlHandler.isReadOnlyConnectionError(errorWith({ errno: 1836 }))).toBe(true); |
| 50 | + it("test read only errno detected on an unwrapped error", () => { |
| 51 | + expect(handler.isReadOnlyConnectionError(errorWith({ errno: 1290 }))).toBe(true); |
| 52 | + }); |
46 | 53 | }); |
47 | 54 |
|
48 | | - it("test mysql unrelated errno not detected", () => { |
49 | | - expect(mysqlHandler.isReadOnlyConnectionError(errorWith({ errno: 1064 }))).toBe(false); |
50 | | - }); |
| 55 | + // pg hands the driver error to the plugins as-is. |
| 56 | + describe("pg", () => { |
| 57 | + const handler = new PgErrorHandler(); |
| 58 | + |
| 59 | + it("test read only sqlstate detected", () => { |
| 60 | + expect(handler.isReadOnlyConnectionError(errorWith({ code: "25006" }))).toBe(true); |
| 61 | + }); |
| 62 | + |
| 63 | + it("test unrelated sqlstate not detected", () => { |
| 64 | + expect(handler.isReadOnlyConnectionError(errorWith({ code: "42601" }))).toBe(false); |
| 65 | + }); |
| 66 | + |
| 67 | + it("test error without sqlstate not detected", () => { |
| 68 | + expect(handler.isReadOnlyConnectionError(new Error("cannot execute INSERT in a read-only transaction"))).toBe(false); |
| 69 | + }); |
51 | 70 |
|
52 | | - it("test mysql error without errno not detected", () => { |
53 | | - expect(mysqlHandler.isReadOnlyConnectionError(new Error("read only"))).toBe(false); |
| 71 | + it("test read only sqlstate detected on a wrapped error", () => { |
| 72 | + expect(handler.isReadOnlyConnectionError(asAwsWrapperError(errorWith({ code: "25006" })))).toBe(true); |
| 73 | + }); |
54 | 74 | }); |
55 | 75 | }); |
0 commit comments