Commit 5360a7c1 authored by Paul Slaughter's avatar Paul Slaughter

Introduce FE lib/logger for errors

- This way we can encapsulate our calls to console.error
- We can also do some smart Sentry stuff through this
  function in the future
parent 81826be8
import Api from '~/api';
import createFlash from '~/flash';
import { logError } from '~/lib/logger';
import { __ } from '~/locale';
import * as types from './mutation_types';
......@@ -63,8 +64,7 @@ export const deleteFreezePeriod = ({ state, commit }, { id }) => {
});
commit(types.RECEIVE_DELETE_FREEZE_PERIOD_ERROR, id);
// eslint-disable-next-line no-console
console.error('[gitlab] Unable to delete deploy freeze:', e);
logError(`Unable to delete deploy freeze`, e);
});
};
......
/* eslint-disable no-console */
export const LOG_PREFIX = '[gitlab]';
export const logError = (message = '', ...args) => {
console.error(LOG_PREFIX, `${message}\n`, ...args);
};
......@@ -5,6 +5,7 @@ import * as actions from '~/deploy_freeze/store/actions';
import * as types from '~/deploy_freeze/store/mutation_types';
import getInitialState from '~/deploy_freeze/store/state';
import createFlash from '~/flash';
import * as logger from '~/lib/logger';
import axios from '~/lib/utils/axios_utils';
import { freezePeriodsFixture, timezoneDataFixture } from '../helpers';
......@@ -218,7 +219,7 @@ describe('deploy freeze store actions', () => {
});
it('should show flash error and set error in state on delete failure', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation();
jest.spyOn(logger, 'logError').mockImplementation();
const error = new Error();
Api.deleteFreezePeriod.mockRejectedValue(error);
......@@ -234,7 +235,7 @@ describe('deploy freeze store actions', () => {
() => {
expect(createFlash).toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalledWith('[gitlab] Unable to delete deploy freeze:', error);
expect(logger.logError).toHaveBeenCalledWith('Unable to delete deploy freeze', error);
},
);
});
......
import { logError, LOG_PREFIX } from '~/lib/logger';
describe('~/lib/logger', () => {
let consoleErrorSpy;
beforeEach(() => {
consoleErrorSpy = jest.spyOn(console, 'error');
consoleErrorSpy.mockImplementation();
});
describe('logError', () => {
it('sends given message to console.error', () => {
const message = 'Lorem ipsum dolar sit amit';
const error = new Error('lorem ipsum');
expect(consoleErrorSpy).not.toHaveBeenCalled();
logError(message, error);
expect(consoleErrorSpy).toHaveBeenCalledWith(LOG_PREFIX, `${message}\n`, error);
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment