Skip to content

Commit 161f6e9

Browse files
fix: replace assertions with descriptive exceptions in findTypeInfoBySqlEngineTypeId
Closes #354 Replace the cryptic assert/Seq.exactlyOne pattern in findTypeInfoBySqlEngineTypeId and findTypeInfoByProviderType with proper exceptions that include actionable diagnostic messages. This helps developers understand the cache invalidation race condition described in issue #354 instead of seeing an opaque assertion failure or 'Sequence contains no elements' error. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ac79dd6 commit 161f6e9

1 file changed

Lines changed: 31 additions & 10 deletions

File tree

src/SqlClient.DesignTime/SqlClientExtensions.fs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,40 @@ type internal TypeInfoPerConnectionStringCache() =
5757

5858
let internal sqlDataTypesCache = new TypeInfoPerConnectionStringCache()
5959
let internal findTypeInfoBySqlEngineTypeId (connStr, system_type_id, user_type_id : int option) =
60-
assert (sqlDataTypesCache.ContainsConnectionString connStr)
61-
62-
sqlDataTypesCache.GetTypesForConnectionString connStr
63-
|> Array.filter(fun x ->
64-
let result =
60+
if not (sqlDataTypesCache.ContainsConnectionString connStr) then
61+
failwithf
62+
"Type info cache does not contain an entry for connection string '%s'. \
63+
This may be caused by a cache invalidation race — try reloading the project. \
64+
See https://github.com/fsprojects/FSharp.Data.SqlClient/issues/354"
65+
connStr
66+
67+
let matches =
68+
sqlDataTypesCache.GetTypesForConnectionString connStr
69+
|> Array.filter (fun x ->
6570
x.SqlEngineTypeId = system_type_id &&
66-
(user_type_id.IsSome && x.UserTypeId = user_type_id.Value || user_type_id.IsNone && x.UserTypeId = int system_type_id)
67-
result
68-
)
69-
|> Seq.exactlyOne
71+
(user_type_id.IsSome && x.UserTypeId = user_type_id.Value
72+
|| user_type_id.IsNone && x.UserTypeId = int system_type_id))
73+
74+
match matches with
75+
| [| single |] -> single
76+
| [||] ->
77+
failwithf
78+
"No SQL type found in cache for SqlEngineTypeId=%d, UserTypeId=%A. \
79+
This may be caused by a cache invalidation race — try reloading the project. \
80+
See https://github.com/fsprojects/FSharp.Data.SqlClient/issues/354"
81+
system_type_id user_type_id
82+
| multiple ->
83+
failwithf
84+
"Ambiguous SQL type: %d matches found for SqlEngineTypeId=%d, UserTypeId=%A: %A"
85+
multiple.Length system_type_id user_type_id [| for t in multiple -> t.TypeName |]
7086

7187
let internal findTypeInfoByProviderType(connStr, sqlDbType) =
72-
assert (sqlDataTypesCache.ContainsConnectionString connStr)
88+
if not (sqlDataTypesCache.ContainsConnectionString connStr) then
89+
failwithf
90+
"Type info cache does not contain an entry for connection string '%s'. \
91+
This may be caused by a cache invalidation race — try reloading the project. \
92+
See https://github.com/fsprojects/FSharp.Data.SqlClient/issues/354"
93+
connStr
7394

7495
sqlDataTypesCache.GetTypesForConnectionString connStr |> Array.find (fun x -> x.SqlDbType = sqlDbType)
7596

0 commit comments

Comments
 (0)