Skip to content

Commit

Permalink
feat: More error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
freshavocado7 committed Oct 1, 2024
1 parent 49dcc3b commit e5f4b2a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
9 changes: 6 additions & 3 deletions capella_model_explorer/backend/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand All @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion capella_model_explorer/backend/model_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/CommitInformation.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// Copyright DB InfraGO AG and contributors
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -31,7 +30,8 @@ const CommitInformation = ({
</p>
{commitDetails.description.includes('\n') && (
<button
className="mb-4 mt-2 font-normal text-custom-blue hover:text-custom-blue-hover"
className="mb-4 mt-2 font-normal text-custom-blue
hover:text-custom-blue-hover"
onClick={() => toggleExpand(section)}>
{isExpanded ? 'Show Less' : 'Show Full Description'}
</button>
Expand Down
30 changes: 19 additions & 11 deletions frontend/src/components/ModelDiff.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.');
}
};

Expand Down Expand Up @@ -212,8 +215,7 @@ export const ModelDiff = ({ onRefetch, hasDiffed }) => {
<div
className="mt-4 rounded border border-green-400
bg-green-100 p-4 text-green-700">
<strong>Success:</strong> Successfully compared
versions ✓
Successfully compared versions ✓
</div>
<button
className="mt-4 rounded bg-green-500 px-4 py-2
Expand All @@ -237,9 +239,15 @@ export const ModelDiff = ({ onRefetch, hasDiffed }) => {
{loadingState === 'error' && (
<>
<div
className="mt-4 rounded border border-red-400
bg-red-100 p-4 text-red-700">
<strong>Error:</strong> {error}
className={`mt-4 rounded border p-4 ${
error === 'No model changes to show'
? 'border-yellow-400 bg-yellow-100 text-yellow-700'
: 'border-red-400 bg-red-100 text-red-700'
}`}>
{error !== 'No model changes to show' && (
<strong>Error:</strong>
)}{' '}
{error}
</div>
<button
className="mt-4 rounded bg-custom-blue px-4 py-2
Expand Down

0 comments on commit e5f4b2a

Please sign in to comment.