comparison Source/Data/DataException.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Runtime.Serialization;
3
4 using BLToolkit.Data.DataProvider;
5
6 namespace BLToolkit.Data
7 {
8 /// <summary>
9 /// Defines the base class for the namespace exceptions.
10 /// </summary>
11 /// <remarks>
12 /// This class is the base class for exceptions that may occur during
13 /// execution of the namespace members.
14 /// </remarks>
15 [Serializable]
16 public class DataException : System.Data.DataException
17 {
18 /// <summary>
19 /// Initializes a new instance of the <see cref="DataException"/> class.
20 /// </summary>
21 /// <remarks>
22 /// This constructor initializes the <see cref="Exception.Message"/>
23 /// property of the new instance
24 /// to a system-supplied message that describes the error,
25 /// such as "BLToolkit Data error has occurred."
26 /// </remarks>
27 public DataException()
28 : base("A BLToolkit Data error has occurred.")
29 {
30 }
31
32 /// <summary>
33 /// Initializes a new instance of the <see cref="DataException"/> class
34 /// with the specified error message.
35 /// </summary>
36 /// <param name="message">The message to display to the client when the
37 /// exception is thrown.</param>
38 /// <seealso cref="Exception.Message"/>
39 public DataException(string message)
40 : base(message)
41 {
42 }
43
44 /// <summary>
45 /// Initializes a new instance of the <see cref="DataException"/> class
46 /// with the specified error message and InnerException property.
47 /// </summary>
48 /// <param name="message">The message to display to the client when the
49 /// exception is thrown.</param>
50 /// <param name="innerException">The InnerException, if any, that threw
51 /// the current exception.</param>
52 /// <seealso cref="Exception.Message"/>
53 /// <seealso cref="Exception.InnerException"/>
54 public DataException(string message, Exception innerException)
55 : base(message, innerException)
56 {
57 }
58
59 /// <summary>
60 /// Initializes a new instance of the <see cref="DataException"/> class
61 /// with the InnerException property.
62 /// </summary>
63 /// <param name="innerException">The InnerException, if any, that threw
64 /// the current exception.</param>
65 /// <seealso cref="Exception.InnerException"/>
66 public DataException(Exception innerException)
67 : base(innerException.Message, innerException)
68 {
69 }
70
71 /// <summary>
72 /// Initializes a new instance of the <see cref="DataException"/> class
73 /// with serialized data.
74 /// </summary>
75 /// <param name="info">The object that holds the serialized object data.</param>
76 /// <param name="context">The contextual information about the source or
77 /// destination.</param>
78 /// <remarks>This constructor is called during deserialization to
79 /// reconstitute the exception object transmitted over a stream.</remarks>
80 protected DataException(SerializationInfo info, StreamingContext context)
81 : base(info, context)
82 {
83 }
84
85 #region Internal
86
87 private readonly DbManager _dbManager;
88
89 static string GetMessage(DbManager dbManager, Exception innerException)
90 {
91 var obj = dbManager.DataProvider.Convert(
92 innerException, ConvertType.ExceptionToErrorMessage);
93
94 return obj is Exception ? ((Exception)obj).Message : obj.ToString();
95 }
96
97 internal DataException(DbManager dbManager, Exception innerException)
98 : this(GetMessage(dbManager, innerException), innerException)
99 {
100 _dbManager = dbManager;
101 }
102
103 #endregion
104
105 #region Public Properties
106
107 /// <summary>
108 /// Gets a number that identifies the type of error.
109 /// </summary>
110 public int? Number
111 {
112 get
113 {
114 var innerException = InnerException as DataException;
115 if (innerException != null)
116 return innerException.Number;
117 if (_dbManager == null) return null;
118 return _dbManager.DataProvider.Convert(InnerException, ConvertType.ExceptionToErrorNumber) as int?;
119 }
120 }
121
122 public DataExceptionType DataExceptionType
123 {
124 get
125 {
126 if (_dbManager == null || Number == null)
127 return DataExceptionType.Undefined;
128 return _dbManager.DataProvider.ConvertErrorNumberToDataExceptionType(Number.Value);
129 }
130 }
131
132 #endregion
133 }
134 }
135