Skip to content

Commit

Permalink
Add better error handling on address lookup
Browse files Browse the repository at this point in the history
- Add better error handling on address lookup.
- Add handling of long segwit address, supporting any size by checking witness prefix.
- Closes #13
  • Loading branch information
sondreb committed Jun 22, 2020
1 parent 816c2ea commit dd6c829
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ <h3><i class="fas fa-wallet"></i>&nbsp;&nbsp;Address</h3>

<app-progress class="centered" *ngIf="!balance"></app-progress>

<div *ngIf="error">
<span class="muted">Error: </span> <span class="negative">{{error.title}}</span><br><br>
{{error.errors | json}}
</div>

<div class="grid-label-value" *ngIf="balance">
<div>
<span>Balance</span>
Expand All @@ -66,6 +71,11 @@ <h3>Transactions ({{total}})</h3>

<app-progress class="centered" *ngIf="!transactions">Loading transactions...</app-progress>

<div *ngIf="errorTransactions">
<span class="muted">Error: </span> <span class="negative">{{errorTransactions.title}}</span><br><br>
{{errorTransactions.errors | json}}
</div>

<div *ngIf="transactions">
<div class="grid-list-transactions">
<span>Date</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -35,6 +35,8 @@ export class AddressComponent implements OnInit, OnDestroy {
count = 0;
total: any;
link: string;
error: any;
errorTransactions: any;

constructor(
private api: ApiService,
Expand All @@ -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;
}
}
});
}

Expand Down Expand Up @@ -85,15 +102,25 @@ 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);

// 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 = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit dd6c829

Please sign in to comment.