Skip to content
This repository has been archived by the owner on Jul 10, 2018. It is now read-only.

Commit

Permalink
Improving error handling (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtlynch committed May 2, 2018
1 parent b29418b commit a8481cf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/app/public/home-page/home-page.component.html
Expand Up @@ -11,8 +11,8 @@
<div *ngIf="isWaitingForParseResult">
<mat-spinner></mat-spinner>
</div>
<div *ngIf="parseResult && parseResult.error">
<p class="error">{{ parseResult.error }}</p>
<div class="error" *ngIf="error">
<p>{{ error }}</p>
</div>
<mat-list role="list" *ngIf="!isWaitingForParseResult && parseResult && parseResult.ingredientParsed">
<mat-list-item role="listitem"><span class="fieldName">Quantity</span>: {{ parseResult.ingredientParsed.quantity }}</mat-list-item>
Expand Down
4 changes: 4 additions & 0 deletions src/app/public/home-page/home-page.component.scss
Expand Up @@ -23,4 +23,8 @@ span.fieldName {

.error {
color: red;
}

div.error {
margin-top: 15px;
}
15 changes: 15 additions & 0 deletions src/app/public/home-page/home-page.component.ts
Expand Up @@ -12,21 +12,32 @@ export class HomePageComponent implements OnInit {
ingredientRaw: string;
isWaitingForParseResult: boolean = false;
parseResult: ParseResult;
error: string;

constructor(private parserService: ParserService) { }

ngOnInit() {}

parse(raw: string) {
this.isWaitingForParseResult = true;
this.error = null;
this.parseResult = null;
this.parserService.parseIngredient(raw).subscribe(
(response) => {
this.isWaitingForParseResult = false;
this.parseResult = response;
if (this.parseResult.error) {
this.error = this.parseResult.error;
}
},
(error) => {
this.isWaitingForParseResult = false;
console.log(error);
if (error.error && this.isString(error.error)) {
this.error = error.error;
} else {
this.error = error.statusText;
}
});
}

Expand All @@ -35,4 +46,8 @@ export class HomePageComponent implements OnInit {
this.parseResult = null;
}

private isString(x) {
return Object.prototype.toString.call(x) === '[object String]';
}

}

0 comments on commit a8481cf

Please sign in to comment.