Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Extend APIError to have errors from core #919

Merged
merged 5 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/lisk-api-client/src/api_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/
import { AxiosRequestConfig } from 'axios';
import {
ApiHandler,
ApiResponse,
APIHandler,
APIResponse,
HashMap,
RequestConfig,
Resource,
Expand All @@ -24,12 +24,12 @@ import { GET } from './constants';
import { solveURLParams, toQueryString } from './utils';

// Bind to resource class
export const apiMethod = (options: RequestConfig = {}): ApiHandler =>
export const apiMethod = (options: RequestConfig = {}): APIHandler =>
async function apiHandler(
this: Resource,
// tslint:disable-next-line readonly-array
...args: Array<number | string | object>
): Promise<ApiResponse> {
): Promise<APIResponse> {
const {
method = GET,
path = '',
Expand Down
34 changes: 17 additions & 17 deletions packages/lisk-api-client/src/api_resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
import Axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
import { APIClient } from './api_client';
import { ApiResponse, HashMap } from './api_types';
import { APIErrorResponse, APIResponse, HashMap } from './api_types';
import { APIError } from './errors';

const API_RECONNECT_MAX_RETRY_COUNT = 3;
Expand Down Expand Up @@ -42,12 +42,12 @@ export class APIResource {
error: Error,
req: AxiosRequestConfig,
retryCount: number,
): Promise<ApiResponse> {
): Promise<APIResponse> {
if (this.apiClient.hasAvailableNodes()) {
return new Promise<ApiResponse>(resolve =>
return new Promise<APIResponse>(resolve =>
setTimeout(resolve, REQUEST_RETRY_TIMEOUT),
).then(
async (): Promise<ApiResponse> => {
async (): Promise<APIResponse> => {
if (retryCount > API_RECONNECT_MAX_RETRY_COUNT) {
throw error;
}
Expand All @@ -67,26 +67,26 @@ export class APIResource {
req: AxiosRequestConfig,
retry: boolean,
retryCount: number = 1,
): Promise<ApiResponse> {
): Promise<APIResponse> {
const request = Axios.request(req)
.then((res: AxiosResponse) => res.data)
.catch(
(error: AxiosError): Error => {
(error: AxiosError): void => {
if (error.response) {
if (error.response.data && error.response.data.message) {
const { status } = error.response;
if (error.response.data) {
const {
error: errorString,
shuse2 marked this conversation as resolved.
Show resolved Hide resolved
errors,
message,
}: APIErrorResponse = error.response.data;
throw new APIError(
`Status ${error.response.status} : ${
error.response.data.message
}`,
error.response.status,
message || errorString || 'An unknown error has occurred.',
status,
errors,
);
}
throw new APIError(
`Status ${
error.response.status
} : An unknown error has occurred.`,
error.response.status,
);
throw new APIError('An unknown error has occurred.', status);
}
throw error;
},
Expand Down
19 changes: 15 additions & 4 deletions packages/lisk-api-client/src/api_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,28 @@
*
*/
/* tslint:disable no-mixed-interface */
export type ApiHandler = (
export type APIHandler = (
// tslint:disable-next-line readonly-array
...args: Array<number | string | object>
) => Promise<ApiResponse>;
) => Promise<APIResponse>;

export interface ApiResponse {
export interface APIResponse {
readonly data: unknown;
readonly links: object;
readonly meta: object;
}

export interface APIErrorResponse {
readonly error?: string;
readonly errors?: ReadonlyArray<APIErrorContents>;
readonly message?: string;
}

export interface APIErrorContents {
readonly code?: string;
readonly message?: string;
}

export interface HashMap {
readonly [key: string]: string;
}
Expand All @@ -48,6 +59,6 @@ export interface RequestConfig {
export interface Resource {
readonly headers: HashMap;
readonly path: string;
readonly request: (data: object, retry: boolean) => Promise<ApiResponse>;
readonly request: (data: object, retry: boolean) => Promise<APIResponse>;
readonly resourcePath: string;
}
22 changes: 17 additions & 5 deletions packages/lisk-api-client/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@ import { VError } from 'verror';

const defaultErrorNo = 500;

export interface APIErrorData {
readonly code?: string;
readonly message?: string;
}

export class APIError extends VError {
public errno?: number;
public constructor(message?: string, errno?: number) {
super(message || '');
this.name = 'API Error';
this.errno = errno || defaultErrorNo;
public errno: number;
public errors?: ReadonlyArray<APIErrorData>;

public constructor(
message: string = '',
errno: number = defaultErrorNo,
errors?: ReadonlyArray<APIErrorData>,
) {
super(message);
this.name = 'APIError';
this.errno = errno;
this.errors = errors;
}
}
8 changes: 4 additions & 4 deletions packages/lisk-api-client/src/resources/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import { APIClient } from '../api_client';
import { apiMethod } from '../api_method';
import { APIResource } from '../api_resource';
import { ApiHandler } from '../api_types';
import { APIHandler } from '../api_types';
import { GET } from '../constants';

export class AccountsResource extends APIResource {
public get: ApiHandler;
public getMultisignatureGroups: ApiHandler;
public getMultisignatureMemberships: ApiHandler;
public get: APIHandler;
public getMultisignatureGroups: APIHandler;
public getMultisignatureMemberships: APIHandler;
public path: string;

public constructor(apiClient: APIClient) {
Expand Down
4 changes: 2 additions & 2 deletions packages/lisk-api-client/src/resources/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import { APIClient } from '../api_client';
import { apiMethod } from '../api_method';
import { APIResource } from '../api_resource';
import { ApiHandler } from '../api_types';
import { APIHandler } from '../api_types';
import { GET } from '../constants';

export class BlocksResource extends APIResource {
public get: ApiHandler;
public get: APIHandler;
public path: string;

public constructor(apiClient: APIClient) {
Expand Down
4 changes: 2 additions & 2 deletions packages/lisk-api-client/src/resources/dapps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import { APIClient } from '../api_client';
import { apiMethod } from '../api_method';
import { APIResource } from '../api_resource';
import { ApiHandler } from '../api_types';
import { APIHandler } from '../api_types';
import { GET } from '../constants';

export class DappsResource extends APIResource {
public get: ApiHandler;
public get: APIHandler;
public path: string;

public constructor(apiClient: APIClient) {
Expand Down
10 changes: 5 additions & 5 deletions packages/lisk-api-client/src/resources/delegates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import { APIClient } from '../api_client';
import { apiMethod } from '../api_method';
import { APIResource } from '../api_resource';
import { ApiHandler } from '../api_types';
import { APIHandler } from '../api_types';
import { GET } from '../constants';

export class DelegatesResource extends APIResource {
public get: ApiHandler;
public getForgers: ApiHandler;
public getForgingStatistics: ApiHandler;
public getStandby: ApiHandler;
public get: APIHandler;
public getForgers: APIHandler;
public getForgingStatistics: APIHandler;
public getStandby: APIHandler;
public path: string;

public constructor(apiClient: APIClient) {
Expand Down
12 changes: 6 additions & 6 deletions packages/lisk-api-client/src/resources/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
import { APIClient } from '../api_client';
import { apiMethod } from '../api_method';
import { APIResource } from '../api_resource';
import { ApiHandler } from '../api_types';
import { APIHandler } from '../api_types';
import { GET, PUT } from '../constants';

export class NodeResource extends APIResource {
public getConstants: ApiHandler;
public getForgingStatus: ApiHandler;
public getStatus: ApiHandler;
public getTransactions: ApiHandler;
public getConstants: APIHandler;
public getForgingStatus: APIHandler;
public getStatus: APIHandler;
public getTransactions: APIHandler;
public path: string;
public updateForgingStatus: ApiHandler;
public updateForgingStatus: APIHandler;

public constructor(apiClient: APIClient) {
super(apiClient);
Expand Down
4 changes: 2 additions & 2 deletions packages/lisk-api-client/src/resources/peers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import { APIClient } from '../api_client';
import { apiMethod } from '../api_method';
import { APIResource } from '../api_resource';
import { ApiHandler } from '../api_types';
import { APIHandler } from '../api_types';
import { GET } from '../constants';

export class PeersResource extends APIResource {
public get: ApiHandler;
public get: APIHandler;
public path: string;

public constructor(apiClient: APIClient) {
Expand Down
4 changes: 2 additions & 2 deletions packages/lisk-api-client/src/resources/signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import { APIClient } from '../api_client';
import { apiMethod } from '../api_method';
import { APIResource } from '../api_resource';
import { ApiHandler } from '../api_types';
import { APIHandler } from '../api_types';
import { POST } from '../constants';

export class SignaturesResource extends APIResource {
public broadcast: ApiHandler;
public broadcast: APIHandler;
public path: string;

public constructor(apiClient: APIClient) {
Expand Down
6 changes: 3 additions & 3 deletions packages/lisk-api-client/src/resources/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
import { APIClient } from '../api_client';
import { apiMethod } from '../api_method';
import { APIResource } from '../api_resource';
import { ApiHandler } from '../api_types';
import { APIHandler } from '../api_types';
import { GET, POST } from '../constants';

export class TransactionsResource extends APIResource {
public broadcast: ApiHandler;
public get: ApiHandler;
public broadcast: APIHandler;
public get: APIHandler;
public path: string;
public constructor(apiClient: APIClient) {
super(apiClient);
Expand Down
4 changes: 2 additions & 2 deletions packages/lisk-api-client/src/resources/voters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import { APIClient } from '../api_client';
import { apiMethod } from '../api_method';
import { APIResource } from '../api_resource';
import { ApiHandler } from '../api_types';
import { APIHandler } from '../api_types';
import { GET } from '../constants';

export class VotersResource extends APIResource {
public get: ApiHandler;
public get: APIHandler;
public path: string;

public constructor(apiClient: APIClient) {
Expand Down
4 changes: 2 additions & 2 deletions packages/lisk-api-client/src/resources/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import { APIClient } from '../api_client';
import { apiMethod } from '../api_method';
import { APIResource } from '../api_resource';
import { ApiHandler } from '../api_types';
import { APIHandler } from '../api_types';
import { GET } from '../constants';

export class VotesResource extends APIResource {
public get: ApiHandler;
public get: APIHandler;
public path: string;

public constructor(apiClient: APIClient) {
Expand Down
4 changes: 2 additions & 2 deletions packages/lisk-api-client/test/api_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
import { expect } from 'chai';
import { apiMethod } from '../src/api_method';
import { Resource, ApiHandler } from '../src/api_types';
import { Resource, APIHandler } from '../src/api_types';

describe('API method module', () => {
const GET = 'GET';
Expand All @@ -36,7 +36,7 @@ describe('API method module', () => {
const secondURLParam = 123;
let resource: Resource;
let requestResult: object;
let handler: ApiHandler;
let handler: APIHandler;
let validationError: Error;

beforeEach(() => {
Expand Down
Loading