Skip to content

Commit adac48b

Browse files
committed
Moved to catch(SqlException…)
Previously, we were catching `Exception`, as per Microsoft's guidance. This was due to the fact that there are a number of other exceptions that can be raised during the processing of a `SqlConnection` and, especially, a `SqlDataReader`. Most of those, however, are not _expected_ exceptions, and thus should not be handled (or even wrapped in a generic exception). In addition, moved `Exception` messages to use string interpolation for cleaner code.
1 parent 1a0a411 commit adac48b

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

Ignia.Topics.Data.Sql/SqlTopicRepository.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Data;
9+
using System.Data.Common;
910
using System.Data.SqlClient;
1011
using System.Diagnostics;
1112
using System.Diagnostics.Contracts;
@@ -362,8 +363,8 @@ public override Topic Load(string topicKey = null, bool isRecursive = true) {
362363
/*------------------------------------------------------------------------------------------------------------------------
363364
| Catch exception
364365
\-----------------------------------------------------------------------------------------------------------------------*/
365-
catch (Exception ex) {
366-
throw new Exception("Topic(s) failed to load: " + ex.Message);
366+
catch (SqlException exception) {
367+
throw new Exception($"Topic(s) failed to load: '{exception.Message}'");
367368
}
368369

369370
/*------------------------------------------------------------------------------------------------------------------------
@@ -501,8 +502,8 @@ public override Topic Load(int topicId, bool isRecursive = true) {
501502
/*------------------------------------------------------------------------------------------------------------------------
502503
| Catch exception
503504
\-----------------------------------------------------------------------------------------------------------------------*/
504-
catch (Exception ex) {
505-
throw new Exception("Topics failed to load: " + ex.Message);
505+
catch (SqlException exception) {
506+
throw new Exception($"Topics failed to load: '{exception.Message}'");
506507
}
507508

508509
/*------------------------------------------------------------------------------------------------------------------------
@@ -636,8 +637,8 @@ public override Topic Load(int topicId, DateTime version) {
636637
/*------------------------------------------------------------------------------------------------------------------------
637638
| Catch exception
638639
\-----------------------------------------------------------------------------------------------------------------------*/
639-
catch (Exception ex) {
640-
throw new Exception("Topics failed to load: " + ex.Message);
640+
catch (SqlException exception) {
641+
throw new Exception($"Topics failed to load: '{exception.Message}'");
641642
}
642643

643644
/*------------------------------------------------------------------------------------------------------------------------
@@ -844,8 +845,10 @@ public override int Save(Topic topic, bool isRecursive = false, bool isDraft = f
844845
/*------------------------------------------------------------------------------------------------------------------------
845846
| Catch exception
846847
\-----------------------------------------------------------------------------------------------------------------------*/
847-
catch (Exception ex) {
848-
throw new Exception("Failed to save Topic " + topic.Key + " (" + topic.Id + ") via " + _connectionString + ": " + ex.Message);
848+
catch (SqlException exception) {
849+
throw new Exception(
850+
$"Failed to save Topic '{topic.Key}' ({topic.Id}) via '{_connectionString}': '{exception.Message}'"
851+
);
849852
}
850853

851854
/*------------------------------------------------------------------------------------------------------------------------
@@ -930,8 +933,10 @@ public override void Move(Topic topic, Topic target, Topic sibling) {
930933
/*------------------------------------------------------------------------------------------------------------------------
931934
| Catch exception
932935
\-----------------------------------------------------------------------------------------------------------------------*/
933-
catch (Exception ex) {
934-
throw new Exception("Failed to move Topic " + topic.Key + " (" + topic.Id + ") to " + target.Key + " (" + target.Id + "): " + ex.Message);
936+
catch (SqlException exception) {
937+
throw new Exception(
938+
$"Failed to move Topic '{topic.Key}' ({topic.Id}) to '{target.Key}' ({target.Id}): '{exception.Message}'"
939+
);
935940
}
936941

937942
/*------------------------------------------------------------------------------------------------------------------------
@@ -997,8 +1002,8 @@ public override void Delete(Topic topic, bool isRecursive = false) {
9971002
/*------------------------------------------------------------------------------------------------------------------------
9981003
| Catch exception
9991004
\-----------------------------------------------------------------------------------------------------------------------*/
1000-
catch (Exception ex) {
1001-
throw new Exception("Failed to delete Topic " + topic.Key + " (" + topic.Id + "): " + ex.Message);
1005+
catch (SqlException exception) {
1006+
throw new Exception($"Failed to delete Topic '{topic.Key}' ({topic.Id}): '{exception.Message}'");
10021007
}
10031008

10041009
/*------------------------------------------------------------------------------------------------------------------------
@@ -1092,8 +1097,8 @@ private static string PersistRelations(Topic topic, SqlConnection connection, bo
10921097
/*------------------------------------------------------------------------------------------------------------------------
10931098
| Catch exception
10941099
\-----------------------------------------------------------------------------------------------------------------------*/
1095-
catch (Exception ex) {
1096-
throw new Exception("Failed to persist relationships for Topic " + topic.Key + " (" + topic.Id + "): " + ex.Message);
1100+
catch (SqlException exception) {
1101+
throw new Exception($"Failed to persist relationships for Topic '{topic.Key}' ({topic.Id}): '{exception.Message}'");
10971102
}
10981103

10991104
/*------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)