Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSErrorException's Message property should contain more useful information #4133

Closed
johannes-schmitt opened this issue May 28, 2018 · 4 comments
Assignees
Labels
enhancement The issue or pull request is an enhancement iOS Issues affecting Xamarin.iOS macOS Issues affecting Xamarin.Mac
Milestone

Comments

@johannes-schmitt
Copy link

Enhancement: From my point of view, NSErrorException's Message property should contain some information about the underlying NSError. Message is currently "Exception of type 'Foundation.NSErrorException' was thrown."

Consider a project that logs all unhandled exceptions (via AppDomain.CurrentDomain.UnhandledException) to a remote error tracking system (like Sentry) or to a log file. In almost all of the cases, these unhandled exceptions are bugs. The existing Message property does not help to understand the root cause of the bug and makes it hard to properly fix it.

Steps to Reproduce

  1. in iOS (or macOS) project: throw new NSErrorException(/* ... */)
  2. in platform independent project (e.g. Xamarin Forms), referencing the project from step 1: AppDomain.CurrentDomain.UnhandledException += (_, e) => { Console.WriteLine(((Exception)e.ExceptionObject).Message); } ;

Expected Behavior

Message should at least contain NSError's Code to be able to understand what the exception was about.

Actual Behavior

Message is "Exception of type 'Foundation.NSErrorException' was thrown."

@therealjohn therealjohn added the enhancement The issue or pull request is an enhancement label May 29, 2018
@therealjohn therealjohn added this to the Future milestone May 29, 2018
@spouliot spouliot self-assigned this Jun 3, 2018
@spouliot spouliot added macOS Issues affecting Xamarin.Mac iOS Issues affecting Xamarin.iOS labels Jun 3, 2018
@spouliot spouliot modified the milestones: Future, d15-9 Jun 3, 2018
spouliot added a commit to spouliot/xamarin-macios that referenced this issue Jun 3, 2018
…arin#4133

The default `Message` property is not every helpful. Better information
is available inside the `Error` property but it's not general (nor cross
platform) when dealing with exception.

Include unit tests (on an existing test checking NSError values)

xamarin#4133
@spouliot
Copy link
Contributor

spouliot commented Jun 3, 2018

Make sense :)

The most useful information, coming from the native code, is the Description property, which we default for ToString () on NSObject subclasses, however NSErrorException is a subclass of Exception.

You can probably workaround this with something like
var msg = (e as NSErrorException)?.Error.Description ?? e.Message;
where e is an Exception subclass, until a release is out with the fix.

@johannes-schmitt
Copy link
Author

johannes-schmitt commented Jun 4, 2018

I'm not sure, but I think the Description property maps to NSError's LocalizedDescription. If my assumption holds true, it'll be tough to understand what an error means for a language I don't speak. Especially, in a error tracking system it would be helpful to have some non-localized exception information to be able to understand common error across language borders.

I would suggest to override Message so that Code (which is language-neutral) is part of it. Something simple like this:
public override string Message { get { return $"{error.Description} ({error.Code})"; } }
Maybe also Domain should be included, because Code is only unique within a domain.
What do you think?

Regarding your suggested workaround: I implemented a similar approch that uses reflection. Why reflection? The type NSErrorException is not available in the platform-independent project.

@spouliot
Copy link
Contributor

spouliot commented Jun 4, 2018

I'll check if it's localized after WWDC (or in between sessions)
but the output is already like Domain + Error + Description + Extra (user info)
so I don't think it can be very confusing to get the same data

@johannes-schmitt
Copy link
Author

I've been some days away from that topic. Coming back now, I agree, NSError.ToString() is sufficient. Also other exception messages in the .NET world are actually localized.

@xamarin xamarin locked as resolved and limited conversation to collaborators May 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement The issue or pull request is an enhancement iOS Issues affecting Xamarin.iOS macOS Issues affecting Xamarin.Mac
Projects
None yet
Development

No branches or pull requests

3 participants