From a7455b5629d07e0ef410dafc0b72721fb01c5268 Mon Sep 17 00:00:00 2001 From: Andrii Chebukin Date: Fri, 12 Jul 2024 06:48:52 +0400 Subject: [PATCH] feat: Implement `ViewMapNotFoundException`s --- .../ViewMapNotFoundException.cs | 141 ++++++++++++++++++ .../ViewRegistryExtensions.cs | 24 ++- 2 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 src/Uno.Extensions.Navigation/ViewMapNotFoundException.cs diff --git a/src/Uno.Extensions.Navigation/ViewMapNotFoundException.cs b/src/Uno.Extensions.Navigation/ViewMapNotFoundException.cs new file mode 100644 index 0000000000..39c2db1539 --- /dev/null +++ b/src/Uno.Extensions.Navigation/ViewMapNotFoundException.cs @@ -0,0 +1,141 @@ +namespace Uno.Extensions.Navigation; + +/// +/// Represents an exception that is thrown when a view map is not found in the view registry. +/// +public class ViewMapNotFoundException : Exception +{ + /// Initializes a new instance of the class. + private ViewMapNotFoundException() { } + + /// Initializes a new instance of the class with a specified error message. + /// The message that describes the error. + public ViewMapNotFoundException(string message) : base(message) { } + + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. + public ViewMapNotFoundException(string message, Exception innerException) : base(message, innerException) { } +} + +/// +/// Represents an exception that is thrown when a view map is not found in the view registry. +/// +public class ViewMapNotFoundByViewException : ViewMapNotFoundException +{ + /// Initializes a new instance of the class with a specified error message. + /// The view type that was not found in the view registry. + public ViewMapNotFoundByViewException(Type viewType) : base(CreateViewTypeNotFoundMessage(viewType)) { } + + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// The view type that was not found in the view registry. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. + public ViewMapNotFoundByViewException(Type viewType, Exception innerException) : base(CreateViewTypeNotFoundMessage(viewType), innerException) { } + + /// Initializes a new instance of the class with a specified error message. + /// The message that describes the error. + private ViewMapNotFoundByViewException(string message) : base(message) { } + + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. + private ViewMapNotFoundByViewException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Creates a message that describes the error when a view model type is not found in the view registry. + /// + /// + /// + public static string CreateViewTypeNotFoundMessage(Type viewType) => $"The view type {viewType} was not found in the view registry."; +} + +/// +/// Represents an exception that is thrown when a view map is not found in the view registry. +/// +public class ViewMapNotFoundByViewModelException : ViewMapNotFoundException +{ + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// The view model type that was not found in the view registry. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. + public ViewMapNotFoundByViewModelException(Type viewModelType, Exception innerException) : base(CreateViewModelTypeNotFoundMessage(viewModelType), innerException) { } + + /// Initializes a new instance of the class with a specified error message. + /// The view type that was not found in the view registry. + public ViewMapNotFoundByViewModelException(Type viewType) : base(CreateViewModelTypeNotFoundMessage(viewType)) { } + + /// Initializes a new instance of the class with a specified error message. + /// The message that describes the error. + private ViewMapNotFoundByViewModelException(string message) : base(message) { } + + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. + private ViewMapNotFoundByViewModelException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Creates a message that describes the error when a view model type is not found in the view registry. + /// + public static string CreateViewModelTypeNotFoundMessage(Type viewModelType) => $"The view model type {viewModelType} was not found in the view registry."; +} + +/// +/// Represents an exception that is thrown when a view map is not found in the view registry. +/// +public class ViewMapNotFoundByDataException : ViewMapNotFoundException +{ + /// Initializes a new instance of the class with a specified error message. + /// The data type that was not found in the view registry. + public ViewMapNotFoundByDataException(Type dataType) : base(CreateDataTypeNotFoundMessage(dataType)) { } + + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// The data type that was not found in the view registry. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. + public ViewMapNotFoundByDataException(Type dataType, Exception innerException) : base(CreateDataTypeNotFoundMessage(dataType), innerException) { } + + /// Initializes a new instance of the class with a specified error message. + /// The message that describes the error. + private ViewMapNotFoundByDataException(string message) : base(message) { } + + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. + private ViewMapNotFoundByDataException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Creates a message that describes the error when a data type is not found in the view registry. + /// + /// The data type that was not found in the view registry. + /// The error message. + public static string CreateDataTypeNotFoundMessage(Type dataType) => $"The data type {dataType} was not found in the view registry."; +} + +/// +/// Represents an exception that is thrown when a view map is not found in the view registry. +/// +public class ViewMapNotFoundByResultDataException : ViewMapNotFoundException +{ + /// Initializes a new instance of the class with a specified error message. + /// The result data type that was not found in the view registry. + public ViewMapNotFoundByResultDataException(Type resultDataType) : base(CreateResultDataTypeNotFoundMessage(resultDataType)) { } + + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// The result data type that was not found in the view registry. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. + public ViewMapNotFoundByResultDataException(Type resultDataType, Exception innerException) : base(CreateResultDataTypeNotFoundMessage(resultDataType), innerException) { } + + /// Initializes a new instance of the class with a specified error message. + /// The message that describes the error. + private ViewMapNotFoundByResultDataException(string message) : base(message) { } + + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. + private ViewMapNotFoundByResultDataException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Creates a message that describes the error when a result data type is not found in the view registry. + /// + /// The result data type that was not found in the view registry. + /// The error message. + public static string CreateResultDataTypeNotFoundMessage(Type resultDataType) => $"The result data type {resultDataType} was not found in the view registry."; +} diff --git a/src/Uno.Extensions.Navigation/ViewRegistryExtensions.cs b/src/Uno.Extensions.Navigation/ViewRegistryExtensions.cs index d70ee3d56a..4ff23efe3d 100644 --- a/src/Uno.Extensions.Navigation/ViewRegistryExtensions.cs +++ b/src/Uno.Extensions.Navigation/ViewRegistryExtensions.cs @@ -9,7 +9,11 @@ public static ViewMap FindByViewModel(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(this IViewRegistry registry) @@ -19,7 +23,11 @@ public static ViewMap FindByView(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(this IViewRegistry registry) @@ -28,7 +36,11 @@ public static ViewMap FindByData(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(this IViewRegistry registry) @@ -37,7 +49,11 @@ public static ViewMap FindByResultData(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; } }