Skip to content

Commit

Permalink
feat(ssl) improve error messages for acme
Browse files Browse the repository at this point in the history
refs TryGhost#587

- fixes an issue, where failed requests with `got` wouldn't get catched properly
- differentiates between `ProcessError` for `stderr` and `CliError` for everything else
- first step on attaching original error properties to `CliError`
  • Loading branch information
aileen committed Jan 24, 2018
1 parent c25c141 commit a2051c1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
32 changes: 22 additions & 10 deletions extensions/nginx/acme.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ function install(ui, task) {
ui.logVerbose('ssl: downloading acme.sh to temporary directory', 'green');
return fs.emptyDir(acmeTmpDir)
}).then(() => got(acmeApiUrl)).then((response) => {
if (response.statusCode !== 200) {
return Promise.reject(new cli.errors.CliError('Unable to query GitHub for ACME download URL'));
}

try {
response = JSON.parse(response.body).tarball_url;
} catch (e) {
Expand All @@ -49,7 +45,17 @@ function install(ui, task) {

// Installs acme.sh into /etc/letsencrypt
return ui.sudo('./acme.sh --install --home /etc/letsencrypt', {cwd: acmeCodeDir});
}).catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
}).catch((error) => {
// catch any request errors first, which isn't a ProcessError
if (error.statusCode !== 200 && !error.stderr) {
return Promise.reject(new cli.errors.CliError({
message: 'Unable to query GitHub for ACME download URL',
err: error
}));
}

return Promise.reject(new cli.errors.ProcessError(error));
});
}

function generateCert(ui, domain, webroot, email, staging) {
Expand All @@ -64,13 +70,19 @@ function generateCert(ui, domain, webroot, email, staging) {

if (error.stderr.match(/Verify error:(Fetching|Invalid Response)/)) {
// Domain verification failed
return Promise.reject(new cli.errors.SystemError(
'Your domain name is not pointing to the correct IP address of your server, please update it and run `ghost setup ssl` again'
));
return Promise.reject(new cli.errors.SystemError({
message: 'Your domain name is not pointing to the correct IP address of your server, please update it and run `ghost setup ssl` again',
err: error
}));
} else if (error.stderr) {
// Use ProcessError only for errors with stderr
return Promise.reject(new cli.errors.ProcessError(error));
}

// It's not an error we expect might happen, throw a ProcessError instead.
return Promise.reject(new cli.errors.ProcessError(error));
return Promise.reject(new cli.errors.CliError({
message: 'Error trying to generate your SSL certificate',
err: error
}));
});
}

Expand Down
3 changes: 3 additions & 0 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class CliError extends Error {
Error.captureStackTrace(this, this.constructor);

this.context = options.context || {};
// TODO: Do something with the original error message which we can receive now as the `err` property
this.err = options.err || {};
this.options = options;

this.help = 'Please refer to https://docs.ghost.org/v1/docs/troubleshooting#section-cli-errors for troubleshooting.'
}

Expand Down

0 comments on commit a2051c1

Please sign in to comment.