Skip to content

Commit

Permalink
chore(nginx): improve error handling
Browse files Browse the repository at this point in the history
refs #587
  • Loading branch information
aileen authored and acburdine committed Mar 26, 2018
1 parent 8b2ecb7 commit 976bfcb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
19 changes: 15 additions & 4 deletions extensions/nginx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ class NginxExtension extends cli.Extension {
'/etc/nginx/sites-available'
).then(() => {
return this.ui.sudo(`ln -sf /etc/nginx/sites-available/${confFile} /etc/nginx/sites-enabled/${confFile}`);
}).then(() => this.restartNginx());
}).then(() => this.restartNginx()).catch((error) => {
return Promise.reject(new cli.errors.ProcessError(error));
});
}

setupSSL(argv, ctx, task) {
Expand Down Expand Up @@ -115,7 +117,10 @@ class NginxExtension extends cli.Extension {
return Promise.fromNode(cb => dns.lookup(parsedUrl.hostname, {family: 4}, cb)).catch((error) => {
if (error.code !== 'ENOTFOUND') {
// Some other error
return Promise.reject(error);
return Promise.reject(new cli.errors.CliError({
message: `Error trying to looking DNS for '${parsedUrl.hostname}'`,
err: error
}));
}

// DNS entry has not populated yet, log an error and skip rest of the
Expand Down Expand Up @@ -222,7 +227,10 @@ class NginxExtension extends cli.Extension {
this.ui.sudo(`rm -f /etc/nginx/sites-available/${confFile}`),
this.ui.sudo(`rm -f /etc/nginx/sites-enabled/${confFile}`)
]).catch(
() => Promise.reject(new cli.errors.SystemError('Nginx config file link could not be removed, you will need to do this manually.'))
(error) => Promise.reject(new cli.errors.CliError({
message: `Nginx config file link could not be removed, you will need to do this manually for ${confFile}.`,
err: error
}))
)
);
}
Expand All @@ -234,7 +242,10 @@ class NginxExtension extends cli.Extension {
this.ui.sudo(`rm -f /etc/nginx/sites-available/${sslConfFile}`),
this.ui.sudo(`rm -f /etc/nginx/sites-enabled/${sslConfFile}`)
]).catch(
() => Promise.reject(new cli.errors.SystemError('SSL config file link could not be removed, you will need to do this manually.'))
(error) => Promise.reject(new cli.errors.CliError({
message: `SSL config file link could not be removed, you will need to do this manually for ${sslConfFile}.`,
err: error
}))
)
);
}
Expand Down
40 changes: 36 additions & 4 deletions extensions/nginx/test/extension-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
const modulePath = '../index';
const Promise = require('bluebird');
const errors = require('../../../lib/errors');

const NGINX = require(modulePath);
const testURL = 'http://ghost.dev';
Expand Down Expand Up @@ -220,6 +221,36 @@ describe('Unit: Extensions > Nginx', function () {
});
});
});

it('returns a ProcessError when symlink command fails', function () {
const name = 'ghost.dev.conf';
const lnExp = new RegExp(`(?=^ln -sf)(?=.*available/${name})(?=.*enabled/${name}$)`);
ctx.instance.dir = dir;
ctx.instance.template = sinon.stub().resolves();
const loadStub = sinon.stub().returns('nginx config file');
const templateStub = sinon.stub().returns(loadStub);
const ext = proxyNginx({
'fs-extra': {
existsSync: () => false,
readFileSync: () => 'hello'
},
'lodash/template': templateStub
});
const sudo = sinon.stub().rejects({stderr: 'oops'})
ext.ui.sudo = sudo;

return ext.setupNginx(null, ctx, task).then(() => {
expect(false, 'Promise should have rejected').to.be.true
}).catch((error) => {
expect(error).to.exist;
expect(error).to.be.an.instanceof(errors.ProcessError);
expect(error.options.stderr).to.be.equal('oops');
expect(templateStub.calledOnce).to.be.true;
expect(loadStub.calledOnce).to.be.true;
expect(sudo.calledOnce).to.be.true;
expect(sudo.args[0][0]).to.match(lnExp);
});
});
});

describe('setupSSL', function () {
Expand Down Expand Up @@ -358,11 +389,12 @@ describe('Unit: Extensions > Nginx', function () {
firstSet = true;
return tasks[0].task(ctx)
}).then(() => {
expect(false, 'Promise should have rejected').to.be.true
expect(false, 'Promise should have rejected').to.be.true;
}).catch((err) => {
expect(firstSet, `ENOTFOUND Failed: ${err}`).to.be.true;
expect(err).to.exist;
expect(err.code).to.equal(DNS.code);
expect(err.options.message).to.match(/Error trying to looking DNS for 'ghost.dev'/);
expect(err.options.err.code).to.equal(DNS.code);
expect(log.called).to.be.false;
expect(ctx.dnsfail).to.not.exist;
});
Expand Down Expand Up @@ -622,6 +654,7 @@ describe('Unit: Extensions > Nginx', function () {
expect(false, 'A rejection should have happened').to.be.true;
}).catch((error) => {
sinon.assert.callCount(ext.ui.sudo, 4);
expect(error).to.be.an.instanceof(errors.CliError);
expect(error.message).to.match(/Nginx config file/);
expect(ext.restartNginx.calledOnce).to.be.false;
});
Expand Down Expand Up @@ -654,8 +687,7 @@ describe('Unit: Extensions > Nginx', function () {
expect(sudo.calledOnce).to.be.true;
expect(sudo.args[0][0]).to.match(/nginx -s reload/);
expect(err).to.be.ok;
const expectedError = require('../../../lib/errors');
expect(err).to.be.instanceof(expectedError.ProcessError);
expect(err).to.be.instanceof(errors.ProcessError);
});
});
});
Expand Down

0 comments on commit 976bfcb

Please sign in to comment.