Skip to content

Commit fb67fe9

Browse files
authored
Merge pull request #191 from companieshouse/ncs-449-return-error-response-if-error
Return ApiErrorResponse is error found on response
2 parents 3394c87 + 57d0ea6 commit fb67fe9

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/services/transaction/service.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { IHttpClient } from "../../http";
2-
import {
3-
Transaction, TransactionResource
4-
5-
} from "./types";
6-
import Resource from "../resource";
2+
import { Transaction, TransactionResource } from "./types";
3+
import Resource, { ApiErrorResponse } from "../resource";
74

85
export default class TransactionService {
96
constructor (private readonly client: IHttpClient) { }
@@ -13,22 +10,25 @@ export default class TransactionService {
1310
*
1411
* @param transaction the transaction to create
1512
*/
16-
public async postTransaction (transaction: Transaction): Promise<Resource<Transaction>> {
13+
public async postTransaction (transaction: Transaction): Promise<Resource<Transaction>|ApiErrorResponse> {
1714
let url = "/transactions"
1815
if (transaction.id) {
1916
url += "/" + transaction.id
2017
}
2118

2219
const resp = await this.client.httpPost(url);
2320

21+
if (resp.error) {
22+
return {
23+
httpStatusCode: resp.status,
24+
errors: [resp.error]
25+
};
26+
}
27+
2428
const resource: Resource<Transaction> = {
2529
httpStatusCode: resp.status
2630
};
2731

28-
if (resp.error) {
29-
return resource;
30-
}
31-
3232
// cast the response body to the expected type
3333
const body = resp.body as TransactionResource;
3434

test/services/transaction/service.spec.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import sinon from "sinon";
44
import TransactionService from "../../../src/services/transaction/service";
55
import { RequestClient } from "../../../src/http";
66
import { Transaction, TransactionResource } from "../../../src/services/transaction";
7+
import { ApiErrorResponse } from "../../../src/services/resource";
8+
import { Resource } from "../../../src";
79
const expect = chai.expect;
810

911
const requestClient = new RequestClient({ baseUrl: "URL-NOT-USED", oauthToken: "TOKEN-NOT-USED" });
@@ -31,7 +33,8 @@ describe("transaction", () => {
3133
const data = await transaction.postTransaction({} as Transaction);
3234

3335
expect(data.httpStatusCode).to.equal(401);
34-
expect(data.resource).to.be.undefined;
36+
const castedData: ApiErrorResponse = data;
37+
expect(castedData.errors[0]).to.equal("An error occurred");
3538
});
3639

3740
it("maps the company field data items correctly", async () => {
@@ -56,10 +59,11 @@ describe("transaction", () => {
5659
const data = await transaction.postTransaction({} as Transaction);
5760

5861
expect(data.httpStatusCode).to.equal(200);
59-
expect(data.resource.companyName).to.equal(mockResponseBody.company_name);
60-
expect(data.resource.companyNumber).to.equal(mockResponseBody.company_number);
61-
expect(data.resource.links.self).to.equal(mockResponseBody.links.self);
62-
expect(data.resource.reference).to.equal(mockResponseBody.reference);
63-
expect(data.resource.description).to.equal(mockResponseBody.description);
62+
const castedData: Resource<Transaction> = data as Resource<Transaction>;
63+
expect(castedData.resource.companyName).to.equal(mockResponseBody.company_name);
64+
expect(castedData.resource.companyNumber).to.equal(mockResponseBody.company_number);
65+
expect(castedData.resource.links.self).to.equal(mockResponseBody.links.self);
66+
expect(castedData.resource.reference).to.equal(mockResponseBody.reference);
67+
expect(castedData.resource.description).to.equal(mockResponseBody.description);
6468
});
6569
});

0 commit comments

Comments
 (0)