Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,40 @@ public async static Task<XmlFavoritesRepository> OpenIsolatedStorage(string isol
};
}
}
catch (Exception)
catch (IOException ex)
{
// Log IO errors (file access issues)
System.Diagnostics.Debug.WriteLine($"IO Error loading favorites: {ex.Message}");
return new XmlFavoritesRepository
{
IsolatedStorageName = isolatedStorageName,
People = new List<Person>()
};
}
catch (XmlException ex)
{
// Log XML parsing errors (corrupted file)
System.Diagnostics.Debug.WriteLine($"XML Error loading favorites: {ex.Message}");
return new XmlFavoritesRepository
{
IsolatedStorageName = isolatedStorageName,
People = new List<Person>()
};
}
catch (InvalidOperationException ex)
{
// Log deserialization errors (schema mismatch)
System.Diagnostics.Debug.WriteLine($"Deserialization Error loading favorites: {ex.Message}");
return new XmlFavoritesRepository
{
IsolatedStorageName = isolatedStorageName,
People = new List<Person>()
};
}
catch (Exception ex)
{
// Log unexpected errors
System.Diagnostics.Debug.WriteLine($"Unexpected error loading favorites: {ex.Message}");
return new XmlFavoritesRepository
{
IsolatedStorageName = isolatedStorageName,
Expand All @@ -84,8 +116,40 @@ public async static Task<XmlFavoritesRepository> OpenFile(string path)
repo.IsolatedStorageName = Path.GetFileName(path);
return repo;
}
catch (Exception)
catch (IOException ex)
{
// Log IO errors (file access issues)
System.Diagnostics.Debug.WriteLine($"IO Error opening file: {ex.Message}");
return new XmlFavoritesRepository
{
IsolatedStorageName = Path.GetFileName(path),
People = new List<Person>()
};
}
catch (XmlException ex)
{
// Log XML parsing errors (corrupted file)
System.Diagnostics.Debug.WriteLine($"XML Error opening file: {ex.Message}");
return new XmlFavoritesRepository
{
IsolatedStorageName = Path.GetFileName(path),
People = new List<Person>()
};
}
catch (InvalidOperationException ex)
{
// Log deserialization errors (schema mismatch)
System.Diagnostics.Debug.WriteLine($"Deserialization Error opening file: {ex.Message}");
return new XmlFavoritesRepository
{
IsolatedStorageName = Path.GetFileName(path),
People = new List<Person>()
};
}
catch (Exception ex)
{
// Log unexpected errors
System.Diagnostics.Debug.WriteLine($"Unexpected error opening file: {ex.Message}");
return new XmlFavoritesRepository
{
IsolatedStorageName = Path.GetFileName(path),
Expand All @@ -106,9 +170,25 @@ private async Task Commit()
using var f = new FileStream(filePath, FileMode.Create, FileAccess.Write);
serializer.Serialize(f, this);
}
catch (Exception)
catch (IOException ex)
{
// Log IO errors during commit
System.Diagnostics.Debug.WriteLine($"IO Error committing favorites: {ex.Message}");
}
catch (XmlException ex)
{
// Log XML serialization errors
System.Diagnostics.Debug.WriteLine($"XML Error committing favorites: {ex.Message}");
}
catch (InvalidOperationException ex)
{
// Log serialization errors
System.Diagnostics.Debug.WriteLine($"Serialization Error committing favorites: {ex.Message}");
}
catch (Exception ex)
{
// Ignore serialization errors
// Log unexpected errors during commit
System.Diagnostics.Debug.WriteLine($"Unexpected error committing favorites: {ex.Message}");
}

var ev = Changed;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System.Diagnostics;
using Recipes.Models;

namespace Recipes.ViewModels
Expand Down Expand Up @@ -104,9 +104,17 @@ public async void LoadItemId(string itemId)
RecipeRating = item.RecipeRating;
RecipeReview = item.RecipeReview;
}
catch (Exception)
catch (ArgumentNullException ex)
{
Debug.WriteLine("Failed to Load Item");
Debug.WriteLine($"Argument null error loading recipe for edit: {ex.Message}");
}
catch (InvalidOperationException ex)
{
Debug.WriteLine($"Invalid operation loading recipe for edit: {ex.Message}");
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to load recipe item for edit: {ex.Message}");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System.Diagnostics;
using Recipes.Views;
using System.Collections.ObjectModel;
using Recipes.Models;
Expand Down Expand Up @@ -186,9 +186,17 @@ public async void LoadItemId(string itemId)
RecipeReviewVisible = !string.IsNullOrEmpty(RecipeReview);

}
catch (Exception)
catch (ArgumentNullException ex)
{
Debug.WriteLine("Failed to Load Item");
Debug.WriteLine($"Argument null error loading recipe: {ex.Message}");
}
catch (InvalidOperationException ex)
{
Debug.WriteLine($"Invalid operation loading recipe: {ex.Message}");
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to load recipe item: {ex.Message}");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Windows.Input;
using System.Windows.Input;
using System.Diagnostics;
using Recipes.Models;
using System.Collections.ObjectModel;
Expand Down Expand Up @@ -147,9 +147,21 @@ public void LoadHitId(string hitId)
OnPropertyChanged(nameof(Hit));
LoadSearchResultDetails();
}
catch (Exception)
catch (ArgumentNullException ex)
{
Debug.WriteLine("Failed to Load Hit");
Debug.WriteLine($"Argument null error loading search result: {ex.Message}");
}
catch (FormatException ex)
{
Debug.WriteLine($"Format error parsing hit ID: {ex.Message}");
}
catch (InvalidOperationException ex)
{
Debug.WriteLine($"Invalid operation loading search result: {ex.Message}");
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to load search result hit: {ex.Message}");
}
}

