Skip to content

Commit

Permalink
Chore: Only emit errors from base API class
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsum committed Dec 7, 2018
1 parent c1eb361 commit bbc73e0
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/api/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import EventEmitter from 'events';
import axios from 'axios';

import { getHeaders } from '../util';
import { PLACEHOLDER_USER, ANNOTATOR_EVENT, ERROR_TYPE } from '../constants';
import { PLACEHOLDER_USER, ANNOTATOR_EVENT } from '../constants';

class API extends EventEmitter {
/** @property {string} */
Expand Down Expand Up @@ -95,7 +95,7 @@ class API extends EventEmitter {
*/
errorHandler = (error: $AxiosError): void => {
this.emit(ANNOTATOR_EVENT.error, {
reason: ERROR_TYPE.auth,
reason: error.name,
error: error.toString()
});
};
Expand Down
14 changes: 5 additions & 9 deletions src/api/AnnotationAPI.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import API from './API';
import { ANNOTATOR_EVENT, ERROR_TYPE } from '../constants';
import { ERROR_TYPE } from '../constants';

const HTTP_POST = 'POST';
const HTTP_DELETE = 'DELETE';
Expand Down Expand Up @@ -57,10 +57,8 @@ class AnnotationAPI extends API {
createSuccessHandler = (data: Object): CommentProps => {
if (data.type === 'error') {
const error = new Error('Could not create annotation');
this.emit(ANNOTATOR_EVENT.error, {
reason: ERROR_TYPE.create,
error: error.toString()
});
error.name = ERROR_TYPE.create;
this.errorHandler(error);
return data;
}

Expand Down Expand Up @@ -94,10 +92,8 @@ class AnnotationAPI extends API {
deleteSuccessHandler = (data: Object, id: string) => {
if (data.type === 'error' || !data.id) {
const error = new Error(`Could not delete annotation with ID ${id}`);
this.emit(ANNOTATOR_EVENT.error, {
reason: ERROR_TYPE.delete,
error: error.toString()
});
error.name = ERROR_TYPE.delete;
this.errorHandler(error);
}

return data;
Expand Down
7 changes: 2 additions & 5 deletions src/api/FileVersionAPI.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import API from './API';
import { ANNOTATOR_EVENT, ERROR_TYPE, PLACEHOLDER_USER, TYPES } from '../constants';
import { ERROR_TYPE, PLACEHOLDER_USER, TYPES } from '../constants';

const FIELDS = 'item,thread,details,message,created_by,created_at,modified_at,permissions';

Expand Down Expand Up @@ -80,10 +80,7 @@ class FileVersionAPI extends API {
successHandler = (data: Data, queryParams: Params): Promise<any> => {
if (data.type === 'error' || !Array.isArray(data.entries)) {
const error = new Error(`Could not read annotations from file version with ID ${this.fileVersionId}`);
this.emit(ANNOTATOR_EVENT.error, {
reason: ERROR_TYPE.read,
error: error.toString()
});
error.name = ERROR_TYPE.read;
return Promise.reject(error);
}

Expand Down
4 changes: 2 additions & 2 deletions src/api/__tests__/API-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-unused-expressions */
import API from '../API';
import { ANNOTATOR_EVENT, ERROR_TYPE } from '../../constants';
import { ANNOTATOR_EVENT } from '../../constants';

const API_HOST = 'https://app.box.com/api';
let api;
Expand Down Expand Up @@ -49,7 +49,7 @@ describe('api/API', () => {

api.errorHandler(error);
expect(api.emit).toBeCalledWith(ANNOTATOR_EVENT.error, {
reason: ERROR_TYPE.auth,
reason: error.name,
error: error.toString()
});
});
Expand Down
17 changes: 6 additions & 11 deletions src/api/__tests__/AnnotationAPI-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-unused-expressions */
import AnnotationAPI from '../AnnotationAPI';
import { ANNOTATOR_EVENT, ERROR_TYPE } from '../../constants';
import { ERROR_TYPE } from '../../constants';

const API_HOST = 'https://app.box.com/api';
const HTTP_POST = 'POST';
Expand All @@ -24,6 +24,7 @@ describe('api/AnnotationAPI', () => {
token: 'someToken'
});
api.axios = jest.fn().mockReturnValue(promise);
api.errorHandler = jest.fn();
api.axiosSource = { token: '123abc' };
api.headers = {};
});
Expand Down Expand Up @@ -67,14 +68,11 @@ describe('api/AnnotationAPI', () => {

describe('createSuccessHandler()', () => {
it('should emit an error event', () => {
api.emit = jest.fn();
const error = new Error('Could not create annotation');
error.name = ERROR_TYPE.create;

api.createSuccessHandler({ type: 'error' });
expect(api.emit).toBeCalledWith(ANNOTATOR_EVENT.error, {
reason: ERROR_TYPE.create,
error: error.toString()
});
expect(api.errorHandler).toBeCalledWith(error);
});

it('should return the created annotation', () => {
Expand All @@ -87,14 +85,11 @@ describe('api/AnnotationAPI', () => {

describe('deleteSuccessHandler()', () => {
it('should emit an error event', () => {
api.emit = jest.fn();
const error = new Error('Could not delete annotation with ID 123');
error.name === ERROR_TYPE.delete;

api.deleteSuccessHandler({ type: 'error' }, '123');
expect(api.emit).toBeCalledWith(ANNOTATOR_EVENT.error, {
reason: ERROR_TYPE.delete,
error: error.toString()
});
expect(api.errorHandler).toBeCalledWith(error);
});

it('should return the deleted annotation', () => {
Expand Down
7 changes: 2 additions & 5 deletions src/api/__tests__/FileVersionAPI-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-unused-expressions */
import FileVersionAPI from '../FileVersionAPI';
import { ANNOTATOR_EVENT, ERROR_TYPE } from '../../constants';
import { ERROR_TYPE } from '../../constants';

const API_HOST = 'https://app.box.com/api';

Expand Down Expand Up @@ -79,13 +79,10 @@ describe('api/FileVersionAPI', () => {
api.fileVersionId = 123;
api.successHandler({ type: 'error' }).catch(() => {
const error = new Error(`Could not read annotations from file version with ID ${api.fileVersionId}`);
error.name = ERROR_TYPE.read;

expect(api.data.entries.length).toEqual(1);
expect(api.fetchFromMarker).not.toBeCalled();
expect(api.emit).toBeCalledWith(ANNOTATOR_EVENT.error, {
reason: ERROR_TYPE.read,
error: error.toString()
});
});
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ export const CREATE_EVENT = {
};

export const ERROR_TYPE = {
auth: 'authorization',
read: 'read',
create: 'create',
delete: 'delete'
auth: 'AuthorizationError',
read: 'ReadError',
create: 'CreateError',
delete: 'DeleteError'
};

export const POINT_ANNOTATION_ICON_HEIGHT = 31;
Expand Down

0 comments on commit bbc73e0

Please sign in to comment.