Skip to content

Commit ee00647

Browse files
authored
fix(dynamo): CNX-8394 Handle CA1031 warnings in Dynamo projects (#3145)
* fix(dynamo): CA1031 Do not catch general exceptions in Dynamo projects * fix(dyn): Last CA1031 warnings 💥 * fix(dyn): Formatting was wrong 🤷🏼‍♂️ * fix(dynamo): Upgraded log to fatal
1 parent 4853d35 commit ee00647

File tree

13 files changed

+104
-109
lines changed

13 files changed

+104
-109
lines changed

ConnectorDynamo/ConnectorDynamo.slnf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"Objects\\Converters\\ConverterRevit\\ConverterRevit2024\\ConverterRevit2024.csproj",
2424
"Objects\\Converters\\ConverterRevit\\ConverterRevitShared\\ConverterRevitShared.shproj",
2525
"Objects\\Objects\\Objects.csproj",
26-
"Objects\\Tests\\Objects.Tests.Unit\\Objects.Tests.Unit.csproj"
26+
"Objects\\Tests\\Objects.Tests.Unit\\Objects.Tests.Unit.csproj",
2727
"ConnectorRevit\\RevitSharedResources2020\\RevitSharedResources2020.csproj",
2828
"ConnectorRevit\\RevitSharedResources2021\\RevitSharedResources2021.csproj",
2929
"ConnectorRevit\\RevitSharedResources2022\\RevitSharedResources2022.csproj",

ConnectorDynamo/ConnectorDynamo/CreateStreamNode/CreateStream.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Speckle.Core.Api;
1111
using Speckle.Core.Credentials;
1212
using Speckle.Core.Logging;
13+
using Speckle.Core.Models.Extensions;
1314
using Account = Speckle.Core.Credentials.Account;
1415

1516
namespace Speckle.ConnectorDynamo.CreateStreamNode;
@@ -146,7 +147,7 @@ internal void DoCreateStream()
146147
CreateEnabled = false;
147148
SelectedUserId = SelectedAccount.userInfo.id;
148149

149-
this.Name = "Stream Created";
150+
Name = "Stream Created";
150151

151152
Analytics.TrackEvent(
152153
SelectedAccount,
@@ -156,22 +157,10 @@ internal void DoCreateStream()
156157

157158
OnNodeModified(true);
158159
}
159-
catch (Exception ex)
160+
catch (Exception ex) when (!ex.IsFatal())
160161
{
161-
//someone improve this pls :)
162-
if (ex.InnerException != null && ex.InnerException.InnerException != null)
163-
{
164-
Error(ex.InnerException.InnerException.Message);
165-
}
166-
167-
if (ex.InnerException != null)
168-
{
169-
Error(ex.InnerException.Message);
170-
}
171-
else
172-
{
173-
Error(ex.Message);
174-
}
162+
SpeckleLog.Logger.Error(ex, "Failed to create stream");
163+
Error("Failed to create stream: " + ex.ToFormattedString());
175164
}
176165
}
177166

