( cause: unknown, message: string, operation: string )
| 471 | } |
| 472 | |
| 473 | const pgConstraintFromCause = (cause: unknown): string => { |
| 474 | if (typeof cause !== "object" || cause === null || !("constraint" in cause)) { |
| 475 | return "unknown" |
| 476 | } |
| 477 | const constraint = cause.constraint |
| 478 | if (typeof constraint !== "string") { |
| 479 | return "unknown" |
| 480 | } |
| 481 | const normalized = constraint.trim() |
| 482 | return normalized.length === 0 ? "unknown" : normalized |
| 483 | } |
| 484 | |
| 485 | const classifyError = ( |
| 486 | cause: unknown, |
| 487 | message: string, |
| 488 | operation: string |
| 489 | ) => { |
| 490 | const props = { cause, message, operation } |
| 491 | const code = pgCodeFromCause(cause) |
| 492 | if (code !== undefined) { |
| 493 | if (code.startsWith("08")) { |
| 494 | return new ConnectionError(props) |
| 495 | } |
| 496 | if (code.startsWith("28")) { |
| 497 | return new AuthenticationError(props) |
| 498 | } |
| 499 | if (code === "42501") { |
| 500 | return new AuthorizationError(props) |
| 501 | } |
| 502 | if (code.startsWith("42")) { |
| 503 | return new SqlSyntaxError(props) |
| 504 | } |
| 505 | if (code === "23505") { |
| 506 | return new UniqueViolation({ ...props, constraint: pgConstraintFromCause(cause) }) |
| 507 | } |
| 508 | if (code.startsWith("23")) { |
| 509 | return new ConstraintError(props) |
| 510 | } |
| 511 | if (code === "40P01") { |
| 512 | return new DeadlockError(props) |
| 513 | } |
| 514 | if (code === "40001") { |
| 515 | return new SerializationError(props) |
| 516 | } |
no test coverage detected
searching dependent graphs…