diff --git a/src/eventlog.js b/src/eventlog.js index e41d7cebe..81ac4791e 100644 --- a/src/eventlog.js +++ b/src/eventlog.js @@ -101,14 +101,9 @@ async function add(action, source, data) { assert.strictEqual(typeof data, 'object'); const id = uuid.v4(); - try { - await database.query('INSERT INTO eventlog (id, action, source, data) VALUES (?, ?, ?, ?)', [ id, action, JSON.stringify(source), JSON.stringify(data) ]); - await notifications.onEvent(id, action, source, data); - return id; - } catch (error) { - debug('add: error adding event', error); - return null; - } + await database.query('INSERT INTO eventlog (id, action, source, data) VALUES (?, ?, ?, ?)', [ id, action, JSON.stringify(source), JSON.stringify(data) ]); + await notifications.onEvent(id, action, source, data); + return id; } // never throws, only logs because previously code did not take a callback @@ -126,16 +121,11 @@ async function upsertLoginEvent(action, source, data) { args: [ action, JSON.stringify(source) ] }]; - try { - const result = await database.transaction(queries); - if (result[0].affectedRows >= 1) return result[1][0].id; + const result = await database.transaction(queries); + if (result[0].affectedRows >= 1) return result[1][0].id; - // no existing eventlog found, create one - return await add(action, source, data); - } catch (error) { - debug('add: error adding event', error); - return null; - } + // no existing eventlog found, create one + return await add(action, source, data); } async function get(id) { diff --git a/src/volumes.js b/src/volumes.js index c2c0cff62..c1b63fa01 100644 --- a/src/volumes.js +++ b/src/volumes.js @@ -92,14 +92,11 @@ async function add(volume, auditSource) { await mounts.tryAddMount(volume, { timeout: 10 }); // 10 seconds } - try { - await database.query('INSERT INTO volumes (id, name, hostPath, mountType, mountOptionsJson) VALUES (?, ?, ?, ?, ?)', [ id, name, volume.hostPath, mountType, JSON.stringify(mountOptions) ]); - } catch (error) { - if (error.code === 'ER_DUP_ENTRY' && error.sqlMessage.indexOf('name') !== -1) throw new BoxError(BoxError.ALREADY_EXISTS, 'name already exists'); - if (error.code === 'ER_DUP_ENTRY' && error.sqlMessage.indexOf('hostPath') !== -1) throw new BoxError(BoxError.ALREADY_EXISTS, 'hostPath already exists'); - if (error.code === 'ER_DUP_ENTRY' && error.sqlMessage.indexOf('PRIMARY') !== -1) throw new BoxError(BoxError.ALREADY_EXISTS, 'id already exists'); - throw error; - } + [error] = await safe(database.query('INSERT INTO volumes (id, name, hostPath, mountType, mountOptionsJson) VALUES (?, ?, ?, ?, ?)', [ id, name, volume.hostPath, mountType, JSON.stringify(mountOptions) ])); + if (error && error.code === 'ER_DUP_ENTRY' && error.sqlMessage.indexOf('name') !== -1) throw new BoxError(BoxError.ALREADY_EXISTS, 'name already exists'); + if (error && error.code === 'ER_DUP_ENTRY' && error.sqlMessage.indexOf('hostPath') !== -1) throw new BoxError(BoxError.ALREADY_EXISTS, 'hostPath already exists'); + if (error && error.code === 'ER_DUP_ENTRY' && error.sqlMessage.indexOf('PRIMARY') !== -1) throw new BoxError(BoxError.ALREADY_EXISTS, 'id already exists'); + if (error) throw error; eventlog.add(eventlog.ACTION_VOLUME_ADD, auditSource, { id, name, hostPath: volume.hostPath }); // in theory, we only need to do this mountpoint volumes. but for some reason a restart is required to detect new "mounts"