ConnectorDynamo/ConnectorDynamo/ReceiveNode/Receive.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,15 @@ void ErrorAction(string transportName, Exception e)
325325
Message = "";
326326
}
327327
}
328-
catch (Exception e)
328+
catch (Exception ex) when (!ex.IsFatal())
329329
{
330330
if (!_cancellationToken.IsCancellationRequested)
331331
{
332332
_cancellationToken.Cancel();
333-
var msg = e.ToFormattedString();
333+
var msg = ex.ToFormattedString();
334334
Message = msg.Contains("401") || msg.Contains("don't have access") ? "Not authorized" : "Error";
335335
Warning(msg);
336-
_errors.Add(e);
336+
_errors.Add(ex);
337337
throw;
338338
}
339339
}
@@ -383,7 +383,7 @@ internal void LoadInputs(EngineController engine)
383383
CommitId = inputStream.CommitId
384384
};
385385
}
386-
catch
386+
catch (Exception ex) when (!ex.IsFatal())
387387
{
388388
// ignored
389389
}
@@ -396,7 +396,7 @@ internal void LoadInputs(EngineController engine)
396396
var url = GetInputAs<string>(engine, 0);
397397
newStream = new StreamWrapper(url);
398398
}
399-
catch
399+
catch (Exception ex) when (!ex.IsFatal())
400400
{
401401
// ignored
402402
}
@@ -520,7 +520,7 @@ private void CheckIfBehind()
520520
Message = "Up to date";
521521
}
522522
}
523-
catch (Exception ex)
523+
catch (Exception ex) when (!ex.IsFatal())
524524
{
525525
SpeckleLog.Logger.Error(
526526
ex,
@@ -547,7 +547,7 @@ private void GetExpiredObjectCount(string objectId)
547547
Message = "Updates available";
548548
_objectCount = count;
549549
}
550-
catch (Exception ex)
550+
catch (Exception ex) when (!ex.IsFatal())
551551
{
552552
SpeckleLog.Logger.Error(
553553
ex,

ConnectorDynamo/ConnectorDynamo/SendNode/Send.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,11 @@ internal void DoSend(EngineController engine)
259259
{
260260
@base = converter.ConvertRecursivelyToSpeckle(_data);
261261
}
262-
catch (Exception e)
262+
catch (Exception ex) when (!ex.IsFatal())
263263
{
264264
Message = "Conversion error";
265-
Warning(e.ToFormattedString());
266-
throw new SpeckleException("Conversion error", e);
265+
Warning(ex.ToFormattedString());
266+
throw new SpeckleException("Conversion error", ex);
267267
}
268268

269269
Message = "Sending...";
@@ -312,13 +312,13 @@ void ErrorAction(string transportName, Exception e)
312312
Message = "";
313313
}
314314
}
315-
catch (Exception e)
315+
catch (Exception ex) when (!ex.IsFatal())
316316
{
317317
if (!_cancellationToken.IsCancellationRequested)
318318
{
319319
_cancellationToken.Cancel();
320-
Message = e.InnerException != null ? e.InnerException.Message : e.Message;
321-
throw new SpeckleException(e.Message, e);
320+
Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
321+
throw new SpeckleException(ex.Message, ex);
322322
}
323323
}
324324
finally
@@ -367,8 +367,9 @@ internal void LoadInputs(EngineController engine)
367367
{
368368
_data = GetInputAs<object>(engine, 0, true);
369369
}
370-
catch
370+
catch (Exception ex) when (!ex.IsFatal())
371371
{
372+
SpeckleLog.Logger.Warning(ex, "Data input is invalid");
372373
ResetNode(true);
373374
Message = "Data input is invalid";
374375
return;
@@ -396,16 +397,17 @@ internal void LoadInputs(EngineController engine)
396397
_transports = transportsDict.Keys.ToList();
397398
_branchNames = transportsDict;
398399
}
399-
catch (Exception e)
400+
catch (Exception ex) when (!ex.IsFatal())
400401
{
402+
SpeckleLog.Logger.Warning(ex, "Send operation failed");
401403
//ignored
402404
ResetNode(true);
403-
Warning(e.InnerException?.Message ?? e.Message);
405+
Warning(ex.ToFormattedString());
404406
Message = "Not authorized";
405407
return;
406408
}
407409

408-
if (_transports == null || !_transports.Any())
410+
if (_transports == null || _transports.Count == 0)
409411
{
410412
ResetNode(true);
411413
Message = "Stream is invalid";
@@ -417,7 +419,7 @@ internal void LoadInputs(EngineController engine)
417419
{
418420
_commitMessage = InPorts[2].Connectors.Any() ? GetInputAs<string>(engine, 2) : ""; //IsConnected not working because has default value
419421
}
420-
catch
422+
catch (Exception ex) when (!ex.IsFatal())
421423
{
422424
Message = "Message is invalid, will skip it";
423425
}

ConnectorDynamo/ConnectorDynamoExtension/SpeckleExtension.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ public void Loaded(ViewLoadedParams viewLoadedParams)
3535

3636
Setup.Init(HostApplications.Dynamo.GetVersion(HostAppVersion.vRevit), HostApplications.Dynamo.Slug);
3737
}
38-
catch (Exception e) { }
38+
catch (Exception ex) when (!ex.IsFatal())
39+
{
40+
SpeckleLog.Logger.Fatal(ex, "Failed to load Speckle extension");
41+
}
3942
}
4043

