Skip to content

Commit ee3d2cf

Browse files
Merge pull request #463 from johelvisguzman/issue461
Added a new repository specific exception
2 parents a3f8f05 + 97a61ac commit ee3d2cf

File tree

5 files changed

+68
-15
lines changed

5 files changed

+68
-15
lines changed

src/DotNetToolkit.Repository/Configuration/Options/RepositoryOptionsBuilder.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Mapper;
1111
using Properties;
1212
using System;
13-
using System.IO;
1413
using System.Linq;
1514
using Utility;
1615

@@ -78,7 +77,7 @@ public RepositoryOptionsBuilder([NotNull] IRepositoryOptions options)
7877
/// </summary>
7978
/// <param name="fileName">The name of the file to configure from.</param>
8079
/// <returns>The same builder instance.</returns>
81-
/// <remarks>Any element that is defined in the config file can be resolved using the <see cref="DotNetToolkit.Repository.Internal.ConfigFile.ConfigurationProvider.SetDefaultFactory"/></remarks>
80+
/// <remarks>Any element that is defined in the config file can be resolved using the <see cref="RepositoryDependencyResolver"/>.</remarks>
8281
public virtual RepositoryOptionsBuilder UseConfiguration([CanBeNull] string fileName = null)
8382
{
8483
const string SectionName = DotNetToolkit.Repository.Internal.ConfigFile.ConfigurationSection.SectionName;
@@ -91,7 +90,7 @@ public virtual RepositoryOptionsBuilder UseConfiguration([CanBeNull] string file
9190
var exeConfiguration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, System.Configuration.ConfigurationUserLevel.None);
9291

9392
if (!exeConfiguration.HasFile)
94-
throw new FileNotFoundException("The file is not found.", fileName);
93+
throw new System.IO.FileNotFoundException("The file is not found.", fileName);
9594

9695
config = (DotNetToolkit.Repository.Internal.ConfigFile.ConfigurationSection)exeConfiguration.GetSection(SectionName);
9796
}
@@ -114,7 +113,7 @@ public virtual RepositoryOptionsBuilder UseConfiguration([CanBeNull] string file
114113
/// </summary>
115114
/// <param name="configuration">The configuration.</param>
116115
/// <returns>The same builder instance.</returns>
117-
/// <remarks>Any element that is defined in the config file can be resolved using the <see cref="DotNetToolkit.Repository.Internal.ConfigFile.ConfigurationProvider.SetDefaultFactory"/></remarks>
116+
/// <remarks>Any element that is defined in the config file can be resolved using the <see cref="RepositoryDependencyResolver"/>.</remarks>
118117
public virtual RepositoryOptionsBuilder UseConfiguration([NotNull] Microsoft.Extensions.Configuration.IConfigurationRoot configuration)
119118
{
120119
Guard.NotNull(configuration, nameof(configuration));

src/DotNetToolkit.Repository/Properties/Resources.Designer.cs

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DotNetToolkit.Repository/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,7 @@
168168
<data name="UnableToResolveTypeWithDependencyResolver_ConfigFile" xml:space="preserve">
169169
<value>Unable to resolve an instance for '{0}'. Please consider using the {1} to use an IOC container or provide additional parameters to create an instance for the specified type. For more information on DotNetToolkit.Repository configuration, visit the https://github.com/johelvisguzman/DotNetToolkit.Repository/wiki/Config-File-Setup.</value>
170170
</data>
171+
<data name="RepositoryExceptionMessage" xml:space="preserve">
172+
<value>An error has occurred while performing an operation within the repository. Please see inner exception for fault details.</value>
173+
</data>
171174
</root>

src/DotNetToolkit.Repository/RepositoryBase.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ public void Delete([NotNull] TKey1 key1, [NotNull] TKey2 key2, [NotNull] TKey3 k
8080

8181
if (!TryDelete(key1, key2, key3))
8282
{
83-
var ex = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.EntityKeyNotFound, key1 + ", " + key2 + ", " + key3));
84-
85-
Logger.Error(ex);
86-
87-
throw ex;
83+
InterceptError(() => new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.EntityKeyNotFound, key1 + ", " + key2 + ", " + key3)));
8884
}
8985

9086
LogExecutedMethod(false);
@@ -2526,7 +2522,7 @@ protected void InterceptError([NotNull] Action action)
25262522
}
25272523
catch (Exception ex)
25282524
{
2529-
Logger.Error(ex);
2525+
Logger.Error(new RepositoryException(ex));
25302526

25312527
throw;
25322528
}
@@ -2546,7 +2542,7 @@ protected T InterceptError<T>([NotNull] Func<T> action)
25462542
}
25472543
catch (Exception ex)
25482544
{
2549-
Logger.Error(ex);
2545+
Logger.Error(new RepositoryException(ex));
25502546

25512547
throw;
25522548
}
@@ -2565,7 +2561,7 @@ protected async Task<T> InterceptErrorAsync<T>([NotNull] Func<Task<T>> action)
25652561
}
25662562
catch (Exception ex)
25672563
{
2568-
Logger.Error(ex);
2564+
Logger.Error(new RepositoryException(ex));
25692565

25702566
throw;
25712567
}
@@ -2584,7 +2580,7 @@ protected async Task InterceptErrorAsync([NotNull] Func<Task> action)
25842580
}
25852581
catch (Exception ex)
25862582
{
2587-
Logger.Error(ex);
2583+
Logger.Error(new RepositoryException(ex));
25882584

25892585
throw;
25902586
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace DotNetToolkit.Repository
2+
{
3+
using System;
4+
using System.Runtime.InteropServices;
5+
using Properties;
6+
#if !NETSTANDARD1_3
7+
using System.Runtime.Serialization;
8+
#endif
9+
10+
/// <summary>
11+
/// Represents errors that occur during application execution.
12+
/// </summary>
13+
[ComVisible(true)]
14+
#if !NETSTANDARD1_3
15+
[Serializable]
16+
#endif
17+
public class RepositoryException : Exception
18+
{
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="RepositoryException" /> class.
21+
/// </summary>
22+
public RepositoryException() { }
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="RepositoryException" /> class with a specified error message.
26+
/// </summary>
27+
public RepositoryException(string message) : base(message) { }
28+
29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="RepositoryException" /> class with a specified error message and a reference to the inner exception that is the cause of this exception.
31+
/// </summary>
32+
public RepositoryException(string message, Exception inner) : base(message, inner) { }
33+
34+
/// <summary>
35+
/// Initializes a new instance of the <see cref="RepositoryException" /> class with a reference to the inner exception that is the cause of this exception.
36+
/// </summary>
37+
public RepositoryException(Exception inner) : this(Resources.RepositoryExceptionMessage, inner) { }
38+
39+
#if !NETSTANDARD1_3
40+
/// <summary>
41+
/// Initializes a new instance of the <see cref="RepositoryException" /> class with serialized data.
42+
/// </summary>
43+
protected RepositoryException(SerializationInfo info, StreamingContext context) : base(info, context) { }
44+
#endif
45+
}
46+
}

0 commit comments

Comments
 (0)