From ad3c295fd66c3063947cd10933f7add7b7bef86a Mon Sep 17 00:00:00 2001 From: softsimon Date: Thu, 16 Jul 2020 16:46:10 +0700 Subject: [PATCH] Handle errors in block component. --- .../bisq/bisq-block/bisq-block.component.html | 14 +++++++--- .../bisq/bisq-block/bisq-block.component.ts | 26 +++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/frontend/src/app/bisq/bisq-block/bisq-block.component.html b/frontend/src/app/bisq/bisq-block/bisq-block.component.html index 148ab3121..4e1c7b95c 100644 --- a/frontend/src/app/bisq/bisq-block/bisq-block.component.html +++ b/frontend/src/app/bisq/bisq-block/bisq-block.component.html @@ -66,9 +66,7 @@ - - - +
@@ -97,4 +95,14 @@
+ +
+ +
+ Error loading block +
+ {{ error.status }}: {{ error.statusText }} +
+
+
\ No newline at end of file diff --git a/frontend/src/app/bisq/bisq-block/bisq-block.component.ts b/frontend/src/app/bisq/bisq-block/bisq-block.component.ts index 07f8a086b..9ce3be8fb 100644 --- a/frontend/src/app/bisq/bisq-block/bisq-block.component.ts +++ b/frontend/src/app/bisq/bisq-block/bisq-block.component.ts @@ -4,9 +4,10 @@ import { Location } from '@angular/common'; import { BisqApiService } from '../bisq-api.service'; import { ActivatedRoute, ParamMap, Router } from '@angular/router'; import { Subscription, of } from 'rxjs'; -import { switchMap } from 'rxjs/operators'; +import { switchMap, catchError } from 'rxjs/operators'; import { SeoService } from 'src/app/services/seo.service'; import { ElectrsApiService } from 'src/app/services/electrs-api.service'; +import { HttpErrorResponse } from '@angular/common/http'; @Component({ selector: 'app-bisq-block', @@ -19,7 +20,7 @@ export class BisqBlockComponent implements OnInit, OnDestroy { blockHash = ''; blockHeight = 0; isLoading = true; - error: any; + error: HttpErrorResponse | null; constructor( private bisqApiService: BisqApiService, @@ -37,6 +38,7 @@ export class BisqBlockComponent implements OnInit, OnDestroy { const blockHash = params.get('id') || ''; document.body.scrollTo(0, 0); this.isLoading = true; + this.error = null; if (history.state.data && history.state.data.blockHeight) { this.blockHeight = history.state.data.blockHeight; } @@ -56,19 +58,28 @@ export class BisqBlockComponent implements OnInit, OnDestroy { return this.electrsApiService.getBlockHashFromHeight$(parseInt(blockHash, 10)) .pipe( switchMap((hash) => { + if (!hash) { + return; + } this.blockHash = hash; this.location.replaceState( this.router.createUrlTree(['/bisq/block/', hash]).toString() ); - return this.bisqApiService.getBlock$(this.blockHash); - }) + return this.bisqApiService.getBlock$(this.blockHash) + .pipe(catchError(this.caughtHttpError.bind(this))); + }), + catchError(this.caughtHttpError.bind(this)) ); } - return this.bisqApiService.getBlock$(this.blockHash); + return this.bisqApiService.getBlock$(this.blockHash) + .pipe(catchError(this.caughtHttpError.bind(this))); }) ) .subscribe((block: BisqBlock) => { + if (!block) { + return; + } this.isLoading = false; this.blockHeight = block.height; this.seoService.setTitle('Block: #' + block.height + ': ' + block.hash, true); @@ -79,4 +90,9 @@ export class BisqBlockComponent implements OnInit, OnDestroy { ngOnDestroy() { this.subscription.unsubscribe(); } + + caughtHttpError(err: HttpErrorResponse){ + this.error = err; + return of(null); + } }