From 18289006d1553e3f7edcaa2ebe68a5b0dacd1275 Mon Sep 17 00:00:00 2001 From: KoCoder Date: Wed, 18 Mar 2026 09:47:58 +0100 Subject: [PATCH] New: better Exception Handling --- .../Components/Layout/MainLayout.razor | 31 ++++++++- .../Components/Pages/ErrorTest.razor | 18 +++++ .../Components/Pages/Home.razor | 10 ++- .../Components/Pages/Rent.razor | 19 +++-- .../Components/Pages/Rollback.razor | 7 +- .../ServiceCollectionExtension.cs | 1 + .../Services/ErrorService.cs | 30 ++++++++ .../Services/RentService.cs | 69 +++++++------------ .../Services/RollbackService.cs | 23 +++---- 9 files changed, 140 insertions(+), 68 deletions(-) create mode 100644 Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/ErrorTest.razor create mode 100644 Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Services/ErrorService.cs diff --git a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Layout/MainLayout.razor b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Layout/MainLayout.razor index e188a38..d0f8e82 100644 --- a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Layout/MainLayout.razor +++ b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Layout/MainLayout.razor @@ -1,4 +1,9 @@ -@inherits LayoutComponentBase +@using Services +@inherits LayoutComponentBase +@implements IDisposable +@rendermode RenderMode.InteractiveServer +@inject IErrorService ErrorService +
+ +@code { + protected override void OnInitialized() + { + ErrorService.OnChange += StateHasChanged; + } + + private void DismissError() + { + ErrorService.ex = null; + } + + public void Dispose() + { + ErrorService.OnChange -= StateHasChanged; + } +} diff --git a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/ErrorTest.razor b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/ErrorTest.razor new file mode 100644 index 0000000..2c62d74 --- /dev/null +++ b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/ErrorTest.razor @@ -0,0 +1,18 @@ +@page "/ErrorTest" +@using Microsoft.Data.SqlClient +@using Services +@inject IErrorService ErrorService + +Error Test + +

Error Page