4144
private void Rdm_RevitDocumentChanged(object sender, EventArgs e)

ConnectorDynamo/ConnectorDynamoExtension/SpeckleWatchHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using ProtoCore.Mirror;
88
using Speckle.Core.Credentials;
9+
using Speckle.Core.Logging;
910

1011
namespace Speckle.ConnectorDynamo.Extension;
1112

@@ -67,7 +68,7 @@ WatchHandlerCallback callback
6768
{
6869
return baseHandler.Process(data, preferredDictionaryOrdering, runtimeCore, tag, showRawData, callback);
6970
}
70-
catch (Exception)
71+
catch (Exception ex) when (!ex.IsFatal())
7172
{
7273
return callback(data.Data, preferredDictionaryOrdering, runtimeCore, tag, showRawData);
7374
}

ConnectorDynamo/ConnectorDynamoFunctions/Auto.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using Autodesk.DesignScript.Runtime;
67
using Speckle.Core.Credentials;
8+
using Speckle.Core.Logging;
79

810
namespace Speckle.ConnectorDynamo.Functions;
911

@@ -73,18 +75,18 @@ public static Dictionary<string, object> AutoReceive(object stream, bool enabled
7375
Task.Run(() =>
7476
{
7577
//try parse as streamWrapper
76-
if (stream is StreamWrapper)
78+
if (stream is StreamWrapper wrapper)
7779
{
78-
sw = (StreamWrapper)stream;
80+
sw = wrapper;
7981
}
8082
//try parse as Url
81-
else if (stream is string)
83+
else if (stream is string s)
8284
{
8385
try
8486
{
85-
sw = new StreamWrapper((string)stream);
87+
sw = new StreamWrapper(s);
8688
}
87-
catch
89+
catch (Exception ex) when (ex is NotSupportedException or SpeckleException)
8890
{
8991
// ignored
9092
}

ConnectorDynamo/ConnectorDynamoFunctions/BatchConverter.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private object TryConvertItemToSpeckle(object value)
177177
{
178178
return _converter.ConvertToSpeckle(value);
179179
}
180-
catch (Exception ex)
180+
catch (Exception ex) when (!ex.IsFatal())
181181
{
182182
var spcklEx = new SpeckleException($"Could not convert {value.GetType().Name} to Speckle:", ex);
183183
OnError?.Invoke(this, new OnErrorEventArgs(spcklEx));
@@ -332,9 +332,10 @@ private object TryConvertItemToNative(object value)
332332
{
333333
return _converter.ConvertToNative(@base);
334334
}
335-
catch (Exception e)
335+
catch (Exception ex) when (!ex.IsFatal())
336336
{
337-
var spcklError = new Exception($"Could not convert {@base.GetType().Name}(id={@base.id}) to Dynamo.", e);
337+
SpeckleLog.Logger.Error("Could not convert {typeName}(id={id}", @base.GetType().Name, @base.id);
338+
var spcklError = new SpeckleException($"Could not convert {@base.GetType().Name}(id={@base.id}) to Dynamo.", ex);
338339
OnError?.Invoke(this, new OnErrorEventArgs(spcklError));
339340
return null;
340341
}

ConnectorDynamo/ConnectorDynamoFunctions/Functions.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public static List<string> Send(
4343
{
4444
totalCount = data?.GetTotalChildrenCount() ?? 0;
4545
}
46-
catch (Exception e) { }
46+
catch (Exception ex) when (!ex.IsFatal())
47+
{
48+
SpeckleLog.Logger.Warning(ex, "Failed to get total children count.");
49+
}
4750

4851
if (totalCount == 0)
4952
{
@@ -104,7 +107,7 @@ public static List<string> Send(
104107
commitWrappers.Add(wrapper.ToString());
105108
Analytics.TrackEvent(client.Account, Analytics.Events.Send);
106109
}
107-
catch (Exception ex)
110+
catch (Exception ex) when (!ex.IsFatal())
108111
{
109112
Utils.HandleApiExeption(ex);
110113
return null;
@@ -159,9 +162,9 @@ public static Dictionary<string, object> Receive(
159162

160163
commit = branch.commits.items[0];
161164
}
162-
catch
165+
catch (Exception ex) when (!ex.IsFatal())
163166
{
164-
throw new SpeckleException("No branch found with name " + stream.BranchName);
167+
throw new SpeckleException("No branch found with name " + stream.BranchName, ex);
165168
}
166169
}
167170
else if (stream.Type == StreamWrapperType.Commit)
@@ -170,7 +173,7 @@ public static Dictionary<string, object> Receive(
170173
{
171174
commit = client.CommitGet(stream.StreamId, stream.CommitId!, cancellationToken).Result;
172175
}
173-
catch (Exception ex)
176+
catch (Exception ex) when (!ex.IsFatal())
174177
{
175178
Utils.HandleApiExeption(ex);
176179
return null;
@@ -209,6 +212,7 @@ public static Dictionary<string, object> Receive(
209212
{
210213
throw new SpeckleException("Receive operation returned nothing");
211214
}
215+
212216
try
213217
{
214218
client
@@ -223,9 +227,9 @@ public static Dictionary<string, object> Receive(
223227
)
224228
.Wait();
225229
}
226-
catch
230+
catch (Exception ex) when (!ex.IsFatal())
227231
{
228-
// Do nothing!
232+
SpeckleLog.Logger.Error(ex, "Failed to register commit receipt");
229233
}
230234

231235
if (cancellationToken.IsCancellationRequested)

ConnectorDynamo/ConnectorDynamoFunctions/Stream.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static object Get(
6262
);
6363
}
6464
}
65-
catch (Exception ex)
65+
catch (Exception ex) when (!ex.IsFatal())
6666
{
6767
Utils.HandleApiExeption(ex);
6868
}
@@ -112,9 +112,9 @@ public static StreamWrapper Update(
112112
{
113113
account = Task.Run(async () => await wrapper.GetAccount()).Result;
114114
}
115-
catch (Exception e)
115+
catch (Exception ex) when (!ex.IsFatal())
116116
{
117-
throw e.InnerException ?? e;
117+
throw ex.InnerException ?? ex;
118118
}
119119

120120
var client = new Client(account);
@@ -151,7 +151,7 @@ public static StreamWrapper Update(
151151
return wrapper;
152152
}
153153
}
154-
catch (Exception ex)
154+
catch (Exception ex) when (!ex.IsFatal())
155155
{
156156
Utils.HandleApiExeption(ex);
157157
}
@@ -191,9 +191,9 @@ public static object Details([ArbitraryDimensionArrayImport] object stream)
191191
{
192192
account = Task.Run(async () => await streamWrapper.GetAccount()).Result;
193193
}
194-
catch (Exception e)
194+
catch (Exception ex) when (!ex.IsFatal())
195195
{
196-
throw e.InnerException ?? e;
196+
throw ex.InnerException ?? ex;
197197
}
198198

199199
var client = new Client(account);
@@ -216,7 +216,7 @@ public static object Details([ArbitraryDimensionArrayImport] object stream)
216216
}
217217
);
218218
}
219-
catch (Exception ex)
219+
catch (Exception ex) when (!ex.IsFatal())
220220
{
221221
Utils.HandleApiExeption(ex);
222222
return details;
@@ -256,7 +256,9 @@ public static List<StreamWrapper> List(
256256
if (account == null)
257257
{
258258
Utils.HandleApiExeption(
259-
new Exception("No accounts found. Please use the Speckle Manager to manage your accounts on this computer.")
259+
new SpeckleAccountManagerException(
260+
"No accounts found. Please use the Speckle Manager to manage your accounts on this computer."
261+
)
260262
);
261263
}
262264

@@ -271,7 +273,7 @@ public static List<StreamWrapper> List(
271273
streamWrappers.Add(new StreamWrapper(x.id, account.userInfo.id, account.serverInfo.url));
272274
});
273275
}
274-
catch (Exception ex)
276+
catch (Exception ex) when (!ex.IsFatal())
275277
{
276278
Utils.HandleApiExeption(ex);
277279
}

0 commit comments

Comments
 (0)