From dd6c829803e82372c1f15738150adac680071140 Mon Sep 17 00:00:00 2001 From: SondreB Date: Mon, 22 Jun 2020 19:12:15 +0200 Subject: [PATCH] Add better error handling on address lookup - Add better error handling on address lookup. - Add handling of long segwit address, supporting any size by checking witness prefix. - Closes #13 --- .../explorer/address/address.component.html | 10 +++++ .../app/explorer/address/address.component.ts | 41 +++++++++++++++---- .../src/app/search/search.component.ts | 7 ++-- .../ClientApp/src/app/services/api.service.ts | 2 +- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/Blockcore.Explorer/ClientApp/src/app/explorer/address/address.component.html b/src/Blockcore.Explorer/ClientApp/src/app/explorer/address/address.component.html index 19b0c4d..c933a50 100644 --- a/src/Blockcore.Explorer/ClientApp/src/app/explorer/address/address.component.html +++ b/src/Blockcore.Explorer/ClientApp/src/app/explorer/address/address.component.html @@ -41,6 +41,11 @@

  Address

+
+ Error: {{error.title}}

+ {{error.errors | json}} +
+
Balance @@ -66,6 +71,11 @@

Transactions ({{total}})

Loading transactions... +
+ Error: {{errorTransactions.title}}

+ {{errorTransactions.errors | json}} +
+
Date diff --git a/src/Blockcore.Explorer/ClientApp/src/app/explorer/address/address.component.ts b/src/Blockcore.Explorer/ClientApp/src/app/explorer/address/address.component.ts index 9a08878..5db77ca 100644 --- a/src/Blockcore.Explorer/ClientApp/src/app/explorer/address/address.component.ts +++ b/src/Blockcore.Explorer/ClientApp/src/app/explorer/address/address.component.ts @@ -1,7 +1,7 @@ import { Component, HostBinding, OnInit, OnDestroy, HostListener } from '@angular/core'; import { ActivatedRoute, Route, Router } from '@angular/router'; import { ApiComponent } from 'src/app/api/api.component'; -import { ApiService } from 'src/app/services/api.service'; +import { ApiService, HttpError } from 'src/app/services/api.service'; import { SetupService } from 'src/app/services/setup.service'; import { ScrollEvent } from 'src/app/shared/scroll.directive'; @@ -35,6 +35,8 @@ export class AddressComponent implements OnInit, OnDestroy { count = 0; total: any; link: string; + error: any; + errorTransactions: any; constructor( private api: ApiService, @@ -47,12 +49,27 @@ export class AddressComponent implements OnInit, OnDestroy { console.log('Address:', id); this.transactions = null; - this.address = id; - this.balance = await this.api.getAddress(id); - console.log(this.balance); - await this.updateTransactions('/api/query/address/' + id + '/transactions?limit=' + this.limit); + try { + this.balance = await this.api.getAddress(id); + } catch (err) { + if (err.message[0] === '{') { + this.error = JSON.parse(err.message); + } else { + this.error = err; + } + } + + try { + await this.updateTransactions('/api/query/address/' + id + '/transactions?limit=' + this.limit); + } catch (err) { + if (err.message[0] === '{') { + this.errorTransactions = JSON.parse(err.message); + } else { + this.errorTransactions = err; + } + } }); } @@ -85,6 +102,17 @@ export class AddressComponent implements OnInit, OnDestroy { // For the block scrolling (using link http header), we must manually set full URL. const response = await this.api.request(baseUrl + url); + // When the offset is not set (0), we should reverse the order of items. + const list = await response.json(); + + if (response.status !== 200) { + if (list && list.status) { + throw new HttpError(list.status, url, JSON.stringify(list)); + } else { + throw new HttpError(response.status, url, response.statusText); + } + } + this.total = response.headers.get('Pagination-Total'); const linkHeader = response.headers.get('Link'); const links = this.api.parseLinkHeader(linkHeader); @@ -92,8 +120,7 @@ export class AddressComponent implements OnInit, OnDestroy { // This will be set to undefined/null when no more next links is available. this.link = links['previous']; - // When the offset is not set (0), we should reverse the order of items. - const list = await response.json(); + if (!this.transactions) { this.transactions = []; diff --git a/src/Blockcore.Explorer/ClientApp/src/app/search/search.component.ts b/src/Blockcore.Explorer/ClientApp/src/app/search/search.component.ts index 50119ec..312c453 100644 --- a/src/Blockcore.Explorer/ClientApp/src/app/search/search.component.ts +++ b/src/Blockcore.Explorer/ClientApp/src/app/search/search.component.ts @@ -27,9 +27,10 @@ export class SearchComponent implements OnInit, OnDestroy { } - inputType(value) { - // LONG_MAX: 9223372036854775807 - if (value.length < 20) { + inputType(value: string) { + if (value.startsWith(this.setup.Network.NetworkWitnessPrefix)) { + return 'address'; + } else if (value.length < 20) { // LONG_MAX: 9223372036854775807 return 'index'; } else if (value.length > 30 && value.length < 54) { return 'address'; diff --git a/src/Blockcore.Explorer/ClientApp/src/app/services/api.service.ts b/src/Blockcore.Explorer/ClientApp/src/app/services/api.service.ts index 2cc9b86..bb5591d 100644 --- a/src/Blockcore.Explorer/ClientApp/src/app/services/api.service.ts +++ b/src/Blockcore.Explorer/ClientApp/src/app/services/api.service.ts @@ -44,7 +44,7 @@ export class ApiService { if (response.status !== 200) { if (json && json.status) { - throw new HttpError(json.status, url, json.title); + throw new HttpError(json.status, url, JSON.stringify(json)); } else { throw new HttpError(response.status, url, response.statusText); }