+ + + +@code { + + private void SetError() { + ErrorService.ex = new Exception("Hello World"); + } + +} diff --git a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/Home.razor b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/Home.razor index 64b7763..edade1f 100644 --- a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/Home.razor +++ b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/Home.razor @@ -1,7 +1,9 @@ @page "/" @using Models @using Services +@using System.Runtime.InteropServices @inject IRentService RentService +@inject IErrorService ErrorService Home @@ -45,9 +47,11 @@ protected override void OnInitialized() { - _vehicles = RentService.GetAvailableCars(); - Console.WriteLine("VC" + _vehicles.Count); - @* return Task.CompletedTask; *@ + try { + _vehicles = RentService.GetAvailableCars(); + } catch (Exception e) { + ErrorService.ex = e; + } } } diff --git a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/Rent.razor b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/Rent.razor index da1f69f..8100b7d 100644 --- a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/Rent.razor +++ b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/Rent.razor @@ -1,8 +1,10 @@ @page "/rent" @using Models @using Services +@using System.Runtime.InteropServices @inject IRentService RentService @inject NavigationManager NavigationManager +@inject IErrorService ErrorService Rent @@ -41,15 +43,22 @@ protected override void OnInitialized() { - _vehicles = RentService.GetAvailableCars(); - _customers = RentService.GetCustomers(); + try { + _vehicles = RentService.GetAvailableCars(); + _customers = RentService.GetCustomers(); + } catch (Exception e) { + ErrorService.ex = e; + } } private Task RentCar() { - RentService.RentCar(_customerId, _vehicleId, _from, _thru); - Console.WriteLine("Rented Car"); - NavigationManager.NavigateTo("/"); + try { + RentService.RentCar(_customerId, _vehicleId, _from, _thru); + NavigationManager.NavigateTo("/"); + } catch (Exception e) { + ErrorService.ex = e; + } return Task.CompletedTask; } } diff --git a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/Rollback.razor b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/Rollback.razor index 148716f..1b0e945 100644 --- a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/Rollback.razor +++ b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Components/Pages/Rollback.razor @@ -2,6 +2,7 @@ @using Models @using Services @inject IRollbackService RollbackService +@inject IErrorService ErrorService Rollback @@ -12,7 +13,11 @@ @code { private Task PerformRollback() { - RollbackService.ResetToDefaults(); + try { + RollbackService.ResetToDefaults(); + } catch (Exception e) { + ErrorService.ex = e; + } return Task.CompletedTask; } } diff --git a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Configuration/ServiceCollectionExtension.cs b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Configuration/ServiceCollectionExtension.cs index e7814ad..e021209 100644 --- a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Configuration/ServiceCollectionExtension.cs +++ b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Configuration/ServiceCollectionExtension.cs @@ -17,6 +17,7 @@ public static class ServiceCollectionExtension services.Services.AddScoped(); services.Services.AddScoped(); + services.Services.AddScoped(); return services; } } \ No newline at end of file diff --git a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Services/ErrorService.cs b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Services/ErrorService.cs new file mode 100644 index 0000000..323129a --- /dev/null +++ b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Services/ErrorService.cs @@ -0,0 +1,30 @@ +using System.Data; +using Configuration; +using Microsoft.Data.SqlClient; +using Models; + +namespace Services +{ + public interface IErrorService + { + Exception? ex { get; set; } + event Action? OnChange; + } + + public class ErrorService : IErrorService + { + private Exception? _ex; + public Exception? ex + { + get => _ex; + set + { + _ex = value; + NotifyStateChanged(); + } + } + + public event Action? OnChange; + private void NotifyStateChanged() => OnChange?.Invoke(); + } +} \ No newline at end of file diff --git a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Services/RentService.cs b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Services/RentService.cs index 18291ca..17521ba 100644 --- a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Services/RentService.cs +++ b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Services/RentService.cs @@ -17,22 +17,16 @@ namespace Services public List GetAvailableCars() { List vehicles = new List(); - try - { - using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection(); - string sql = "SELECT * FROM v_VerfuegbareFahrzeuge;"; - SqlCommand cmd = new SqlCommand(sql, conn); - using SqlDataReader reader = cmd.ExecuteReader(); + using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection(); + string sql = "SELECT * FROM v_VerfuegbareFahrzeuge;"; + SqlCommand cmd = new SqlCommand(sql, conn); - while (reader.Read()) - { - vehicles.Add(Vehicle.FromReader(reader)); - } - } - catch (Exception e) + using SqlDataReader reader = cmd.ExecuteReader(); + + while (reader.Read()) { - Console.WriteLine(e); + vehicles.Add(Vehicle.FromReader(reader)); } return vehicles; @@ -41,45 +35,34 @@ namespace Services public List GetCustomers() { List customers = new List(); - try + + using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection(); + string sql = "SELECT * FROM Customer;"; + SqlCommand cmd = new SqlCommand(sql, conn); + + using SqlDataReader reader = cmd.ExecuteReader(); + + while (reader.Read()) { - using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection(); - string sql = "SELECT * FROM Customer;"; - SqlCommand cmd = new SqlCommand(sql, conn); - - using SqlDataReader reader = cmd.ExecuteReader(); - - while (reader.Read()) - { - customers.Add(Customer.FromReader(reader)); - } + customers.Add(Customer.FromReader(reader)); } - catch (Exception e) - { - Console.WriteLine(e); - } - + return customers; } public void RentCar(long customerId, long vehicleId, DateTime start, DateTime end) { - try - { - using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection(); - using SqlCommand cmd = new SqlCommand("sp_MieteFahrzeug", conn); - cmd.CommandType = CommandType.StoredProcedure; + + using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection(); + using SqlCommand cmd = new SqlCommand("sp_MieteFahrzeug", conn); + cmd.CommandType = CommandType.StoredProcedure; - cmd.Parameters.AddWithValue("@KundeID", customerId); - cmd.Parameters.AddWithValue("@FahrzeugID", vehicleId); - cmd.Parameters.AddWithValue("@Start", start); - cmd.Parameters.AddWithValue("@Ende", end); + cmd.Parameters.AddWithValue("@KundeID", customerId); + cmd.Parameters.AddWithValue("@FahrzeugID", vehicleId); + cmd.Parameters.AddWithValue("@Start", start); + cmd.Parameters.AddWithValue("@Ende", end); - cmd.ExecuteNonQuery(); - }catch (Exception ex) - { - Console.WriteLine(ex); - } + cmd.ExecuteNonQuery(); } } } \ No newline at end of file diff --git a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Services/RollbackService.cs b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Services/RollbackService.cs index d160b13..c59f74b 100644 --- a/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Services/RollbackService.cs +++ b/Einheit_4/2025_5BHITN_Konstantin_Hintermayer_smarterrent/Services/RollbackService.cs @@ -14,24 +14,17 @@ namespace Services { public void ResetToDefaults() { - try - { - using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection(); + using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection(); - string sql = @" - USE [master]; - ALTER DATABASE [2025_5bhitn_konstantin_hintermayer_smarter_rentDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; - RESTORE DATABASE [2025_5bhitn_konstantin_hintermayer_smarter_rentDB] FROM DATABASE_SNAPSHOT = '2025_5bhitn_konstantin_hintermayer_smarter_rentDB_snapshot'; - ALTER DATABASE [2025_5bhitn_konstantin_hintermayer_smarter_rentDB] SET MULTI_USER;"; + string sql = @" +USE [master]; +ALTER DATABASE [2025_5bhitn_konstantin_hintermayer_smarter_rentDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; +RESTORE DATABASE [2025_5bhitn_konstantin_hintermayer_smarter_rentDB] FROM DATABASE_SNAPSHOT = '2025_5bhitn_konstantin_hintermayer_smarter_rentDB_snapshot'; +ALTER DATABASE [2025_5bhitn_konstantin_hintermayer_smarter_rentDB] SET MULTI_USER;"; - using SqlCommand cmd = new SqlCommand(sql, conn); + using SqlCommand cmd = new SqlCommand(sql, conn); - cmd.ExecuteNonQuery(); - } - catch (Exception ex) - { - Console.WriteLine(ex); - } + cmd.ExecuteNonQuery(); } } } \ No newline at end of file