Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit cb739b8

Browse files
committed
Added OrmLiteConfig.ExceptionFilter callback
1 parent c0cfdda commit cb739b8

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/ServiceStack.OrmLite/OrmLiteConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ public static IOrmLiteExecFilter ExecFilter
186186

187187
public static Func<FieldDefinition, object> OnDbNullFilter { get; set; }
188188

189+
public static Action<IDbCommand, Exception> ExceptionFilter { get; set; }
190+
189191
public static Func<string, string> SanitizeFieldNameForParamNameFn = fieldName =>
190192
(fieldName ?? "").Replace(" ", "");
191193

src/ServiceStack.OrmLite/OrmLiteExecFilter.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ public virtual T Exec<T>(IDbConnection dbConn, Func<IDbCommand, T> filter)
6161
var ret = filter(dbCmd);
6262
return ret;
6363
}
64+
catch (Exception ex)
65+
{
66+
if (OrmLiteConfig.ExceptionFilter != null)
67+
OrmLiteConfig.ExceptionFilter(dbCmd, ex);
68+
69+
throw;
70+
}
6471
finally
6572
{
6673
DisposeCommand(dbCmd, dbConn);
@@ -85,6 +92,13 @@ public virtual void Exec(IDbConnection dbConn, Action<IDbCommand> filter)
8592
{
8693
filter(dbCmd);
8794
}
95+
catch (Exception ex)
96+
{
97+
if (OrmLiteConfig.ExceptionFilter != null)
98+
OrmLiteConfig.ExceptionFilter(dbCmd, ex);
99+
100+
throw;
101+
}
88102
finally
89103
{
90104
DisposeCommand(dbCmd, dbConn);

tests/ServiceStack.OrmLite.Tests/LoggingTests.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using NUnit.Framework;
1+
using System;
2+
using System.Data;
3+
using NUnit.Framework;
24
using ServiceStack.DataAnnotations;
35
using ServiceStack.Logging;
46
using ServiceStack.Text;
@@ -31,8 +33,10 @@ public void Does_log_all_statements()
3133
db.DropTable<LogTest>();
3234
db.CreateTable<LogTest>();
3335

34-
db.Insert(new LogTest {
35-
CustomerId = 2, Name = "Foo"
36+
db.Insert(new LogTest
37+
{
38+
CustomerId = 2,
39+
Name = "Foo"
3640
});
3741

3842
var test = db.Single<LogTest>(x => x.CustomerId == 2);
@@ -55,5 +59,33 @@ public void Does_log_all_statements()
5559
Assert.That(logs, Is.StringContaining("DELETE FROM"));
5660
}
5761
}
62+
63+
[Test]
64+
public void Can_handle_sql_exceptions()
65+
{
66+
string lastSql = null;
67+
IDbDataParameter lastParam = null;
68+
OrmLiteConfig.ExceptionFilter = (dbCmd, ex) =>
69+
{
70+
lastSql = dbCmd.CommandText;
71+
lastParam = (IDbDataParameter)dbCmd.Parameters[0];
72+
};
73+
74+
using (var db = OpenDbConnection())
75+
{
76+
db.DropAndCreateTable<LogTest>();
77+
78+
try
79+
{
80+
var results = db.Select<LogTest>("Unknown = @arg", new { arg = "foo" });
81+
Assert.Fail("Should throw");
82+
}
83+
catch (Exception ex)
84+
{
85+
Assert.That(lastSql, Is.StringContaining("Unknown"));
86+
Assert.That(lastParam.Value, Is.EqualTo("foo"));
87+
}
88+
}
89+
}
5890
}
5991
}

0 commit comments

Comments
 (0)