Skip to content

Commit d29b848

Browse files
committed
Added checking of parameters to avoid NPEs
1 parent d7be82a commit d29b848

8 files changed

+109
-4
lines changed

WebFrontend/App.razor

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,22 @@
77
</MatThemeProvider>
88
</Found>
99
<NotFound>
10-
<LayoutView Layout="@typeof(MainLayout)">
11-
<p>Sorry, there's nothing at this address.</p>
12-
</LayoutView>
10+
<MatThemeProvider Theme="appTheme">
11+
<LayoutView Layout="@typeof(MainLayout)">
12+
<div class="mat-layout-grid mat-layout-grid-align-center">
13+
<div class="mat-layout-grid-inner center">
14+
15+
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-2"></div>
16+
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-8">
17+
<MatPaper Elevation="2" style="padding:1em">
18+
<span>There is nothing at this address.</span>
19+
</MatPaper>
20+
</div>
21+
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-2"></div>
22+
</div>
23+
</div>
24+
</LayoutView>
25+
</MatThemeProvider>
1326
</NotFound>
1427
</Router>
1528

@@ -21,4 +34,4 @@
2134
{
2235
Primary = MatThemeColors.BlueGrey._500.Value
2336
};
24-
}
37+
}

WebFrontend/Pages/EditMarketOrder.razor

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@
5454
{
5555
// fetch the order to be edited
5656
_activeMarketOrder = MarketOrderService.GetMarketOrder(OrderId);
57+
58+
if (_activeEntry == null)
59+
{
60+
NavigationManager.NavigateTo("/notfound");
61+
return;
62+
}
63+
5764
_activeEntry = PortfolioEntrySerivce.GetPortfolioEntry(_activeMarketOrder.PortfolioEntryId);
5865
_activePortfolio = PortfolioService.GetPortfolio(_activeEntry.PortfolioId);
5966

WebFrontend/Pages/EditPortfolio.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
// find the portfolio
6666
_activePortfolio = PortfolioService.GetPortfolio(PortfolioId);
6767

68+
if (_activePortfolio == null)
69+
{
70+
NavigationManager.NavigateTo("/notfound");
71+
return;
72+
}
73+
6874
// update the form model
6975
_formFormModel.Name = _activePortfolio.Name;
7076
_formFormModel.Description = _activePortfolio.Description;

WebFrontend/Pages/NewMarketOrder.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
// load the portfolio entry
4949
_activeEntry = PortfolioEntrySerivce.GetPortfolioEntry(EntryId);
5050

51+
if (_activeEntry == null)
52+
{
53+
NavigationManager.NavigateTo("/notfound");
54+
return;
55+
}
56+
5157
// load the portfolio the entry belongs to
5258
_activePortfolio = PortfolioService.GetPortfolio(_activeEntry.PortfolioId);
5359
}

WebFrontend/Pages/PortfolioDetail.razor

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,33 @@
126126

127127
@code
128128
{
129+
// id of the portfolio whose detail should be shown
129130
[Parameter]
130131
public int PortfolioId { get; set; }
131132

133+
// portfolio whose detail should be shown
132134
private Portfolio _activePortfolio;
133135

136+
// summary of the portfolio
134137
private ISummaryService.Summary _portfolioSummary;
135138

139+
// entries of the portfolio
136140
private List<PortfolioEntry> _activePortfolioEntries;
137141

142+
// rows of the portfolio entry table
138143
private List<PortfolioEntryRow> _portfolioEntryRows;
139144

140145
protected record PortfolioEntryRow(string Symbol, decimal CurrentPrice, decimal RelativeChange, decimal Percentage, decimal AbsoluteChange, decimal MarketValue, int EntryId);
141146

142147
protected override void OnInitialized()
143148
{
144149
_activePortfolio = PortfolioService.GetPortfolio(PortfolioId);
150+
if (_activePortfolio == null)
151+
{
152+
NavigationManager.NavigateTo("/notfound");
153+
return;
154+
}
155+
145156
_activePortfolioEntries = PortfolioEntryService.GetPortfolioEntries(PortfolioId);
146157
_loadEntryInfo();
147158
}

WebFrontend/Pages/PortfolioEntryDetail.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@
254254
// get the portfolio entry
255255
_activePortfolioEntry = PortfolioEntryService.GetPortfolioEntry(EntryId);
256256

257+
if (_activePortfolioEntry == null)
258+
{
259+
NavigationManager.NavigateTo("/notfound");
260+
return;
261+
}
262+
257263
// get the entry's portfolio
258264
_activePortfolio = PortfolioService.GetPortfolio(_activePortfolioEntry.PortfolioId);
259265
}

WebFrontend/Pages/PortfolioEntryManagement.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@
112112
// load the portfolio using the specified ID
113113
_portfolio = PortfolioService.GetPortfolio(PortfolioId);
114114

115+
if (_portfolio == null)
116+
{
117+
NavigationManager.NavigateTo("/notfound");
118+
return;
119+
}
120+
115121
// load all entries of the portfolio
116122
_portfolioEntries = PortfolioEntryService.GetPortfolioEntries(PortfolioId);
117123
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
@page "/notfound"
2+
@using Services
3+
@inject NavigationManager NavigationManager
4+
@inject IPortfolioService PortfolioService
5+
@inject IPortfolioEntryService PortfolioEntryService
6+
@inject IMatDialogService MatDialogService
7+
@inject IMatToaster Toaster
8+
9+
<style>
10+
.demo-mat-card {
11+
margin-bottom: 2em;
12+
}
13+
14+
.demo-mat-card-content {
15+
padding: 1rem;
16+
}
17+
18+
.clear-margin {
19+
margin: 0px;
20+
}
21+
22+
.app-fab--absolute {
23+
position: fixed;
24+
bottom: 1rem;
25+
right: 1rem;
26+
}
27+
28+
.mat-paper {
29+
display: flex;
30+
flex-direction: column;
31+
justify-content: center;
32+
align-items: center;
33+
padding: 1em;
34+
}
35+
</style>
36+
<div class="mat-layout-grid mat-layout-grid-align-center">
37+
<div class="mat-layout-grid-inner center">
38+
39+
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-2"></div>
40+
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-8">
41+
<MatPaper Elevation="2">
42+
<span>Resource not found</span>
43+
<div>
44+
<MatButton Style="margin-top:1em;" Label="Back to portfolio list" OnClick='() => { NavigationManager.NavigateTo($"/"); }'></MatButton>
45+
</div>
46+
</MatPaper>
47+
</div>
48+
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-2"></div>
49+
</div>
50+
</div>

0 commit comments

Comments
 (0)