( cause: unknown, message: string, operation: string )
| 908 | return undefined |
| 909 | } |
| 910 | const code = cause.code |
| 911 | return typeof code === "string" ? code : undefined |
| 912 | } |
| 913 | |
| 914 | const pgConstraintFromCause = (cause: unknown): string => { |
| 915 | if (typeof cause !== "object" || cause === null || !("constraint" in cause)) { |
| 916 | return "unknown" |
| 917 | } |
| 918 | const constraint = cause.constraint |
| 919 | if (typeof constraint !== "string") { |
| 920 | return "unknown" |
| 921 | } |
| 922 | const normalized = constraint.trim() |
| 923 | return normalized.length === 0 ? "unknown" : normalized |
| 924 | } |
| 925 | |
| 926 | const classifyError = ( |
| 927 | cause: unknown, |
| 928 | message: string, |
| 929 | operation: string |
| 930 | ) => { |
| 931 | const props = { cause, message, operation } |
| 932 | const code = pgCodeFromCause(cause) |
| 933 | if (code !== undefined) { |
| 934 | if (code.startsWith("08")) { |
| 935 | return new ConnectionError(props) |
| 936 | } |
| 937 | if (code.startsWith("28")) { |
| 938 | return new AuthenticationError(props) |
| 939 | } |
| 940 | if (code === "42501") { |
| 941 | return new AuthorizationError(props) |
| 942 | } |
| 943 | if (code.startsWith("42")) { |
| 944 | return new SqlSyntaxError(props) |
| 945 | } |
| 946 | if (code === "23505") { |
| 947 | return new UniqueViolation({ ...props, constraint: pgConstraintFromCause(cause) }) |
| 948 | } |
| 949 | if (code.startsWith("23")) { |
| 950 | return new ConstraintError(props) |
| 951 | } |
| 952 | if (code === "40P01") { |
| 953 | return new DeadlockError(props) |
no test coverage detected
searching dependent graphs…