Skip to content

Commit

Permalink
Add special error messages for secret required
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Nov 20, 2019
1 parent 516cd80 commit 44f013a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 8 deletions.
84 changes: 77 additions & 7 deletions src/cmds/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,78 @@ const ora = require('ora');
const version = require('../../package.json').version;
const log = require('../log');
const messages = require('../messages');
const { stripIndent } = require('common-tags');

const indent = (text, depth = 2) => {
return text
.split('\n')
.map(line => `${' '.repeat(depth)}${line}`)
.join('\n');
};

const printErrors = errors => {
console.log('');
errors.forEach(printError);
console.log('');
};

const printError = (error, idx) => {
const [title, body] = errorMessage(error);
console.error(` ${`${idx + 1})`} ${title}`);

if (body) {
console.error('');
console.error(indent(body, 7));
console.error('');
}
};

const errorMessage = error => {
switch (error.code) {
case 'SECRET_REQUIRED':
// prettier-ignore
var title = `The secret ${log.variable('@' + error.properties.key)} is not defined yet ${chalk.gray(`(${error.field})`)}`;
let body;

switch (error.properties.app) {
case 'mailchimp':
// prettier-ignore
body = stripIndent`
Here's where to find your API key:
• Log in to your Mailchimp account
• Open the menu with your avatar in the upper right
• Navigate to ${chalk.bold.cyan('Profile › Extras › API keys')}
• Under "Your API keys", click ${chalk.bold.cyan('Create A Key')}
Copy the generated key and run the following command:
${chalk.gray('$')} statickit secrets add ${chalk.cyan(error.properties.key)} ${chalk.yellow('<paste-api-key-here>')}
Then, try deploying again.
`;

break;

default:
// prettier-ignore
body = stripIndent`
Run the following command to add this secret:
${chalk.gray('$')} statickit secrets add ${chalk.cyan(error.properties.key)} ${chalk.yellow('<enter-the-value-here>')}
Then, try deploying again.
`;
break;
}

return [title, body];

default:
var title = `${chalk.cyan(error.field)} ${error.message}`;
return [title, ''];
}
};

exports.command = 'deploy';
exports.describe = 'Deploys statickit.json';
Expand Down Expand Up @@ -76,8 +148,9 @@ exports.handler = async args => {

switch (response.status) {
case 200:
log.success('Deployment succeeded');
log.meta(`id: ${response.data.id}`);
log.success(
`Deployment succeeded ${chalk.gray(`(${response.data.id})`)}`
);
return;

case 401:
Expand All @@ -86,11 +159,8 @@ exports.handler = async args => {
return;

case 422:
log.error('Deployment failed due to configuration errors');
console.log('');
console.table(response.data.errors);
console.log('');
log.meta(`id: ${response.data.id}`);
log.error(`Deployment failed ${chalk.gray(`(${response.data.id})`)}`);
printErrors(response.data.errors);
process.exitCode = 1;
return;

Expand Down
2 changes: 1 addition & 1 deletion src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const secretAlreadyExists = (name, value, key) => {
};

const secretKeyInvalid = () => {
log.error('Name is invalid');
log.error('Secret name is invalid');
console.error('');

// prettier-ignore
Expand Down

0 comments on commit 44f013a

Please sign in to comment.