Handle errors in block component.

This commit is contained in:
softsimon 2020-07-16 16:46:10 +07:00
parent d3d3fd0db1
commit ad3c295fd6
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
2 changed files with 32 additions and 8 deletions

View File

@ -66,9 +66,7 @@
</ng-template> </ng-template>
<ng-template [ngIf]="isLoading"> <ng-template [ngIf]="isLoading && !error">
<div class="box"> <div class="box">
<div class="row"> <div class="row">
<div class="col-sm"> <div class="col-sm">
@ -97,4 +95,14 @@
</div> </div>
</ng-template> </ng-template>
<ng-template [ngIf]="error">
<div class="clearfix"></div>
<div class="text-center">
Error loading block
<br>
<i>{{ error.status }}: {{ error.statusText }}</i>
</div>
</ng-template>
</div> </div>

View File

@ -4,9 +4,10 @@ import { Location } from '@angular/common';
import { BisqApiService } from '../bisq-api.service'; import { BisqApiService } from '../bisq-api.service';
import { ActivatedRoute, ParamMap, Router } from '@angular/router'; import { ActivatedRoute, ParamMap, Router } from '@angular/router';
import { Subscription, of } from 'rxjs'; 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 { SeoService } from 'src/app/services/seo.service';
import { ElectrsApiService } from 'src/app/services/electrs-api.service'; import { ElectrsApiService } from 'src/app/services/electrs-api.service';
import { HttpErrorResponse } from '@angular/common/http';
@Component({ @Component({
selector: 'app-bisq-block', selector: 'app-bisq-block',
@ -19,7 +20,7 @@ export class BisqBlockComponent implements OnInit, OnDestroy {
blockHash = ''; blockHash = '';
blockHeight = 0; blockHeight = 0;
isLoading = true; isLoading = true;
error: any; error: HttpErrorResponse | null;
constructor( constructor(
private bisqApiService: BisqApiService, private bisqApiService: BisqApiService,
@ -37,6 +38,7 @@ export class BisqBlockComponent implements OnInit, OnDestroy {
const blockHash = params.get('id') || ''; const blockHash = params.get('id') || '';
document.body.scrollTo(0, 0); document.body.scrollTo(0, 0);
this.isLoading = true; this.isLoading = true;
this.error = null;
if (history.state.data && history.state.data.blockHeight) { if (history.state.data && history.state.data.blockHeight) {
this.blockHeight = 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)) return this.electrsApiService.getBlockHashFromHeight$(parseInt(blockHash, 10))
.pipe( .pipe(
switchMap((hash) => { switchMap((hash) => {
if (!hash) {
return;
}
this.blockHash = hash; this.blockHash = hash;
this.location.replaceState( this.location.replaceState(
this.router.createUrlTree(['/bisq/block/', hash]).toString() 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) => { .subscribe((block: BisqBlock) => {
if (!block) {
return;
}
this.isLoading = false; this.isLoading = false;
this.blockHeight = block.height; this.blockHeight = block.height;
this.seoService.setTitle('Block: #' + block.height + ': ' + block.hash, true); this.seoService.setTitle('Block: #' + block.height + ': ' + block.hash, true);
@ -79,4 +90,9 @@ export class BisqBlockComponent implements OnInit, OnDestroy {
ngOnDestroy() { ngOnDestroy() {
this.subscription.unsubscribe(); this.subscription.unsubscribe();
} }
caughtHttpError(err: HttpErrorResponse){
this.error = err;
return of(null);
}
} }