Skip to content

Commit

Permalink
[foundation] Enable nullable for MonoTouchException (#12350)
Browse files Browse the repository at this point in the history
and avoid potential `NullReferenceException` in some code paths.

Also remove multiple string concatenations by using `StringBuilder`
  • Loading branch information
spouliot authored Aug 5, 2021
1 parent 9dbf451 commit 9844af7
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/Foundation/MonoTouchException.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#if !MONOMAC

using System;
using System.Text;

#nullable enable

namespace Foundation {
public class MonoTouchException : Exception {
private NSException native_exc;
NSException? native_exc;

public MonoTouchException () : base () {
native_exc = new NSException ("default", String.Empty, null);
Expand All @@ -14,49 +17,51 @@ public MonoTouchException (NSException exc) : base () {
native_exc = exc;
}

public NSException NSException {
public NSException? NSException {
get {
return native_exc;
}
}

public string Reason {
public string? Reason {
get {
return native_exc.Reason;
return native_exc?.Reason;
}
}

public string Name {
public string? Name {
get {
return native_exc.Name;
return native_exc?.Name;
}
}

public override string Message {
get {
return string.Format ("Objective-C exception thrown. Name: {0} Reason: {1}\nNative stack trace:\n{2}", Name, Reason, NativeStackTrace);
var sb = new StringBuilder ("Objective-C exception thrown. Name: ").Append (Name);
sb.Append (" Reason: ").AppendLine (Reason);
sb.AppendLine ("Native stack trace:");
AppendNativeStackTrace (sb);
return sb.ToString ();
}
}

string NativeStackTrace {
get {
var symbols = native_exc.CallStackSymbols;
var msg = string.Empty;
for (int i = 0; i < symbols.Length; i++) {
msg += "\t" + symbols [i] + "\n";
}
return msg;
void AppendNativeStackTrace (StringBuilder sb)
{
if (native_exc is not null) {
foreach (var symbol in native_exc.CallStackSymbols)
sb.Append ('\t').AppendLine (symbol);
}
}

public override string ToString ()
{
var msg = base.ToString ();
if (native_exc is null)
return msg;

if (native_exc != null)
msg += NativeStackTrace;

return msg;
var sb = new StringBuilder (msg);
AppendNativeStackTrace (sb);
return sb.ToString ();
}
}
}
Expand Down

5 comments on commit 9844af7

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ [CI Build] Tests failed on Build ❌

Tests failed on Build.

API diff

✅ API Diff from stable

View API diff

API & Generator diff

API Diff (from PR only) (no change)
Generator Diff (only version changes)

Packages generated

View packages

Test results

4 tests failed, 221 tests passed.

Failed tests

  • monotouch-test/Mac Catalyst/Debug [dotnet]: Failed (Tests run: 2638 Passed: 2490 Inconclusive: 35 Failed: 1 Ignored: 147)
  • dont link/Mac Catalyst/Debug: TimedOut (Execution timed out after 1200 seconds.
    No test log file was produced)
  • Documentation/All: Failed
  • DotNet tests: Failed (Execution failed with exit code 1)

Pipeline on Agent XAMBOT-1025.BigSur'
[foundation] Enable nullable for MonoTouchException (#12350)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Tests were not ran (VSTS: device tests iOS). ⚠️

Results were skipped for this run due to provisioning problems Azure Devops. Please contact the bot administrator.

Pipeline on Agent
[foundation] Enable nullable for MonoTouchException (#12350)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Tests were not ran (VSTS: device tests tvOS). ⚠️

Results were skipped for this run due to provisioning problems Azure Devops. Please contact the bot administrator.

Pipeline on Agent
[foundation] Enable nullable for MonoTouchException (#12350)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Tests failed on macOS M1 - Mac Big Sur (11.5) ❌

Tests failed on M1 - Mac Big Sur (11.5).

Failed tests are:

  • xammac_tests

Pipeline on Agent
[foundation] Enable nullable for MonoTouchException (#12350)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Tests passed on macOS Mac Mojave (10.14) ✅

Tests passed

All tests on macOS X Mac Mojave (10.14) passed.

Pipeline on Agent
[foundation] Enable nullable for MonoTouchException (#12350)

Please sign in to comment.