Skip to content

Commit 9fdd75e

Browse files
committed
dss_sharp.Error.UseExceptions: Toggle mapping Error.Number to C# exceptions
Previously, we forced the use of exceptions. Now users can easily toggle the behavior. The default will still be to use exceptions for the safety of new users, besides the added convenience. **Usage:** set `dss_sharp.Error.UseExceptions = false` to deactivate the automatic mapping. Other toggles as `Error.EarlyAbort` and the upcoming `DSS.CompatFlags` should be enough to more closely match the logic of older programs using the official COM DLL behavior even for the more lax usage of OpenDSS. Closes #15
1 parent 7102dc6 commit 9fdd75e

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

src/dss_capi_lib.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// See LICENSE for more information.
77
//
88
// NOTE: This file is partially generated from other files. If you would like to contribute
9-
// a patch // or suggestion, please open a issue ticket on:
9+
// a patch or suggestion, please open an issue ticket on:
1010
// https://github.com/dss-extensions/dss_sharp/issues/
1111

1212
using System;

src/dss_sharp.cs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
// See LICENSE for more information.
77
//
88
// NOTE: This file is partially generated from other files. If you would like to contribute
9-
// a patch // or suggestion, please open a issue ticket on:
9+
// a patch or suggestion, please open an issue ticket on:
1010
// https://github.com/dss-extensions/dss_sharp/issues/
1111

1212
using System;
13-
using System.Runtime.InteropServices;
1413
using dss_sharp.detail;
1514
using dss_sharp.native;
1615

@@ -76,7 +75,7 @@ public enum ActionCodes {
7675
dssActionOpen = 1, // Open a switch
7776
dssActionClose = 2, // Close a switch
7877
dssActionReset = 3, // Reset to the shelf state (unlocked, closed for a switch)
79-
dssActionLock = 4, // Lock a switch, prventing both manual and automatic operation
78+
dssActionLock = 4, // Lock a switch, preventing both manual and automatic operation
8079
dssActionUnlock = 5, // Unlock a switch, permitting both manual and automatic operation
8180
dssActionTapUp = 6, // Move a regulator tap up
8281
dssActionTapDown = 7 // Move a regulator tap down
@@ -7276,14 +7275,7 @@ public string Description
72767275
{
72777276
get
72787277
{
7279-
try
7280-
{
7281-
return APIUtil.get_string(DSS_CAPI.ctx_Error_Get_Description(ctx));
7282-
}
7283-
finally
7284-
{
7285-
CheckForError();
7286-
}
7278+
return APIUtil.get_string(DSS_CAPI.ctx_Error_Get_Description(ctx));
72877279
}
72887280
}
72897281

@@ -7294,14 +7286,7 @@ public int Number
72947286
{
72957287
get
72967288
{
7297-
try
7298-
{
7299-
return DSS_CAPI.ctx_Error_Get_Number(ctx);
7300-
}
7301-
finally
7302-
{
7303-
CheckForError();
7304-
}
7289+
return DSS_CAPI.ctx_Error_Get_Number(ctx);
73057290
}
73067291
}
73077292

@@ -7348,9 +7333,8 @@ public bool EarlyAbort
73487333
///
73497334
/// Extended errors use the Error interface to provide a more clear message
73507335
/// and should help users, especially new users, to find usage issues earlier.
7351-
///
7352-
/// At Python level, an exception is raised when an error is detected through
7353-
/// the Error interface.
7336+
/// Combined with the automatic exception mapping mechanism (see `UseExceptions`),
7337+
/// a more modern and safe experience is achieved in dss_sharp.
73547338
///
73557339
/// The current default state is ON. For compatibility, the user can turn it
73567340
/// off to restore the previous behavior.
@@ -7382,6 +7366,31 @@ public bool ExtendedErrors
73827366
}
73837367
}
73847368
}
7369+
7370+
/// <summary>
7371+
/// UseExceptions controls whether the error numbers from the DSS engine are
7372+
/// automatically mapped to .NET exceptions. The default and recommended state
7373+
/// is `true`, but users can disable this to achieve better compatibility with
7374+
/// old, COM-based code. Note that most code from OpenDSS users "in the wild"
7375+
/// do not check for errors, so the exception mapping can unearth hidden errors
7376+
/// that were being ignored in old code.
7377+
///
7378+
/// NOTE: This is a global, static setting. Affects all DSS instances in the process.
7379+
///
7380+
/// (API Extension)
7381+
/// </summary>
7382+
public static bool UseExceptions
7383+
{
7384+
get
7385+
{
7386+
return APIUtil.exceptionsAllowed;
7387+
}
7388+
set
7389+
{
7390+
APIUtil.exceptionsAllowed = value;
7391+
}
7392+
}
7393+
73857394
}
73867395

73877396
public class Fuses : ContextState

src/dss_sharp_util.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public IntPtr GetContextHandle()
6363
/// </summary>
6464
public class APIUtil
6565
{
66+
public static bool exceptionsAllowed = true;
6667
private static bool MOLoaded = false;
6768
public delegate void StringArrayDelegate1(IntPtr ctx, ref IntPtr resultPtr, int[] resultCount);
6869
public delegate void StringArrayDelegate2(IntPtr ctx, ref IntPtr resultPtr, int[] resultCount, int param1);
@@ -210,8 +211,17 @@ public string[] get_string_array(StringArrayDelegate3 fn, string param1)
210211
return result;
211212
}
212213

214+
/// <summary>
215+
/// If the exception mapping mechanism is enabled, the DSS.Error.Number code is inspected.
216+
/// If non-zero, the matching DSS.Error.Description message is read and a DSSException is
217+
/// thrown. This is called internally after most of the dss_sharp DSS C-API calls to ensure
218+
/// most engine errors are being handled as early as possible.
219+
///
220+
/// If exceptions cannot be used, check `DSS.Error.Number` directly.
221+
/// </summary>
213222
public void CheckForError()
214223
{
224+
if (!exceptionsAllowed) return;
215225
var error_num = Marshal.ReadInt32(errorPtr);
216226
if (error_num != 0)
217227
{

0 commit comments

Comments
 (0)