Skip to content

Commit

Permalink
Merge pull request #35 from Azure-Samples/32-improve-the-exception-ha…
Browse files Browse the repository at this point in the history
…ndling

Update the application to display the error message in the frontend
  • Loading branch information
pmalarme authored Aug 28, 2023
2 parents 5570a54 + 923ff6b commit 33f42bf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
logging.level.org.hibernate.SQL=INFO

server.error.include-message = always

spring.datasource.url=<add your URL here>
spring.datasource.username=<add your username here>
spring.datasource.password=<add your password here>
Expand Down
30 changes: 23 additions & 7 deletions src/frontend/src/components/AiNutritionAnalysis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,30 @@ export class AiNutritionAnalysis extends React.Component<AiNutritionAnalysisProp
};

componentDidMount(): void {
let responseOk: boolean = true;
fetch(`${API_URL}/api/cart-items/ai-nutrition-analysis`)
.then((response) => response.json())
.then((data) => this.setState({
nutriscore: data.nutriscore,
explanation: data.explanation,
recommendation: data.recommendation,
status: Status.LOADED
}))
.then((response) => {
responseOk = response.ok;
return response.json()
})
.then((data) => {
if (!responseOk) {
this.setState({
nutriscore: '',
explanation: '',
recommendation: '',
status: Status.ERROR,
errorMessage: data.message,
});
} else {
this.setState({
nutriscore: data.nutriscore,
explanation: data.explanation,
recommendation: data.recommendation,
status: Status.LOADED
});
}
})
.catch((error) => this.setState({
nutriscore: '',
explanation: '',
Expand Down
27 changes: 22 additions & 5 deletions src/frontend/src/components/Top3Recipes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,26 @@ export class Top3Recipes extends React.Component<RecipesProps, RecipesState> {
};

componentDidMount(): void {
let responseOk: boolean = true;
fetch(`${API_URL}/api/cart-items/top-3-recipes`)
.then((response) => response.json())
.then((data) => this.setState({
recipes: data.recipes,
status: Status.LOADED
}))
.then((response) => {
responseOk = response.ok;
return response.json()
})
.then((data) => {
if (!responseOk) {
this.setState({
recipes: [] as Array<Recipe>,
status: Status.ERROR,
errorMessage: data.message
});
} else {
this.setState({
recipes: data.recipes,
status: Status.LOADED
});
}
})
.catch((error) => this.setState({
recipes: [] as Array<Recipe>,
status: Status.ERROR,
Expand All @@ -57,6 +71,9 @@ export class Top3Recipes extends React.Component<RecipesProps, RecipesState> {
}

getHtmlForRecipes(): Array<JSX.Element> {
if (this.state === undefined || this.state.recipes === undefined || this.state.recipes.length === 0) {
return [<Card.Text key='no-recipes'>No recipes found</Card.Text>];
}
return this.state.recipes.map((recipe) => this.getRecipeHtml(recipe));
}

Expand Down

0 comments on commit 33f42bf

Please sign in to comment.