IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
Exception.cs
Go to the documentation of this file.
1using System.Runtime.InteropServices;
2using System.Runtime.Serialization;
5
6namespace IDSImaging.Peak.ICV;
7
13[Serializable]
15{
17 internal IcvException(string message, Backend.Types.peak_icv_status errorCode)
18 : base(message)
19 {
20 ErrorCode = errorCode;
21 }
22
23 internal IcvException(SerializationInfo info, StreamingContext context)
24 : base(info, context)
25 {
26 ErrorCode = (Backend.Types.peak_icv_status)
27 info.GetValue(nameof(ErrorCode), typeof(Backend.Types.peak_icv_status));
28 }
29
30 internal Backend.Types.peak_icv_status ErrorCode { get; }
31
33 public override string Message => $"{base.Message} - ErrorCode: {ErrorCode}";
34
36 public override void GetObjectData(SerializationInfo info, StreamingContext context)
37 {
38 base.GetObjectData(info, context);
39 info.AddValue(nameof(ErrorCode), ErrorCode);
40 }
41}
42
49{
50 internal IcvInvalidConfigurationException(string message, Backend.Types.peak_icv_status errorCode)
51 : base(message, errorCode)
52 {
53 }
54}
55
62{
63 internal IcvNotSupportedException(string message)
64 : base(message, Backend.Types.peak_icv_status.PEAK_ICV_STATUS_NOT_SUPPORTED)
65 {
66 }
67}
68
75{
76 internal IcvCorruptedException(string message)
77 : base(message, Backend.Types.peak_icv_status.PEAK_ICV_STATUS_CORRUPTED)
78 {
79 }
80}
81
88{
89 internal IcvMismatchException(string message)
90 : base(message, Backend.Types.peak_icv_status.PEAK_ICV_STATUS_MISMATCH)
91 {
92 }
93}
94
102{
103 internal IcvNotPossibleException(string message)
104 : base(message, Backend.Types.peak_icv_status.PEAK_ICV_STATUS_NOT_POSSIBLE)
105 {
106 }
107}
108
115{
116 internal IcvNullPointerException(string message)
117 : base(message, Backend.Types.peak_icv_status.PEAK_ICV_STATUS_NULL_POINTER)
118 {
119 }
120}
121
128{
129 internal IcvOutOfRangeException(string message)
130 : base(message, Backend.Types.peak_icv_status.PEAK_ICV_STATUS_OUT_OF_RANGE)
131 {
132 }
133}
134
141{
142 internal IcvMathErrorException(string message)
143 : base(message, Backend.Types.peak_icv_status.PEAK_ICV_STATUS_MATH_ERROR)
144 {
145 }
146}
147
154{
155 internal IcvTargetNotFoundException(string message)
156 : base(message, Backend.Types.peak_icv_status.PEAK_ICV_STATUS_TARGET_NOT_FOUND)
157 {
158 }
159}
160
167{
168 internal IcvIOException(string message)
169 : base(message, Backend.Types.peak_icv_status.PEAK_ICV_STATUS_IO_ERROR)
170 {
171 }
172}
173
180{
181 internal IcvInternalErrorException(string message, Backend.Types.peak_icv_status errorCode)
182 : base(message, errorCode)
183 {
184 }
185}
186
187internal static class DllHelper
188{
193 public static void ExecuteAndMapReturnCodes(Func<Backend.Types.peak_icv_status> dllFunction)
194 {
195 Backend.Types.peak_icv_status errorCode = dllFunction();
196 if (errorCode == Backend.Types.peak_icv_status.PEAK_ICV_STATUS_SUCCESS)
197 {
198 return;
199 }
200
201 var errorText = GetLastError();
202 switch (errorCode)
203 {
204 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_LIBRARY_NOT_INITIALIZED:
205 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_DYNAMIC_DEPENDENCY_MISSING:
206 throw new IcvInvalidConfigurationException(errorText, errorCode);
207 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_MISMATCH:
208 throw new IcvMismatchException(errorText);
209 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_NOT_SUPPORTED:
210 throw new IcvNotSupportedException(errorText);
211 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_NOT_POSSIBLE:
212 throw new IcvNotPossibleException(errorText);
213 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_NULL_POINTER:
214 throw new IcvNullPointerException(errorText);
215 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_OUT_OF_RANGE:
216 throw new IcvOutOfRangeException(errorText);
217 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_MATH_ERROR:
218 throw new IcvMathErrorException(errorText);
219 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_TARGET_NOT_FOUND:
220 throw new IcvTargetNotFoundException(errorText);
221 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_CORRUPTED:
222 throw new IcvCorruptedException(errorText);
223 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_IO_ERROR:
224 throw new IcvIOException(errorText);
225 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_INTERNAL_ERROR:
226 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_INVALID_BUFFER_SIZE:
227 case Backend.Types.peak_icv_status.PEAK_ICV_STATUS_INVALID_HANDLE:
228 throw new IcvInternalErrorException(errorText, errorCode);
229
230 default:
231 throw new IcvException("Generic Error!", errorCode);
232 }
233 }
234
239 public static void ExecuteAndMapReturnCodesLight(Func<Backend.Types.peak_icv_status> dllFunction)
240 {
241 Backend.Types.peak_icv_status errorCode = dllFunction();
242 if (errorCode == Backend.Types.peak_icv_status.PEAK_ICV_STATUS_SUCCESS)
243 {
244 return;
245 }
246
247 throw new IcvException("Could not query the last error!", errorCode);
248 }
249
250 internal static string GetLastError()
251 {
252 var count = (nuint) 0u;
253 ExecuteAndMapReturnCodesLight(() => CInterface.peak_icv_GetLastErrorMessage_GetCount(out count));
254 var buffer = new byte[(int) count];
255 ExecuteAndMapReturnCodesLight(() => CInterface.peak_icv_GetLastErrorMessage(buffer, count));
256 return Utils.FromUtf8(buffer);
257 }
258
259 private static class CInterface
260 {
261 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
262 public static extern Backend.Types.peak_icv_status peak_icv_GetLastErrorMessage_GetCount(out nuint count);
263
264 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
265 public static extern Backend.Types.peak_icv_status peak_icv_GetLastErrorMessage(
266 byte[] lastErrorMessage,
267 nuint count);
268 }
269}
Base class for all exceptions thrown by the peak libraries.
Definition Exceptions.cs:18
Occurs when reading from a file or stream that is incomplete, damaged, or in an unexpected format.
Definition Exception.cs:75
Base class for all exceptions thrown by the library.
Definition Exception.cs:15
override void GetObjectData(SerializationInfo info, StreamingContext context)
Definition Exception.cs:36
Occurs when a file operation fails (e.g., missing file, insufficient permissions, or inaccessible dir...
Definition Exception.cs:167
Occurs when a failure happens during processing due to an unhandled internal routine condition.
Definition Exception.cs:180
Occurs when a required dependency is missing, the library is not initialized, or a misconfiguration a...
Definition Exception.cs:49
Occurs when a mathematical computation fails (e.g., division by zero or invalid matrix operations).
Definition Exception.cs:141
Occurs when input parameters do not match expected conditions (e.g., two input images have different ...
Definition Exception.cs:88
Occurs when an operation cannot be performed due to logical constraints (e.g., camera calibration wit...
Definition Exception.cs:102
Occurs when a requested operation is not implemented or supported (e.g., exporting to an unsupported ...
Definition Exception.cs:62
Occurs when attempting to access an object or value via an uninitialized or null pointer.
Definition Exception.cs:115
Occurs when a provided value falls outside the allowed valid range.
Definition Exception.cs:128
Occurs when an expected target object (e.g., calibration plate or marker) is not detected in the inpu...
Definition Exception.cs:154
Marker interface for exceptions indicating an I/O failure.
Marker interface for exceptions representing internal errors, which are thrown when an unexpected err...
Definition Exceptions.cs:74
Marker interface for exceptions indicating an unsupported operation.
Marker interface for exceptions indicating an out-of-range condition.