11import { assert , it } from "@effect/vitest" ;
2+ import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient" ;
23import * as Effect from "effect/Effect" ;
34import * as Schema from "effect/Schema" ;
5+ import * as SqlClient from "effect/unstable/sql/SqlClient" ;
6+ import { classifySqliteError , SqlError } from "effect/unstable/sql/SqlError" ;
47
58import { PersistenceDecodeError , PersistenceSqlError , toPersistenceSqlError } from "./Errors.ts" ;
69
@@ -25,16 +28,23 @@ it("keeps SQL operation context without a tautological detail", () => {
2528 assert . equal ( error . message , "SQL error in AuthSessionRepository.list:query" ) ;
2629} ) ;
2730
28- it ( "names the SQLite condition by its normalized result code" , ( ) => {
29- const cause = Object . assign ( new Error ( "UNIQUE constraint failed: orders.customer_email" ) , {
30- errcode : 1555 ,
31- errstr : "constraint failed" ,
32- } ) ;
33- const error = toPersistenceSqlError ( "OrchestrationCommandReceiptRepository.upsert:query" ) ( cause ) ;
31+ it . effect ( "names a real SQLite condition without copying query data" , ( ) =>
32+ Effect . gen ( function * ( ) {
33+ const sql = yield * SqlClient . SqlClient ;
34+ const payload = "sql-private-sentinel" ;
35+ yield * sql `CREATE TABLE error_test (private_column TEXT PRIMARY KEY)` ;
36+ yield * sql `INSERT INTO error_test VALUES (${ payload } )` ;
37+ const cause = yield * Effect . flip ( sql `INSERT INTO error_test VALUES (${ payload } )` ) ;
38+ const error = toPersistenceSqlError ( "OrchestrationCommandReceiptRepository.upsert:query" ) (
39+ cause ,
40+ ) ;
3441
35- assert . equal ( error . detail , "SQLITE(1555) constraint failed" ) ;
36- assert . equal ( error . cause , cause ) ;
37- } ) ;
42+ assert . equal ( error . detail , "SQLITE(1555) constraint failed" ) ;
43+ assert . equal ( error . cause , cause ) ;
44+ assert . notInclude ( error . message , payload ) ;
45+ assert . notInclude ( error . message , "private_column" ) ;
46+ } ) . pipe ( Effect . provide ( NodeSqliteClient . layerMemory ( ) ) ) ,
47+ ) ;
3848
3949it ( "reads the condition through a wrapping driver error" , ( ) => {
4050 const driver = Object . assign ( new Error ( "locked" ) , { errcode : 5 , errstr : "database is locked" } ) ;
@@ -45,18 +55,29 @@ it("reads the condition through a wrapping driver error", () => {
4555 assert . equal ( error . detail , "SQLITE(5) database is locked" ) ;
4656} ) ;
4757
48- it ( "keeps the driver's own prose out of the message" , ( ) => {
49- const cause = Object . assign ( new Error ( "UNIQUE constraint failed: orders.customer_email" ) , {
50- errcode : 1555 ,
51- errstr : "constraint failed" ,
52- } ) ;
53- const error = toPersistenceSqlError ( "AuthSessionRepository.create:query" ) ( cause ) ;
54-
55- assert . ok ( ! error . message . includes ( "customer_email" ) ) ;
56- } ) ;
58+ it . each ( [ { errno : 1555 , code : "SQLITE_CONSTRAINT_PRIMARYKEY" } , { errno : 1 } ] ) (
59+ "names Bun SQLite condition $errno through the SQL error wrapper" ,
60+ ( condition ) => {
61+ const driver = Object . assign ( new Error ( "bun-sql-private-sentinel" ) , {
62+ name : "SQLiteError" ,
63+ ...condition ,
64+ } ) ;
65+ const cause = new SqlError ( { reason : classifySqliteError ( driver ) } ) ;
66+ const error = toPersistenceSqlError ( "AuthSessionRepository.create:query" ) ( cause ) ;
67+
68+ assert . equal (
69+ error . message ,
70+ `SQL error in AuthSessionRepository.create:query: SQLITE(${ condition . errno } )` ,
71+ ) ;
72+ assert . equal ( error . cause , cause ) ;
73+ } ,
74+ ) ;
5775
58- it ( "omits a detail for a cause it cannot categorize" , ( ) => {
59- const error = toPersistenceSqlError ( "AuthSessionRepository.list:query" ) ( new Error ( "unhelpful" ) ) ;
76+ it . each ( [
77+ new Error ( "unhelpful" ) ,
78+ Object . assign ( new Error ( "file not found" ) , { errno : - 2 , code : "ENOENT" } ) ,
79+ ] ) ( "omits a detail for a cause it cannot categorize (%#)" , ( cause ) => {
80+ const error = toPersistenceSqlError ( "AuthSessionRepository.list:query" ) ( cause ) ;
6081
6182 assert . equal ( error . detail , undefined ) ;
6283 assert . equal ( error . message , "SQL error in AuthSessionRepository.list:query" ) ;
0 commit comments