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

refactor: throw exception if HTTP request fails #689

Merged
merged 4 commits into from
Sep 2, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/platform-sdk-ark/src/services/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ describe("ClientService", function () {
});

describe("#broadcast", () => {
it("should succeed", async () => {
it("should accept 1 transaction and reject 1 transaction", async () => {
nock(/.+/)
.post("/api/transactions")
.reply(200, require(`${__dirname}/../../test/fixtures/client/broadcast.json`));
.reply(422, require(`${__dirname}/../../test/fixtures/client/broadcast.json`));

const result = await subject.broadcast([]);

Expand Down
27 changes: 16 additions & 11 deletions packages/platform-sdk-ark/src/services/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,20 @@ export class ClientService implements Contracts.ClientService {
}

public async broadcast(transactions: Contracts.SignedTransactionData[]): Promise<Contracts.BroadcastResponse> {
const { data, errors } = await this.post("transactions", {
body: {
transactions: transactions.map((transaction: Contracts.SignedTransactionData) => transaction.data()),
},
});
let response: Contracts.KeyValuePair;
try {
response = await this.post("transactions", {
body: {
transactions: transactions.map((transaction: Contracts.SignedTransactionData) =>
transaction.data(),
),
},
});
} catch (error) {
response = error.response.json();
}

const { data, errors } = response;

const result: Contracts.BroadcastResponse = {
accepted: [],
Expand Down Expand Up @@ -143,15 +152,11 @@ export class ClientService implements Contracts.ClientService {
}

private async get(path: string, query?: Contracts.KeyValuePair): Promise<Contracts.KeyValuePair> {
const response = await this.#http.get(`${this.#peer}/${path}`, query?.searchParams);

return response.json();
return (await this.#http.get(`${this.#peer}/${path}`, query?.searchParams)).json();
}

private async post(path: string, { body, searchParams }: { body; searchParams? }): Promise<Contracts.KeyValuePair> {
const response = await this.#http.post(`${this.#peer}/${path}`, body, searchParams || undefined);

return response.json();
return (await this.#http.post(`${this.#peer}/${path}`, body, searchParams || undefined)).json();
}

private createMetaPagination(body): Contracts.MetaPagination {
Expand Down
2 changes: 0 additions & 2 deletions packages/platform-sdk/src/contracts/coins/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,4 @@ export interface HttpResponse {
clientError(): boolean;

serverError(): boolean;

throw(): HttpResponse;
}
33 changes: 22 additions & 11 deletions packages/platform-sdk/src/http/response.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
import { Primitive } from "type-fest";
import { HttpResponse } from "../contracts";
import { RequestException } from "./exceptions";

interface ResponseInput {
body: string | undefined;
headers: Record<string, Primitive>;
statusCode: number;
}

export class Response implements HttpResponse {
protected _response;
protected _response: ResponseInput;

protected _error: Error | undefined;

protected _body: string;
protected _body: string | undefined;

public constructor(response, error?: Error | undefined) {
public constructor(response: ResponseInput, error?: Error | undefined) {
this._response = response;
this._body = response.body || "";
this._body = response.body;
this._error = error;

this.throw();
}

public body(): string {
if (!this._body) {
throw new Error("The response body is empty.");
}

return this._body;
}

public json(): Record<string, any> {
return JSON.parse(this._body);
public json(): Record<string, Primitive> {
return JSON.parse(this.body());
}

public header(header: string): any {
public header(header: string): Primitive {
return this.headers()[header];
}

public headers(): Record<string, any> {
public headers(): Record<string, Primitive> {
return this._response.headers;
}

Expand Down Expand Up @@ -58,11 +71,9 @@ export class Response implements HttpResponse {
return this.status() >= 500;
}

public throw(): HttpResponse {
private throw(): void {
if (this.serverError() || this.clientError()) {
throw new RequestException(this, this._error);
}

return this;
}
}