Skip to content

Commit b63dc07

Browse files
committed
Introduced TopicRepositoryException
Previously, the `SqlTopicRepository` was throwing generic `Exception`s. This is not a best practice. It is appropriate, however, for implementations of `ITopicRepository` to throw a generic, database agnostic exception (as opposed to e.g. `SqlException`) since it should not be tied to any specific persistence technology. The `TopicRepositoryException` allows it to accomplish this.
1 parent adac48b commit b63dc07

9 files changed

Lines changed: 87 additions & 21 deletions

File tree

Ignia.Topics.Data.Caching/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
[assembly: AssemblyTrademark("")]
2222
[assembly: AssemblyCulture("")]
2323
[assembly: ComVisible(false)]
24-
[assembly: AssemblyVersion("3.5.1758.0")]
25-
[assembly: AssemblyFileVersion("3.5.1790.0")]
24+
[assembly: AssemblyVersion("3.5.1759.0")]
25+
[assembly: AssemblyFileVersion("3.5.1791.0")]
2626
[assembly: CLSCompliant(true)]
2727
[assembly: Guid("206b7f91-ca25-4e9d-9576-60d2e54a2c0a")]
2828

Ignia.Topics.Data.Sql/SqlTopicRepository.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ public override Topic Load(string topicKey = null, bool isRecursive = true) {
364364
| Catch exception
365365
\-----------------------------------------------------------------------------------------------------------------------*/
366366
catch (SqlException exception) {
367-
throw new Exception($"Topic(s) failed to load: '{exception.Message}'");
367+
throw new TopicRepositoryException($"Topic(s) failed to load: '{exception.Message}'", exception);
368368
}
369369

370370
/*------------------------------------------------------------------------------------------------------------------------
@@ -503,7 +503,7 @@ public override Topic Load(int topicId, bool isRecursive = true) {
503503
| Catch exception
504504
\-----------------------------------------------------------------------------------------------------------------------*/
505505
catch (SqlException exception) {
506-
throw new Exception($"Topics failed to load: '{exception.Message}'");
506+
throw new TopicRepositoryException($"Topics failed to load: '{exception.Message}'", exception);
507507
}
508508

509509
/*------------------------------------------------------------------------------------------------------------------------
@@ -638,7 +638,7 @@ public override Topic Load(int topicId, DateTime version) {
638638
| Catch exception
639639
\-----------------------------------------------------------------------------------------------------------------------*/
640640
catch (SqlException exception) {
641-
throw new Exception($"Topics failed to load: '{exception.Message}'");
641+
throw new TopicRepositoryException($"Topics failed to load: '{exception.Message}'", exception);
642642
}
643643

644644
/*------------------------------------------------------------------------------------------------------------------------
@@ -846,8 +846,9 @@ public override int Save(Topic topic, bool isRecursive = false, bool isDraft = f
846846
| Catch exception
847847
\-----------------------------------------------------------------------------------------------------------------------*/
848848
catch (SqlException exception) {
849-
throw new Exception(
850-
$"Failed to save Topic '{topic.Key}' ({topic.Id}) via '{_connectionString}': '{exception.Message}'"
849+
throw new TopicRepositoryException(
850+
$"Failed to save Topic '{topic.Key}' ({topic.Id}) via '{_connectionString}': '{exception.Message}'",
851+
exception
851852
);
852853
}
853854

@@ -934,8 +935,9 @@ public override void Move(Topic topic, Topic target, Topic sibling) {
934935
| Catch exception
935936
\-----------------------------------------------------------------------------------------------------------------------*/
936937
catch (SqlException exception) {
937-
throw new Exception(
938-
$"Failed to move Topic '{topic.Key}' ({topic.Id}) to '{target.Key}' ({target.Id}): '{exception.Message}'"
938+
throw new TopicRepositoryException(
939+
$"Failed to move Topic '{topic.Key}' ({topic.Id}) to '{target.Key}' ({target.Id}): '{exception.Message}'",
940+
exception
939941
);
940942
}
941943

@@ -1003,7 +1005,10 @@ public override void Delete(Topic topic, bool isRecursive = false) {
10031005
| Catch exception
10041006
\-----------------------------------------------------------------------------------------------------------------------*/
10051007
catch (SqlException exception) {
1006-
throw new Exception($"Failed to delete Topic '{topic.Key}' ({topic.Id}): '{exception.Message}'");
1008+
throw new TopicRepositoryException(
1009+
$"Failed to delete Topic '{topic.Key}' ({topic.Id}): '{exception.Message}'",
1010+
exception
1011+
);
10071012
}
10081013

10091014
/*------------------------------------------------------------------------------------------------------------------------
@@ -1098,7 +1103,10 @@ private static string PersistRelations(Topic topic, SqlConnection connection, bo
10981103
| Catch exception
10991104
\-----------------------------------------------------------------------------------------------------------------------*/
11001105
catch (SqlException exception) {
1101-
throw new Exception($"Failed to persist relationships for Topic '{topic.Key}' ({topic.Id}): '{exception.Message}'");
1106+
throw new TopicRepositoryException(
1107+
$"Failed to persist relationships for Topic '{topic.Key}' ({topic.Id}): '{exception.Message}'",
1108+
exception
1109+
);
11021110
}
11031111

11041112
/*------------------------------------------------------------------------------------------------------------------------

Ignia.Topics.Tests/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
[assembly: AssemblyTrademark("")]
2222
[assembly: AssemblyCulture("")]
2323
[assembly: ComVisible(false)]
24-
[assembly: AssemblyVersion("3.5.1787.0")]
25-
[assembly: AssemblyFileVersion("3.5.1835.0")]
24+
[assembly: AssemblyVersion("3.5.1788.0")]
25+
[assembly: AssemblyFileVersion("3.5.1836.0")]
2626
[assembly: CLSCompliant(true)]
2727
[assembly: Guid("27632801-bfe3-41d9-8678-3c4bbe45e6c9")]

Ignia.Topics.ViewModels/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
[assembly: AssemblyTrademark("")]
2222
[assembly: AssemblyCulture("")]
2323
[assembly: ComVisible(false)]
24-
[assembly: AssemblyVersion("3.5.1758.0")]
25-
[assembly: AssemblyFileVersion("3.5.1789.0")]
24+
[assembly: AssemblyVersion("3.5.1759.0")]
25+
[assembly: AssemblyFileVersion("3.5.1790.0")]
2626
[assembly: CLSCompliant(true)]
2727
[assembly: Guid("e52fc633-b4c5-4a2b-8caf-30e756d7a6a7")]
2828

Ignia.Topics.Web.Mvc/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
[assembly: AssemblyTrademark("")]
2222
[assembly: AssemblyCulture("")]
2323
[assembly: ComVisible(false)]
24-
[assembly: AssemblyVersion("3.5.1763.0")]
25-
[assembly: AssemblyFileVersion("3.5.1795.0")]
24+
[assembly: AssemblyVersion("3.5.1764.0")]
25+
[assembly: AssemblyFileVersion("3.5.1796.0")]
2626
[assembly: CLSCompliant(true)]
2727
[assembly: Guid("3b3ce34d-b5e5-47ca-bfef-e6740650f378")]

Ignia.Topics.Web/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
[assembly: AssemblyTrademark("")]
2222
[assembly: AssemblyCulture("")]
2323
[assembly: ComVisible(false)]
24-
[assembly: AssemblyVersion("3.5.1755.0")]
25-
[assembly: AssemblyFileVersion("3.5.1779.0")]
24+
[assembly: AssemblyVersion("3.5.1756.0")]
25+
[assembly: AssemblyFileVersion("3.5.1780.0")]
2626
[assembly: CLSCompliant(true)]
2727
[assembly: Guid("c98f7b48-a085-4394-b820-c244f23868ce")]

Ignia.Topics/Ignia.Topics.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
<Compile Include="Reflection\MemberInfoCollection{T}.cs" />
176176
<Compile Include="Reflection\TypeCollection.cs" />
177177
<Compile Include="DefaultTopicLookupService.cs" />
178+
<Compile Include="Repositories\TopicRepositoryException.cs" />
178179
<Compile Include="StaticTypeLookupService.cs" />
179180
<Compile Include="Repositories\DeleteEventArgs.cs" />
180181
<Compile Include="Repositories\ITopicRepository.cs" />

Ignia.Topics/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
[assembly: AssemblyTrademark("")]
2323
[assembly: AssemblyCulture("")]
2424
[assembly: ComVisible(false)]
25-
[assembly: AssemblyVersion("3.5.1758.0")]
26-
[assembly: AssemblyFileVersion("3.5.1790.0")]
25+
[assembly: AssemblyVersion("3.5.1759.0")]
26+
[assembly: AssemblyFileVersion("3.5.1791.0")]
2727
[assembly: InternalsVisibleTo("Ignia.Topics.Tests")]
2828
[assembly: CLSCompliant(true)]
2929
[assembly: GuidAttribute("3CA9F6CB-B45A-4E74-AAA4-0C87CAA2704F")]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*==============================================================================================================================
2+
| Author Ignia, LLC
3+
| Client Ignia, LLC
4+
| Project Topics Library
5+
\=============================================================================================================================*/
6+
using System;
7+
using System.Data.Common;
8+
using System.Diagnostics.Contracts;
9+
using System.Runtime.Serialization;
10+
11+
namespace Ignia.Topics.Repositories {
12+
13+
/*============================================================================================================================
14+
| CLASS: TOPIC REPOSITORY EXCEPTION
15+
\---------------------------------------------------------------------------------------------------------------------------*/
16+
/// <summary>
17+
/// The <see cref="TopicRepositoryException"/> provides a general exception that can be thrown for any persistence errors
18+
/// that arise from concrete <see cref="ITopicRepository"/> implementations.
19+
/// </summary>
20+
/// <remarks>
21+
/// Microsoft provides a set of <see cref="DbException"/> classes, such as <see cref="SqlException"/>, which are specific to
22+
/// their target implementations. Since <see cref="ITopicRepository"/>, however, is intended to be database agnostic, none
23+
/// of these are appropriate to catch when implementing <see cref="ITopicRepository"/>. Instead, the <see
24+
/// cref="TopicRepositoryException"/> provides a database agnostic version of an exception that can provide a wrapper
25+
/// around any of these more concrete exceptions.
26+
/// </remarks>
27+
public class TopicRepositoryException : DbException {
28+
29+
/*==========================================================================================================================
30+
| CONSTRUCTOR: TAXONOMY DELETE EVENT ARGS
31+
\-------------------------------------------------------------------------------------------------------------------------*/
32+
/// <summary>
33+
/// Initializes a new <see cref="TopicRepositoryException" /> instance with a specific error message.
34+
/// </summary>
35+
/// <param name="message">The message to display for this exception.</param>
36+
public TopicRepositoryException(string message) : base(message) {}
37+
38+
/// <summary>
39+
/// Initializes a new <see cref="TopicRepositoryException" /> instance with a specific error message and nested exception.
40+
/// </summary>
41+
/// <param name="message">The message to display for this exception.</param>
42+
/// <param name="innerException">The reference to the original, underlying exception.</param>
43+
public TopicRepositoryException(string message, Exception innerException) : base(message, innerException) { }
44+
45+
/// <summary>
46+
/// Instantiates a new <see cref="TopicRepositoryException"/> instance for serialization.
47+
/// </summary>
48+
/// <param name="info">A <see cref="SerializationInfo"/> instance with details about the serialization requirements.</param>
49+
/// <param name="context">A <see cref="StreamingContext"/> instance with details about the request context.</param>
50+
/// <returns>A new <see cref="InvalidKeyException"/> instance.</returns>
51+
protected TopicRepositoryException(SerializationInfo info, StreamingContext context) : base(info, context) {
52+
Contract.Requires<ArgumentNullException>(info != null);
53+
}
54+
55+
} // Class
56+
57+
} // Namespace

0 commit comments

Comments
 (0)