From e5f4b2aaf8abb5494c6ec142e0936f04926f3746 Mon Sep 17 00:00:00 2001
From: freshavocado7
Date: Wed, 25 Sep 2024 17:47:52 +0200
Subject: [PATCH] feat: More error handling
---
capella_model_explorer/backend/explorer.py | 9 ++++--
capella_model_explorer/backend/model_diff.py | 2 +-
frontend/src/components/CommitInformation.jsx | 4 +--
frontend/src/components/ModelDiff.jsx | 30 ++++++++++++-------
4 files changed, 28 insertions(+), 17 deletions(-)
diff --git a/capella_model_explorer/backend/explorer.py b/capella_model_explorer/backend/explorer.py
index ec9abfc..e324c22 100644
--- a/capella_model_explorer/backend/explorer.py
+++ b/capella_model_explorer/backend/explorer.py
@@ -288,7 +288,7 @@ async def post_compare(commit_range: CommitRange):
self.diff["lookup"] = create_diff_lookup(self.diff["objects"])
if self.diff["lookup"]:
return {"success": True}
- return {"success": False, "error": "No diff data available"}
+ return {"success": False, "error": "No model changes to show"}
except Exception as e:
LOGGER.exception("Failed to compare versions")
return {"success": False, "error": str(e)}
@@ -303,8 +303,11 @@ async def post_object_diff(object_id: ObjectDiffID):
@self.router.get("/api/commits")
async def get_commits():
- result = model_diff.populate_commits(self.model)
- return result
+ try:
+ result = model_diff.populate_commits(self.model)
+ return result
+ except Exception as e:
+ return {"error": str(e)}
@self.router.get("/api/diff")
async def get_diff():
diff --git a/capella_model_explorer/backend/model_diff.py b/capella_model_explorer/backend/model_diff.py
index c4f7cce..dad95c2 100644
--- a/capella_model_explorer/backend/model_diff.py
+++ b/capella_model_explorer/backend/model_diff.py
@@ -168,7 +168,7 @@ def _get_revision_info(
.strip()
.split("\x00")
)
- subject = description.splitlines()[0]
+ subject = description.splitlines()[0] if description.splitlines() else ""
try:
tag = subprocess.check_output(
["git", "tag", "--points-at", revision],
diff --git a/frontend/src/components/CommitInformation.jsx b/frontend/src/components/CommitInformation.jsx
index 7a8e40e..ebad513 100644
--- a/frontend/src/components/CommitInformation.jsx
+++ b/frontend/src/components/CommitInformation.jsx
@@ -1,4 +1,3 @@
-
// Copyright DB InfraGO AG and contributors
// SPDX-License-Identifier: Apache-2.0
@@ -31,7 +30,8 @@ const CommitInformation = ({
{commitDetails.description.includes('\n') && (
diff --git a/frontend/src/components/ModelDiff.jsx b/frontend/src/components/ModelDiff.jsx
index f709b0c..c62ee41 100644
--- a/frontend/src/components/ModelDiff.jsx
+++ b/frontend/src/components/ModelDiff.jsx
@@ -87,15 +87,18 @@ export const ModelDiff = ({ onRefetch, hasDiffed }) => {
const response = await fetch(`${API_BASE_URL}/commits`);
- if (!response.ok) throw new Error('Failed to fetch commits info.');
+ if (!response.ok) {
+ const err = await response.json();
+ throw new Error(err.error || 'Internal server error.');
+ }
const data = await response.json();
if (data === null) {
- alert('Not a git repo');
- return;
+ throw new Error('Not a git repo');
} else if (data.length < 2) {
- alert('Not enough commits to compare.');
- return;
+ throw new Error('Not enough commits to compare.');
+ } else if (data.error) {
+ throw new Error('Internal server error: ' + data.error);
}
setCommitDetails(data);
@@ -110,7 +113,7 @@ export const ModelDiff = ({ onRefetch, hasDiffed }) => {
setSelectionOptions(options);
setIsPopupVisible(true);
} catch (err) {
- setError(err.message);
+ alert(err.message || 'An error occurred while fetching commits.');
}
};
@@ -212,8 +215,7 @@ export const ModelDiff = ({ onRefetch, hasDiffed }) => {
- Success: Successfully compared
- versions ✓
+ Successfully compared versions ✓