From 387cf6fb308c7729ec6c6afcf2115512ebc0be78 Mon Sep 17 00:00:00 2001 From: Joshua Grosso Date: Thu, 31 Aug 2017 10:29:02 -0700 Subject: [PATCH] Standardize error-handling logic --- src/commands/checkout.js | 3 +-- src/commands/clone.js | 11 +++++------ src/commands/fetch.js | 12 +++++------- src/commands/ls.js | 7 ++++--- src/commands/pointer.js | 4 ++-- src/commands/prune.js | 9 +++------ src/commands/pull.js | 12 +++++------- src/commands/push.js | 12 +++++------- src/commands/track.js | 4 ++-- src/commands/untrack.js | 4 ++-- src/commands/version.js | 10 ++++------ src/helpers.js | 13 ++++++++----- 12 files changed, 46 insertions(+), 55 deletions(-) diff --git a/src/commands/checkout.js b/src/commands/checkout.js index f2783f4..339d0b8 100644 --- a/src/commands/checkout.js +++ b/src/commands/checkout.js @@ -55,10 +55,9 @@ const generateCheckoutStats = (raw) => { }; function checkout(repo) { - const response = generateResponse(); - return core.checkout('', { cwd: repo.workdir() }) .then(({ stdout, stderr }) => { + const response = generateResponse(); response.raw = stdout; if (stderr) { diff --git a/src/commands/clone.js b/src/commands/clone.js index 72fb81b..ba428ed 100644 --- a/src/commands/clone.js +++ b/src/commands/clone.js @@ -70,13 +70,12 @@ function clone(url, cwd, options) { } = (options || {}); const args = branch ? `-b ${branch}` : ''; - const response = generateResponse(); return core.clone(`${url} ${args}`, { cwd, env }, callback) - .then(({ stdout }) => { - response.raw = stdout; - response.clone = generateCloneStats(stdout); - return response; - }, errorCatchHandler(response)); + .then(({ stdout }) => ({ + ...generateResponse(), + raw: stdout, + clone: generateCloneStats(stdout) + }), errorCatchHandler); } export default clone; diff --git a/src/commands/fetch.js b/src/commands/fetch.js index 1f487e2..9b822d6 100644 --- a/src/commands/fetch.js +++ b/src/commands/fetch.js @@ -59,8 +59,6 @@ const generateFetchStats = (raw) => { }; function fetch(repo, options) { - const response = generateResponse(); - const args = []; const { remoteName, @@ -78,11 +76,11 @@ function fetch(repo, options) { const argsString = R.join(' ', args); return core.fetch(argsString, { cwd: repo.workdir(), shell: true }, callback) - .then(({ stdout }) => { - response.raw = stdout; - response.fetch = generateFetchStats(stdout); - return response; - }, errorCatchHandler(response)); + .then(({ stdout }) => ({ + ...generateResponse(), + fetch: generateFetchStats(stdout), + raw: stdout + }), errorCatchHandler); } export default fetch; diff --git a/src/commands/ls.js b/src/commands/ls.js index e66ae0c..f8ed839 100644 --- a/src/commands/ls.js +++ b/src/commands/ls.js @@ -47,17 +47,18 @@ const buildArgs = (options) => { }; const ls = (repo, options) => { - const response = generateResponse(); const args = buildArgs(options); return core.ls(args, { cwd: repo.workdir() }) .then(({ stdout, stderr }) => { + const response = generateResponse(); response.raw = stdout; - if (stderr.length > 0) { + if (stderr) { + response.errno = BAD_CORE_RESPONSE; response.stderr = stderr; response.success = false; - response.errno = BAD_CORE_RESPONSE; + return response; } response.files = extractFileNames(stdout); diff --git a/src/commands/pointer.js b/src/commands/pointer.js index 28849d9..e75754d 100644 --- a/src/commands/pointer.js +++ b/src/commands/pointer.js @@ -13,16 +13,16 @@ const pointer = (repo, filePath, pointerPath) => { args += `--file=${pointerPath} `; } - const response = generateResponse(); return core.pointer(args, { cwd: repo.workdir() }) .then(({ stdout, stderr }) => { + const response = generateResponse(); response.raw = stdout; if (stderr) { - response.success = false; response.errno = BAD_CORE_RESPONSE; response.stderr = stderr; + response.success = false; return response; } diff --git a/src/commands/prune.js b/src/commands/prune.js index ad28e02..f7d772f 100644 --- a/src/commands/prune.js +++ b/src/commands/prune.js @@ -2,22 +2,19 @@ import { core } from './lfsCommands'; import generateResponse from '../utils/generateResponse'; import { BAD_CORE_RESPONSE } from '../constants'; -const prune = (repo, args) => { - const response = generateResponse(); - - return core.prune(args, { cwd: repo.workdir() }) +const prune = (repo, args) => + core.prune(args, { cwd: repo.workdir() }) .then(({ stdout, stderr }) => { + const response = generateResponse(); response.raw = stdout; if (stderr) { response.errno = BAD_CORE_RESPONSE; response.stderr = stderr; response.success = false; - return response; } return response; }); -}; export default prune; diff --git a/src/commands/pull.js b/src/commands/pull.js index 90d2176..a818d7d 100644 --- a/src/commands/pull.js +++ b/src/commands/pull.js @@ -59,8 +59,6 @@ const generatePullStats = (raw) => { }; function pull(repo, options) { - const response = generateResponse(); - const args = []; const { remoteName, @@ -77,11 +75,11 @@ function pull(repo, options) { const argsString = R.join(' ', args); return core.pull(argsString, { cwd: repo.workdir(), shell: true }, callback) - .then(({ stdout }) => { - response.raw = stdout; - response.pull = generatePullStats(stdout); - return response; - }, errorCatchHandler(response)); + .then(({ stdout }) => ({ + ...generateResponse(), + pull: generatePullStats(stdout), + raw: stdout + }), errorCatchHandler); } export default pull; diff --git a/src/commands/push.js b/src/commands/push.js index e3b75ce..26fceed 100644 --- a/src/commands/push.js +++ b/src/commands/push.js @@ -62,8 +62,6 @@ const generatePushStats = (raw) => { }; function push(repo, options) { - const response = generateResponse(); - const { remoteName, branchName, @@ -96,11 +94,11 @@ function push(repo, options) { return getRemoteAndBranchPromise .then(() => core.push(`${remote} ${branch}`, { cwd: repo.path() }, callback)) - .then(({ stdout }) => { - response.raw = stdout; - response.push = generatePushStats(stdout); - return response; - }, errorCatchHandler(response)); + .then(({ stdout }) => ({ + ...generateResponse(), + push: generatePushStats(stdout), + raw: stdout + }), errorCatchHandler); } export default push; diff --git a/src/commands/track.js b/src/commands/track.js index 36ae9c3..f4f0fc6 100644 --- a/src/commands/track.js +++ b/src/commands/track.js @@ -22,16 +22,16 @@ const track = (repo, globs) => { R.filter(isString), R.map(g => `${ticks}${g}${ticks}`) )(globs); - const response = generateResponse(); return core.track(R.join(' ', filteredGlobs), { cwd: repo.workdir() }) .then(({ stdout, stderr }) => { + const response = generateResponse(); response.raw = stdout; if (stderr) { - response.success = false; response.errno = BAD_CORE_RESPONSE; response.stderr = stderr; + response.success = false; return response; } diff --git a/src/commands/untrack.js b/src/commands/untrack.js index b4b4f04..a87b3b1 100644 --- a/src/commands/untrack.js +++ b/src/commands/untrack.js @@ -22,16 +22,16 @@ const untrack = (repo, globs) => { R.filter(isString), R.map(g => `${ticks}${g}${ticks}`) )(globs); - const response = generateResponse(); return core.untrack(R.join(' ', filteredGlobs), { cwd: repo.workdir() }) .then(({ stdout, stderr }) => { + const response = generateResponse(); response.raw = stdout; if (stderr) { - response.success = false; response.errno = BAD_CORE_RESPONSE; response.stderr = stderr; + response.success = false; return response; } diff --git a/src/commands/version.js b/src/commands/version.js index f10e3c8..78cb0a6 100644 --- a/src/commands/version.js +++ b/src/commands/version.js @@ -7,22 +7,20 @@ import { import generateResponse from '../utils/generateResponse'; const version = () => { - const response = generateResponse(); return core.version() .then(({ stdout, stderr }) => { + const response = generateResponse(); response.raw = stdout; if (stderr) { + response.errno = BAD_CORE_RESPONSE; response.stderr = stderr; - response.raw = stderr; response.success = false; - response.errno = BAD_CORE_RESPONSE; - } else { - response.version = parseVersion(stdout, regex.LFS); + return response; } + response.version = parseVersion(stdout, regex.LFS); return response; }); -}; export default version; diff --git a/src/helpers.js b/src/helpers.js index 2e48189..58f1a84 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -7,6 +7,7 @@ import { BAD_CORE_RESPONSE, BAD_REGEX_PARSE_RESULT } from './constants'; +import generateResponse from './utils/generateResponse'; export const getGitattributesPathFromRepo = repo => path.join(repo.workdir(), '.gitattributes'); @@ -66,13 +67,15 @@ export const verifyOutput = (stats, raw) => { } }; -export const errorCatchHandler = response => (code) => { +export const errorCatchHandler = (code) => { // This is a manually detected error we get from LFS if (code === BAD_CORE_RESPONSE) { - response.stderr = response.raw; - response.errno = BAD_CORE_RESPONSE; - response.success = false; - return response; + return { + ...generateResponse(), + errno: BAD_CORE_RESPONSE, + stderr: '', + success: false + }; } throw code;