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

feat: Implement ViewMapNotFoundExceptions #2425

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions src/Uno.Extensions.Navigation/ViewMapNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
namespace Uno.Extensions.Navigation;

/// <summary>
/// Represents an exception that is thrown when a view map is not found in the view registry.
/// </summary>
public class ViewMapNotFoundException : Exception
Copy link
Contributor

Choose a reason for hiding this comment

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

Hello @xperiandri, thanks for the PR! Maybe we could simplify a bit and have something like:

public enum ViewMapSearchType
    View,
    ViewModel,
    Data,
    ResultData

public class ViewMapNotFoundException : Exception

    public ViewMapNotFoundException(ViewMapSearchType searchType, Type? type = null)
        : base(CreateNotFoundMessage(searchType, type))

    public ViewMapNotFoundException(ViewMapSearchType searchType, Type? type, Exception innerException)
        :base(CreateNotFoundMessage(searchType, type), innerException)

    private static string CreateNotFoundMessage(ViewMapSearchType searchType, Type? type)

Or if it's preferable to have multiple exception types (so they can be caught independently), we could define a base exception class like ViewMapNotFoundExceptionBase. In this base class, we can define the CreateTypeNotFoundMessage method such as:

CreateTypeNotFoundMessage(string typeName, Type? type) 
    => $"The {typeName} type {type} was not found in the view registry.";

This method can then be used by the other exception classes that inherit from ViewMapNotFoundExceptionBase. Remember to use the enum ViewMapSearchType, please.

Copy link
Author

Choose a reason for hiding this comment

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

I defined a base class.
Type comes from different errors, so different exception types needed

