fix: use ResourceNotFoundException for not-found cases in updateExpense, IllegalArgumentException for ownership mismatch in deleteCustomCategory

This commit is contained in:
2026-05-05 16:55:01 +02:00
parent 906b60d264
commit 797d482ebf
@@ -117,7 +117,7 @@ public class ExpenseListService {
public Expense updateExpense(Long expenseListId, Expense updatedExpense) { public Expense updateExpense(Long expenseListId, Expense updatedExpense) {
ExpenseList expenseList = repository.findById(expenseListId) ExpenseList expenseList = repository.findById(expenseListId)
.orElseThrow(() -> new IllegalArgumentException("ExpenseList not found")); .orElseThrow(() -> new ResourceNotFoundException("ExpenseList not found with id: " + expenseListId));
if (!expenseList.getExpenses().stream() if (!expenseList.getExpenses().stream()
.anyMatch(expense -> expense.getId().equals(updatedExpense.getId()))) { .anyMatch(expense -> expense.getId().equals(updatedExpense.getId()))) {
@@ -125,7 +125,7 @@ public class ExpenseListService {
} }
Expense existingExpense = expenseRepository.findById(updatedExpense.getId()) Expense existingExpense = expenseRepository.findById(updatedExpense.getId())
.orElseThrow(() -> new IllegalArgumentException("Expense not found")); .orElseThrow(() -> new ResourceNotFoundException("Expense not found with id: " + updatedExpense.getId()));
existingExpense.setTitle(updatedExpense.getTitle()); existingExpense.setTitle(updatedExpense.getTitle());
existingExpense.setAmount(updatedExpense.getAmount()); existingExpense.setAmount(updatedExpense.getAmount());
existingExpense.setPersonalUseAmount(updatedExpense.getPersonalUseAmount()); existingExpense.setPersonalUseAmount(updatedExpense.getPersonalUseAmount());
@@ -151,7 +151,7 @@ public class ExpenseListService {
XpenselyCustomCategory category = customCategoryRepository.findById(categoryId) XpenselyCustomCategory category = customCategoryRepository.findById(categoryId)
.orElseThrow(() -> new ResourceNotFoundException("Custom Category not found")); .orElseThrow(() -> new ResourceNotFoundException("Custom Category not found"));
if (!category.getExpenseList().getId().equals(expenseListId)) { if (!category.getExpenseList().getId().equals(expenseListId)) {
throw new RuntimeException("Category does not belong to the specified Expense List"); throw new IllegalArgumentException("Category does not belong to the specified Expense List");
} }
customCategoryRepository.delete(category); customCategoryRepository.delete(category);
} }