Skip to content

Commit

Permalink
Standardize error-handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Grosso committed Aug 31, 2017
1 parent e18ce5c commit 387cf6f
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 55 deletions.
3 changes: 1 addition & 2 deletions src/commands/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 5 additions & 6 deletions src/commands/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
12 changes: 5 additions & 7 deletions src/commands/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ const generateFetchStats = (raw) => {
};

function fetch(repo, options) {
const response = generateResponse();

const args = [];
const {
remoteName,
Expand All @@ -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;
7 changes: 4 additions & 3 deletions src/commands/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/pointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 3 additions & 6 deletions src/commands/prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
12 changes: 5 additions & 7 deletions src/commands/pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ const generatePullStats = (raw) => {
};

function pull(repo, options) {
const response = generateResponse();

const args = [];
const {
remoteName,
Expand All @@ -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;
12 changes: 5 additions & 7 deletions src/commands/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ const generatePushStats = (raw) => {
};

function push(repo, options) {
const response = generateResponse();

const {
remoteName,
branchName,
Expand Down Expand Up @@ -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;
4 changes: 2 additions & 2 deletions src/commands/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/untrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 4 additions & 6 deletions src/commands/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
13 changes: 8 additions & 5 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 387cf6f

Please sign in to comment.