Expand Down
14 changes: 11 additions & 3 deletions 10.0/Fundamentals/Shell/Xaminals/Views/CatDetailPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Xaminals.Data;
using Xaminals.Data;
using Xaminals.Models;

namespace Xaminals.Views
Expand Down Expand Up @@ -26,9 +26,17 @@ void LoadAnimal(string name)
Animal animal = CatData.Cats.FirstOrDefault(a => a.Name == name);
BindingContext = animal;
}
catch (Exception)
catch (ArgumentNullException ex)
{
Console.WriteLine("Failed to load animal.");
System.Diagnostics.Debug.WriteLine($"Argument null error loading cat: {ex.Message}");
}
catch (InvalidOperationException ex)
{
System.Diagnostics.Debug.WriteLine($"Invalid operation loading cat: {ex.Message}");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to load cat: {ex.Message}");
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions 10.0/Fundamentals/Shell/Xaminals/Views/DogDetailPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Xaminals.Data;
using Xaminals.Data;
using Xaminals.Models;

namespace Xaminals.Views
Expand Down Expand Up @@ -26,9 +26,17 @@ void LoadAnimal(string name)
Animal animal = DogData.Dogs.FirstOrDefault(a => a.Name == name);
BindingContext = animal;
}
catch (Exception)
catch (ArgumentNullException ex)
{
Console.WriteLine("Failed to load animal.");
System.Diagnostics.Debug.WriteLine($"Argument null error loading dog: {ex.Message}");
}
catch (InvalidOperationException ex)
{
System.Diagnostics.Debug.WriteLine($"Invalid operation loading dog: {ex.Message}");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to load dog: {ex.Message}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using TodoAPI.Interfaces;
using TodoAPI.Models;

Expand Down Expand Up @@ -54,9 +54,19 @@ public IActionResult Create([FromBody]TodoItem item)
}
_todoRepository.Insert(item);
}
catch (Exception)
catch (ArgumentNullException ex)
{
return BadRequest(ErrorCode.CouldNotCreateItem.ToString());
return BadRequest($"{ErrorCode.CouldNotCreateItem}: {ex.Message}");
}
catch (ArgumentException ex)
{
return BadRequest($"{ErrorCode.CouldNotCreateItem}: {ex.Message}");
}
catch (Exception ex)
{
// Log the exception here in production
return StatusCode(StatusCodes.Status500InternalServerError,
$"{ErrorCode.CouldNotCreateItem}: An unexpected error occurred");
}
return Ok(item);
}
Expand All @@ -79,9 +89,23 @@ public IActionResult Edit([FromBody] TodoItem item)
}
_todoRepository.Update(item);
}
catch (Exception)
catch (ArgumentNullException ex)
{
return BadRequest($"{ErrorCode.CouldNotUpdateItem}: {ex.Message}");
}
catch (ArgumentException ex)
{
return BadRequest($"{ErrorCode.CouldNotUpdateItem}: {ex.Message}");
}
catch (InvalidOperationException ex)
{
return NotFound($"{ErrorCode.RecordNotFound}: {ex.Message}");
}
catch (Exception ex)
{
return BadRequest(ErrorCode.CouldNotUpdateItem.ToString());
// Log the exception here in production
return StatusCode(StatusCodes.Status500InternalServerError,
$"{ErrorCode.CouldNotUpdateItem}: An unexpected error occurred");
}
return NoContent();
}
Expand All @@ -100,9 +124,19 @@ public IActionResult Delete(string id)
}
_todoRepository.Delete(id);
}
catch (Exception)
catch (ArgumentException ex)
{
return BadRequest($"{ErrorCode.CouldNotDeleteItem}: {ex.Message}");
}
catch (InvalidOperationException ex)
{
return NotFound($"{ErrorCode.RecordNotFound}: {ex.Message}");
}
catch (Exception ex)
{
return BadRequest(ErrorCode.CouldNotDeleteItem.ToString());
// Log the exception here in production
return StatusCode(StatusCodes.Status500InternalServerError,
$"{ErrorCode.CouldNotDeleteItem}: An unexpected error occurred");
}
return NoContent();
}
Expand Down
47 changes: 43 additions & 4 deletions 10.0/WebServices/TodoREST/TodoAPI/Services/TodoRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using TodoAPI.Interfaces;
using TodoAPI.Interfaces;
using TodoAPI.Models;

namespace TodoAPI.Services
Expand Down Expand Up @@ -29,20 +29,59 @@ public TodoItem Find(string id)

public void Insert(TodoItem item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item), "TodoItem cannot be null");
}

if (string.IsNullOrWhiteSpace(item.ID))
{
throw new ArgumentException("TodoItem ID cannot be null or empty", nameof(item));
}

_todoList.Add(item);
}

public void Update(TodoItem item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item), "TodoItem cannot be null");
}

if (string.IsNullOrWhiteSpace(item.ID))
{
throw new ArgumentException("TodoItem ID cannot be null or empty", nameof(item));
}

var todoItem = this.Find(item.ID);
if (todoItem == null)
{
throw new InvalidOperationException($"TodoItem with ID '{item.ID}' not found");
}

var index = _todoList.IndexOf(todoItem);
_todoList.RemoveAt(index);
_todoList.Insert(index, item);
if (index >= 0)
{
_todoList.RemoveAt(index);
_todoList.Insert(index, item);
}
}

public void Delete(string id)
{
_todoList.Remove(this.Find(id));
if (string.IsNullOrWhiteSpace(id))
{
throw new ArgumentException("ID cannot be null or empty", nameof(id));
}

var todoItem = this.Find(id);
if (todoItem == null)
{
throw new InvalidOperationException($"TodoItem with ID '{id}' not found");
}

_todoList.Remove(todoItem);
}

private void InitializeData()
Expand Down
Loading