Wrap request promise in try catch

This commit is contained in:
Thomas Hu
2020-07-17 11:57:18 -04:00
parent e16d515d3b
commit 8ddda09c14
5 changed files with 217 additions and 205 deletions

View File

@@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2019 Codecov Copyright (c) 2019-2020 Codecov
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

208
dist/index.js vendored
View File

@@ -2236,121 +2236,127 @@ try {
url: "https://codecov.io/bash", url: "https://codecov.io/bash",
json: false json: false
}, (error, response, body) => { }, (error, response, body) => {
if (error && fail_ci) { try {
throw error; if (error && fail_ci) {
} else if (error) { throw error;
core.warning(`Codecov warning: ${error.message}`); } else if (error) {
} core.warning(`Codecov warning: ${error.message}`);
fs.writeFile("codecov.sh", body, err => {
if (err && fail_ci) {
throw err;
} else if (err) {
core.warning(`Codecov warning: ${err.message}`);
} }
let output = ""; fs.writeFile("codecov.sh", body, err => {
let execError = ""; if (err && fail_ci) {
const options = {}; throw err;
options.listeners = { } else if (err) {
stdout: data => { core.warning(`Codecov warning: ${err.message}`);
output += data.toString();
},
stderr: data => {
execError += data.toString();
} }
};
options.env = Object.assign(process.env, { let output = "";
GITHUB_ACTION: process.env.GITHUB_ACTION, let execError = "";
GITHUB_RUN_ID: process.env.GITHUB_RUN_ID, const options = {};
GITHUB_REF: process.env.GITHUB_REF, options.listeners = {
GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY, stdout: data => {
GITHUB_SHA: process.env.GITHUB_SHA, output += data.toString();
GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || '' },
}); stderr: data => {
execError += data.toString();
}
};
if(token){ options.env = Object.assign(process.env, {
options.env.CODECOV_TOKEN = token GITHUB_ACTION: process.env.GITHUB_ACTION,
} GITHUB_RUN_ID: process.env.GITHUB_RUN_ID,
GITHUB_REF: process.env.GITHUB_REF,
GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY,
GITHUB_SHA: process.env.GITHUB_SHA,
GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || ''
});
const env_vars_arg = [] if(token){
for (let env_var of env_vars.split(",")) { options.env.CODECOV_TOKEN = token
let env_var_clean = env_var.trim();
if (env_var_clean) {
options.env[env_var_clean] = process.env[env_var_clean];
env_vars_arg.push(env_var_clean)
} }
}
const execArgs = ["codecov.sh"]; const env_vars_arg = []
if (file) { for (let env_var of env_vars.split(",")) {
execArgs.push( let env_var_clean = env_var.trim();
"-f", `${file}` if (env_var_clean) {
); options.env[env_var_clean] = process.env[env_var_clean];
} env_vars_arg.push(env_var_clean)
}
}
if (files) { const execArgs = ["codecov.sh"];
files.split(',').forEach(f => { if (file) {
execArgs.push( execArgs.push(
"-f", `${f}` "-f", `${file}`
); );
}); }
}
if (dir) { if (files) {
execArgs.push( files.split(',').forEach(f => {
"-s", `${dir}` execArgs.push(
); "-f", `${f}`
}
execArgs.push(
"-n", `${name}`,
"-F", `${flags}`
);
if (fail_ci) {
execArgs.push(
"-Z"
);
}
if (env_vars_arg.length) {
execArgs.push(
"-e", env_vars_arg.join(",")
);
}
if (write_path) {
execArgs.push(
"-q", `${write_path}`
);
}
exec.exec("bash", execArgs, options)
.catch(err => {
if (fail_ci) {
core.setFailed(
`Codecov failed with the following error: ${err.message}`
); );
} else { });
core.warning(`Codecov warning: ${err.message}`); }
}
})
.then(() => {
unlinkFile();
});
const unlinkFile = () => { if (dir) {
fs.unlink("codecov.sh", err => { execArgs.push(
if (err && fail_ci) { "-s", `${dir}`
throw err; );
} else if (err) { }
core.warning(`Codecov warning: ${err.message}`);
} execArgs.push(
}); "-n", `${name}`,
}; "-F", `${flags}`
}); );
if (fail_ci) {
execArgs.push(
"-Z"
);
}
if (env_vars_arg.length) {
execArgs.push(
"-e", env_vars_arg.join(",")
);
}
if (write_path) {
execArgs.push(
"-q", `${write_path}`
);
}
exec.exec("bash", execArgs, options)
.catch(err => {
if (fail_ci) {
core.setFailed(
`Codecov failed with the following error: ${err.message}`
);
} else {
core.warning(`Codecov warning: ${err.message}`);
}
})
.then(() => {
unlinkFile();
});
const unlinkFile = () => {
fs.unlink("codecov.sh", err => {
if (err && fail_ci) {
throw err;
} else if (err) {
core.warning(`Codecov warning: ${err.message}`);
}
});
};
});
} catch (error) {
core.setFailed(
`Codecov failed with the following error: ${error.message}`
);
}
}); });
} catch (error) { } catch (error) {
if (fail_ci) { if (fail_ci) {

208
index.js
View File

@@ -32,121 +32,127 @@ try {
url: "https://codecov.io/bash", url: "https://codecov.io/bash",
json: false json: false
}, (error, response, body) => { }, (error, response, body) => {
if (error && fail_ci) { try {
throw error; if (error && fail_ci) {
} else if (error) { throw error;
core.warning(`Codecov warning: ${error.message}`); } else if (error) {
} core.warning(`Codecov warning: ${error.message}`);
fs.writeFile("codecov.sh", body, err => {
if (err && fail_ci) {
throw err;
} else if (err) {
core.warning(`Codecov warning: ${err.message}`);
} }
let output = ""; fs.writeFile("codecov.sh", body, err => {
let execError = ""; if (err && fail_ci) {
const options = {}; throw err;
options.listeners = { } else if (err) {
stdout: data => { core.warning(`Codecov warning: ${err.message}`);
output += data.toString();
},
stderr: data => {
execError += data.toString();
} }
};
options.env = Object.assign(process.env, { let output = "";
GITHUB_ACTION: process.env.GITHUB_ACTION, let execError = "";
GITHUB_RUN_ID: process.env.GITHUB_RUN_ID, const options = {};
GITHUB_REF: process.env.GITHUB_REF, options.listeners = {
GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY, stdout: data => {
GITHUB_SHA: process.env.GITHUB_SHA, output += data.toString();
GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || '' },
}); stderr: data => {
execError += data.toString();
}
};
if(token){ options.env = Object.assign(process.env, {
options.env.CODECOV_TOKEN = token GITHUB_ACTION: process.env.GITHUB_ACTION,
} GITHUB_RUN_ID: process.env.GITHUB_RUN_ID,
GITHUB_REF: process.env.GITHUB_REF,
GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY,
GITHUB_SHA: process.env.GITHUB_SHA,
GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || ''
});
const env_vars_arg = [] if(token){
for (let env_var of env_vars.split(",")) { options.env.CODECOV_TOKEN = token
let env_var_clean = env_var.trim();
if (env_var_clean) {
options.env[env_var_clean] = process.env[env_var_clean];
env_vars_arg.push(env_var_clean)
} }
}
const execArgs = ["codecov.sh"]; const env_vars_arg = []
if (file) { for (let env_var of env_vars.split(",")) {
execArgs.push( let env_var_clean = env_var.trim();
"-f", `${file}` if (env_var_clean) {
); options.env[env_var_clean] = process.env[env_var_clean];
} env_vars_arg.push(env_var_clean)
}
}
if (files) { const execArgs = ["codecov.sh"];
files.split(',').forEach(f => { if (file) {
execArgs.push( execArgs.push(
"-f", `${f}` "-f", `${file}`
); );
}); }
}
if (dir) { if (files) {
execArgs.push( files.split(',').forEach(f => {
"-s", `${dir}` execArgs.push(
); "-f", `${f}`
}
execArgs.push(
"-n", `${name}`,
"-F", `${flags}`
);
if (fail_ci) {
execArgs.push(
"-Z"
);
}
if (env_vars_arg.length) {
execArgs.push(
"-e", env_vars_arg.join(",")
);
}
if (write_path) {
execArgs.push(
"-q", `${write_path}`
);
}
exec.exec("bash", execArgs, options)
.catch(err => {
if (fail_ci) {
core.setFailed(
`Codecov failed with the following error: ${err.message}`
); );
} else { });
core.warning(`Codecov warning: ${err.message}`); }
}
})
.then(() => {
unlinkFile();
});
const unlinkFile = () => { if (dir) {
fs.unlink("codecov.sh", err => { execArgs.push(
if (err && fail_ci) { "-s", `${dir}`
throw err; );
} else if (err) { }
core.warning(`Codecov warning: ${err.message}`);
} execArgs.push(
}); "-n", `${name}`,
}; "-F", `${flags}`
}); );
if (fail_ci) {
execArgs.push(
"-Z"
);
}
if (env_vars_arg.length) {
execArgs.push(
"-e", env_vars_arg.join(",")
);
}
if (write_path) {
execArgs.push(
"-q", `${write_path}`
);
}
exec.exec("bash", execArgs, options)
.catch(err => {
if (fail_ci) {
core.setFailed(
`Codecov failed with the following error: ${err.message}`
);
} else {
core.warning(`Codecov warning: ${err.message}`);
}
})
.then(() => {
unlinkFile();
});
const unlinkFile = () => {
fs.unlink("codecov.sh", err => {
if (err && fail_ci) {
throw err;
} else if (err) {
core.warning(`Codecov warning: ${err.message}`);
}
});
};
});
} catch (error) {
core.setFailed(
`Codecov failed with the following error: ${error.message}`
);
}
}); });
} catch (error) { } catch (error) {
if (fail_ci) { if (fail_ci) {

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{ {
"name": "codecov-action", "name": "codecov-action",
"version": "1.0.11", "version": "1.0.12",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@@ -1,6 +1,6 @@
{ {
"name": "codecov-action", "name": "codecov-action",
"version": "1.0.11", "version": "1.0.12",
"description": "Upload coverage reports to Codecov from GitHub Actions", "description": "Upload coverage reports to Codecov from GitHub Actions",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {