Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions services/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var redis = require('../redis'),
postgres = require('../postgres/client'),
{ isUri } = require('clayutils'),
{ noKeyError } = require('./errors'),
{ CACHE_ENABLED } = require('./constants');

/**
Expand Down Expand Up @@ -35,6 +36,8 @@ function put(key, value, testCacheEnabled) {
* @return {Promise}
*/
function get(key) {
if (!key) return Promise.reject(noKeyError());

return redis.get(key)
.then(data => {
if (isUri(key)) return data;
Expand Down
18 changes: 17 additions & 1 deletion services/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const { put, get, del, batch, createReadStream } = require('./db'),
redis = require('../redis'),
postgres = require('../postgres/client');
postgres = require('../postgres/client'),
{ noKeyError } = require('./errors');

jest.mock('../redis');
jest.mock('../postgres/client');
Expand Down Expand Up @@ -47,6 +48,21 @@ describe('services/db', () => {
expect(postgres.get).toHaveBeenCalledWith(KEY);
});
});

test('it fails when no key is being passed', () => {
let rejected = false;

redis.get.mockResolvedValue(JSON.stringify(VALUE));

return get()
.then(() => {
expect(rejected).to.be.true;
})
.catch(err => {
rejected = true;
expect(err).toEqual(noKeyError());
});
});
});

describe('put', () => {
Expand Down
14 changes: 14 additions & 0 deletions services/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ function notFoundError(key) {
return error;
}

/**
* Format an error for when the key
* is not provided
*
* @return {Error}
*/
function noKeyError() {
const error = new Error('No key provided');

error.name = 'NoKeyError';
return error;
}

/**
* Log an error
*
Expand All @@ -28,6 +41,7 @@ function logGenericError(file) {
}

module.exports.notFoundError = notFoundError;
module.exports.noKeyError = noKeyError;
module.exports.logGenericError = logGenericError;

// For testing
Expand Down