Skip to content

Commit

Permalink
feat(deploy-toolbar): improve error handling
Browse files Browse the repository at this point in the history
* use endpoint url provided by client
* display user-friendly error messages
* do not throw error for response other than JSON

Closes #838
  • Loading branch information
barmac committed Dec 3, 2018
1 parent 5e84b82 commit 29a94ac
Show file tree
Hide file tree
Showing 16 changed files with 324 additions and 114 deletions.
93 changes: 56 additions & 37 deletions app/lib/createDeployer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,74 @@ function createDeployer({ fetch, fs, FormData }) {
/**
* Deploy diagram to the given endpoint URL.
*/
return function deploy(url, { deploymentName, tenantId, file = {} }, cb) {
return async function deploy(url, { deploymentName, tenantId, file = {} }, cb) {

// callback is optional
cb = cb || noop;
try {
// callback is optional
cb = cb || noop;

if (!deploymentName) {
return cb(
new Error(
'Failed to deploy process, deployment name must be provided.'
)
);
}
if (!deploymentName) {
throw new Error('Failed to deploy process, deployment name must be provided.');
}

if (!file.fileType || !file.name || !file.path) {
return cb(
new Error(
'Failed to deploy process, file name and path must be provided.'
)
);
}
if (!file.name || !file.path) {
throw new Error('Failed to deploy process, file name and path must be provided.');
}

if (!url) {
return cb(
new Error(
'Failed to deploy process, endpoint url must not be empty.'
)
);
}
if (!url) {
throw new Error('Failed to deploy process, endpoint url must not be empty.');
}

const form = new FormData();
const form = new FormData();

form.append('deployment-name', deploymentName);
form.append('deployment-name', deploymentName);

if (tenantId) {
form.append('tenant-id', tenantId);
}
if (tenantId) {
form.append('tenant-id', tenantId);
}

form.append(file.name, fs.createReadStream(file.path));

const serverResponse = await fetch(url, { method: 'POST', body: form });

if (!serverResponse.ok) {
const error = await getErrorFromResponse(serverResponse);
throw error;
}

let response;

form.append(file.name, fs.createReadStream(file.path));
try {
response = await serverResponse.json();
} catch (error) {
response = serverResponse.statusText;
}

return cb(null, response);
} catch (error) {
return cb(error);
}

fetch(url, { method: 'POST', body: form })
.then(res => res.json())
.then(json => cb(null, json))
.catch(function(error) {
cb(error);
});
};
}

module.exports = createDeployer;


function noop() { }
// helpers //////
function noop() { }


async function getErrorFromResponse(response) {
const error = new Error();

try {
const body = await response.json();
error.message = body.message;
} catch (_) {
error.code = response.status;
error.message = response.statusText;
}

return error;
}
41 changes: 16 additions & 25 deletions app/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,31 +167,7 @@ renderer.on('dialog:show', async function(options, done) {
// deploying //////////
// TODO: remove and add as plugin instead

renderer.on('deploy', function(data, done) {
var workspaceConfig = config.get('workspace', { endpoints: [] });

var endpointUrl = (workspaceConfig.endpoints || [])[0];

if (!endpointUrl) {

let err = new Error('no deploy endpoint configured');

console.error('failed to deploy', err);
return done(err.message);
}

deploy(endpointUrl, data, function(err, result) {

if (err) {
console.error('failed to deploy', err);

return done(err.message);
}

done(null, result);
});

});
renderer.on('deploy', handleDeployment);

// filesystem //////////

Expand Down Expand Up @@ -430,5 +406,20 @@ app.on('ready', function() {
});


function handleDeployment(data, done) {
const { endpointUrl } = data;

deploy(endpointUrl, data, function(error, result) {

if (error) {
console.error('failed to deploy', error);

return done(error);
}

done(null, result);
});
}

// expose app
module.exports = app;
6 changes: 4 additions & 2 deletions app/lib/util/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var electron = require('electron'),
app = electron.app;

const {
assign
assign,
pick
} = require('min-dash');


Expand All @@ -15,8 +16,9 @@ function on(event, callback, that) {

function done(...doneArgs) {
var actualArgs = doneArgs.map(function(e) {
// error.message and error.code are not enumerable
if (e instanceof Error) {
return assign({}, e);
return assign({}, pick(e, [ 'message', 'code' ]), e);
}

return e;
Expand Down
1 change: 1 addition & 0 deletions app/test/helper/mock/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var RESPONSE_OK = { mocked: true };

module.exports = function(url, options) {
return Promise.resolve({
ok: true,
json: () => Promise.resolve(RESPONSE_OK)
});
};
Expand Down
Loading

0 comments on commit 29a94ac

Please sign in to comment.