Skip to content

Commit

Permalink
refactor(/api/0.6/changeset/create): error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
galta95 committed Nov 29, 2020
1 parent 8718281 commit 45e5e8f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 38 deletions.
12 changes: 10 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
},
"homepage": "https://github.com/MapColonies/node-osm-api#readme",
"dependencies": {
"@types/node": "^14.14.9",
"axios": "^0.21.0"
"axios": "^0.21.0",
"http-status-codes": "^2.1.4"
},
"devDependencies": {
"@types/node": "^14.14.9",
"@commitlint/config-conventional": "^11.0.0",
"@map-colonies/eslint-config": "^1.1.0",
"@types/chai": "^4.2.14",
Expand Down
27 changes: 19 additions & 8 deletions src/api/v6/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
import axios, { AxiosInstance, AxiosResponse } from 'axios';
import { IResponse } from '../../lib/response-handler';
import axios, { AxiosError, AxiosInstance, AxiosResponse } from 'axios';
import StatusCodes from 'http-status-codes';
import { createChangesetEndPoint } from '../../lib/endpoints';
import HttpErrorHandler from '../../lib/http-error-handler';
import { Unauthorized, BADXml, InternalServerError } from '../../lib/error-handler';
class Apiv6 {
private readonly httpClient: AxiosInstance;

public constructor(private readonly baseUrl: string, username: string, password: string) {
this.httpClient = axios.create({ baseURL: baseUrl, auth: { username, password } });
}

public async createChangeset(data: string): Promise<IResponse<number>> {
public async createChangeset(data: string): Promise<number> {
let res: AxiosResponse<number>;
try {
res = await this.httpClient.put<number>(createChangesetEndPoint, data);
}
}
catch (e) {
throw new HttpErrorHandler(e);
const axiosError = e as AxiosError;

if (!axiosError.response) {
throw new InternalServerError(e);
}
if (axiosError.response.status === StatusCodes.BAD_REQUEST) {
throw new BADXml(axiosError);
} else if (axiosError.response.status === StatusCodes.UNAUTHORIZED) {
throw new Unauthorized(axiosError);
} else {
throw new InternalServerError(e);
}
}
const { status, data: changeSetId } = res;
return { status, data: changeSetId };
const { data: changeSetId } = res;
return changeSetId;
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/lib/error-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AxiosError } from "axios";

class HttpErrorHandler extends Error {
public constructor(error: AxiosError) {
super(error.response?.data);
Object.setPrototypeOf(this, HttpErrorHandler.prototype);
}
}

export class Unauthorized extends HttpErrorHandler {
public constructor(error: AxiosError) {
super(error);
Object.setPrototypeOf(this, Unauthorized.prototype);
}
}

export class BADXml extends HttpErrorHandler {
public constructor(error: AxiosError) {
super(error);
Object.setPrototypeOf(this, BADXml.prototype);
}
}

export class InternalServerError extends HttpErrorHandler {
public constructor(error: AxiosError) {
super(error);
Object.setPrototypeOf(this, InternalServerError.prototype);
}
}
15 changes: 0 additions & 15 deletions src/lib/http-error-handler.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/lib/response-handler.ts

This file was deleted.

9 changes: 2 additions & 7 deletions tests/unit/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ describe('apiv6', function () {
const xmlData = createChangesetXml("test-generator", "test-user");
const res = await apiv6.createChangeset(xmlData);

expect(res).to.be.a('object')
.with.property('status')
.and.to.be.equal(200);
expect(res).to.have.property('data')
.and.to.be.equal(12);
expect(res).to.be.equal(12);
});
});
describe('with unregisterd user', function () {
Expand All @@ -38,11 +34,10 @@ describe('apiv6', function () {
try {
await apiv6.createChangeset(xmlData);
} catch (e) {
console.log(e);
expect(e).to.be.a('Error')
.with.property('message')
.and.to.be.equal('Couldn\'t authenticate you');
expect(e).to.have.property('status')
.and.to.be.equal(401);
}
});
});
Expand Down

0 comments on commit 45e5e8f

Please sign in to comment.