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

Commit 161e77d

Browse files
committed
Add OrmLiteConfig.ThrowOnError to throw instead of logging Errors
1 parent f561a01 commit 161e77d

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

src/ServiceStack.OrmLite/OrmLiteConfig.cs

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

196196
public static Action<IDbCommand, Exception> ExceptionFilter { get; set; }
197197

198+
public static bool ThrowOnError { get; set; }
199+
198200
public static Func<string, string> SanitizeFieldNameForParamNameFn = fieldName =>
199201
(fieldName ?? "").Replace(" ", "");
200202

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ public virtual bool PrepareParameterizedUpdateStatement<T>(IDbCommand cmd, IColl
799799
}
800800
catch (Exception ex)
801801
{
802-
Log.Error("ERROR in PrepareParameterizedUpdateStatement(): " + ex.Message, ex);
802+
OrmLiteUtils.HandleException(ex, "ERROR in PrepareParameterizedUpdateStatement(): " + ex.Message);
803803
}
804804
}
805805

@@ -870,7 +870,7 @@ public virtual bool PrepareParameterizedDeleteStatement<T>(IDbCommand cmd, IDict
870870
}
871871
catch (Exception ex)
872872
{
873-
Log.Error("ERROR in PrepareParameterizedDeleteStatement(): " + ex.Message, ex);
873+
OrmLiteUtils.HandleException(ex, "ERROR in PrepareParameterizedDeleteStatement(): " + ex.Message);
874874
}
875875
}
876876

@@ -1065,7 +1065,7 @@ public virtual void PrepareUpdateRowStatement(IDbCommand dbCmd, object objWithPr
10651065
}
10661066
catch (Exception ex)
10671067
{
1068-
Log.Error("ERROR in ToUpdateRowStatement(): " + ex.Message, ex);
1068+
OrmLiteUtils.HandleException(ex, "ERROR in ToUpdateRowStatement(): " + ex.Message);
10691069
}
10701070
}
10711071

@@ -1102,7 +1102,7 @@ public virtual void PrepareUpdateRowStatement<T>(IDbCommand dbCmd, Dictionary<st
11021102
}
11031103
catch (Exception ex)
11041104
{
1105-
Log.Error("ERROR in PrepareUpdateRowStatement(cmd,args): " + ex.Message, ex);
1105+
OrmLiteUtils.HandleException(ex, "ERROR in PrepareUpdateRowStatement(cmd,args): " + ex.Message);
11061106
}
11071107
}
11081108

@@ -1153,7 +1153,7 @@ public virtual void PrepareUpdateRowAddStatement<T>(IDbCommand dbCmd, Dictionary
11531153
}
11541154
catch (Exception ex)
11551155
{
1156-
Log.Error("ERROR in PrepareUpdateRowAddStatement(): " + ex.Message, ex);
1156+
OrmLiteUtils.HandleException(ex, "ERROR in PrepareUpdateRowAddStatement(): " + ex.Message);
11571157
}
11581158
}
11591159

src/ServiceStack.OrmLite/OrmLiteUtils.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ public static class OrmLiteUtils
3535
private static Dictionary<IndexFieldsCacheKey, Tuple<FieldDefinition, int, IOrmLiteConverter>[]> indexFieldsCache
3636
= new Dictionary<IndexFieldsCacheKey, Tuple<FieldDefinition, int, IOrmLiteConverter>[]>(maxCachedIndexFields);
3737

38+
private static readonly ILog Log = LogManager.GetLogger(typeof(OrmLiteUtils));
39+
40+
public static void HandleException(Exception ex, string message = null)
41+
{
42+
if (OrmLiteConfig.ThrowOnError)
43+
throw ex;
44+
45+
if (message != null)
46+
{
47+
Log.Error(message, ex);
48+
}
49+
else
50+
{
51+
Log.Error(ex);
52+
}
53+
}
54+
3855
public static void DebugCommand(this ILog log, IDbCommand cmd)
3956
{
4057
var sb = StringBuilderCache.Allocate();

src/ServiceStack.OrmLite/OrmLiteWriteCommandExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public static T PopulateWithSqlReader<T>(this T objWithProperties,
354354
}
355355
catch (Exception ex)
356356
{
357-
Log.Error(ex);
357+
OrmLiteUtils.HandleException(ex);
358358
}
359359
return objWithProperties;
360360
}

tests/ServiceStack.OrmLite.Tests/MaxDataTypeTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class MaxDataTypeTests : OrmLiteTestBase
1010
[Test]
1111
public void Can_insert_and_select_max_values()
1212
{
13+
//OrmLiteConfig.ThrowOnError = true;
14+
1315
var model = new ModelWithFieldsOfDifferentTypes
1416
{
1517
Int = int.MaxValue,

0 commit comments

Comments
 (0)