{
/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundException"></see> class.</summary>
private ViewMapNotFoundException() { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundException"></see> class with a specified error message.</summary>
/// <param name="message">The message that describes the error.</param>
public ViewMapNotFoundException(string message) : base(message) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundException"></see> class with a specified error message and a reference to the inner exception that is the cause of this exception.</summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
public ViewMapNotFoundException(string message, Exception innerException) : base(message, innerException) { }
}

/// <summary>
/// Represents an exception that is thrown when a view map is not found in the view registry.
/// </summary>
public class ViewMapNotFoundByViewException : ViewMapNotFoundException
{
/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundException"></see> class with a specified error message.</summary>
/// <param name="viewType">The view type that was not found in the view registry.</param>
public ViewMapNotFoundByViewException(Type viewType) : base(CreateViewTypeNotFoundMessage(viewType)) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundException"></see> class with a specified error message and a reference to the inner exception that is the cause of this exception.</summary>
/// <param name="viewType">The view type that was not found in the view registry.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
public ViewMapNotFoundByViewException(Type viewType, Exception innerException) : base(CreateViewTypeNotFoundMessage(viewType), innerException) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundByViewException"></see> class with a specified error message.</summary>
/// <param name="message">The message that describes the error.</param>
private ViewMapNotFoundByViewException(string message) : base(message) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundByViewException"></see> class with a specified error message and a reference to the inner exception that is the cause of this exception.</summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
private ViewMapNotFoundByViewException(string message, Exception innerException) : base(message, innerException) { }

/// <summary>
/// Creates a message that describes the error when a view model type is not found in the view registry.
/// </summary>
/// <param name="viewType"></param>
/// <returns></returns>
public static string CreateViewTypeNotFoundMessage(Type viewType) => $"The view type {viewType} was not found in the view registry.";
}

/// <summary>
/// Represents an exception that is thrown when a view map is not found in the view registry.
/// </summary>
public class ViewMapNotFoundByViewModelException : ViewMapNotFoundException
{
/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundException"></see> class with a specified error message and a reference to the inner exception that is the cause of this exception.</summary>
/// <param name="viewModelType">The view model type that was not found in the view registry.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
public ViewMapNotFoundByViewModelException(Type viewModelType, Exception innerException) : base(CreateViewModelTypeNotFoundMessage(viewModelType), innerException) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundException"></see> class with a specified error message.</summary>
/// <param name="viewType">The view type that was not found in the view registry.</param>
public ViewMapNotFoundByViewModelException(Type viewType) : base(CreateViewModelTypeNotFoundMessage(viewType)) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundByViewModelException"></see> class with a specified error message.</summary>
/// <param name="message">The message that describes the error.</param>
private ViewMapNotFoundByViewModelException(string message) : base(message) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundByViewModelException"></see> class with a specified error message and a reference to the inner exception that is the cause of this exception.</summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
private ViewMapNotFoundByViewModelException(string message, Exception innerException) : base(message, innerException) { }

/// <summary>
/// Creates a message that describes the error when a view model type is not found in the view registry.
/// </summary>
public static string CreateViewModelTypeNotFoundMessage(Type viewModelType) => $"The view model type {viewModelType} was not found in the view registry.";
}

/// <summary>
/// Represents an exception that is thrown when a view map is not found in the view registry.
/// </summary>
public class ViewMapNotFoundByDataException : ViewMapNotFoundException
{
/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundByDataException"/> class with a specified error message.</summary>
/// <param name="dataType">The data type that was not found in the view registry.</param>
public ViewMapNotFoundByDataException(Type dataType) : base(CreateDataTypeNotFoundMessage(dataType)) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundByDataException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.</summary>
/// <param name="dataType">The data type that was not found in the view registry.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
public ViewMapNotFoundByDataException(Type dataType, Exception innerException) : base(CreateDataTypeNotFoundMessage(dataType), innerException) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundByDataException"/> class with a specified error message.</summary>
/// <param name="message">The message that describes the error.</param>
private ViewMapNotFoundByDataException(string message) : base(message) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundByDataException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.</summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
private ViewMapNotFoundByDataException(string message, Exception innerException) : base(message, innerException) { }

/// <summary>
/// Creates a message that describes the error when a data type is not found in the view registry.
/// </summary>
/// <param name="dataType">The data type that was not found in the view registry.</param>
/// <returns>The error message.</returns>
public static string CreateDataTypeNotFoundMessage(Type dataType) => $"The data type {dataType} was not found in the view registry.";
}

/// <summary>
/// Represents an exception that is thrown when a view map is not found in the view registry.
/// </summary>
public class ViewMapNotFoundByResultDataException : ViewMapNotFoundException
{
/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundByResultDataException"/> class with a specified error message.</summary>
/// <param name="resultDataType">The result data type that was not found in the view registry.</param>
public ViewMapNotFoundByResultDataException(Type resultDataType) : base(CreateResultDataTypeNotFoundMessage(resultDataType)) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundByResultDataException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.</summary>
/// <param name="resultDataType">The result data type that was not found in the view registry.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
public ViewMapNotFoundByResultDataException(Type resultDataType, Exception innerException) : base(CreateResultDataTypeNotFoundMessage(resultDataType), innerException) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundByResultDataException"/> class with a specified error message.</summary>
/// <param name="message">The message that describes the error.</param>
private ViewMapNotFoundByResultDataException(string message) : base(message) { }

/// <summary>Initializes a new instance of the <see cref="ViewMapNotFoundByResultDataException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.</summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
private ViewMapNotFoundByResultDataException(string message, Exception innerException) : base(message, innerException) { }

/// <summary>
/// Creates a message that describes the error when a result data type is not found in the view registry.
/// </summary>
/// <param name="resultDataType">The result data type that was not found in the view registry.</param>
/// <returns>The error message.</returns>
public static string CreateResultDataTypeNotFoundMessage(Type resultDataType) => $"The result data type {resultDataType} was not found in the view registry.";
}
24 changes: 20 additions & 4 deletions src/Uno.Extensions.Navigation/ViewRegistryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ public static ViewMap FindByViewModel<TViewModel>(this IViewRegistry registry)

public static ViewMap FindByViewModel(this IViewRegistry registry, Type? viewModelType)
{
return registry.Items.FindByInheritedTypes(viewModelType, map => map.ViewModel).First();
viewModelType = viewModelType ?? throw new ArgumentNullException(nameof(viewModelType));
var viewMap = registry.Items.FindByInheritedTypes(viewModelType, map => map.ViewModel).FirstOrDefault();
if (viewMap is null)
throw new ViewMapNotFoundByViewModelException(viewModelType);
return viewMap;
}

public static ViewMap FindByView<TView>(this IViewRegistry registry)
Expand All @@ -19,7 +23,11 @@ public static ViewMap FindByView<TView>(this IViewRegistry registry)

public static ViewMap FindByView(this IViewRegistry registry, Type? viewType)
{
return registry.Items.FindByInheritedTypes(viewType, map => map.View).First();
viewType = viewType ?? throw new ArgumentNullException(nameof(viewType));
var viewMap = registry.Items.FindByInheritedTypes(viewType, map => map.View).FirstOrDefault();
if (viewMap is null)
throw new ViewMapNotFoundByViewException(viewType);
return viewMap;
}

public static ViewMap FindByData<TData>(this IViewRegistry registry)
Expand All @@ -28,7 +36,11 @@ public static ViewMap FindByData<TData>(this IViewRegistry registry)
}
public static ViewMap FindByData(this IViewRegistry registry, Type? dataType)
{
return registry.Items.FindByInheritedTypes(dataType, map => map.Data?.Data).First();
dataType = dataType ?? throw new ArgumentNullException(nameof(dataType));
var viewMap = registry.Items.FindByInheritedTypes(dataType, map => map.Data?.Data).FirstOrDefault();
if (viewMap is null)
throw new ViewMapNotFoundByDataException(dataType);
return viewMap;
}

public static ViewMap FindByResultData<TResultData>(this IViewRegistry registry)
Expand All @@ -37,7 +49,11 @@ public static ViewMap FindByResultData<TResultData>(this IViewRegistry registry)
}
public static ViewMap FindByResultData(this IViewRegistry registry, Type? dataType)
{
return registry.Items.FindByInheritedTypes(dataType, map => map.ResultData).First();
dataType = dataType ?? throw new ArgumentNullException(nameof(dataType));
var viewMap = registry.Items.FindByInheritedTypes(dataType, map => map.ResultData).FirstOrDefault();
if (viewMap is null)
throw new ViewMapNotFoundByResultDataException(dataType);
return viewMap;
}
}

Loading