Skip to content

Commit b7bdc59

Browse files
committed
Checking for 204 response status when deleting cloud server
1 parent 2a664a7 commit b7bdc59

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/cloud/digitalocean/provisioner.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ class Provisioner {
114114

115115
/**
116116
* Destroys cloud server; assumes OAuth has already been completed
117-
* @todo DELETE request does not return a response body so we
118-
* need to check that the response header indicates success (204)
119117
* @param {String} droplet name, as a string
120118
* @return {Promise.<void>}
121119
*/
@@ -134,8 +132,12 @@ class Provisioner {
134132
'message': 'Droplet ' + name + ' doesnt exist'
135133
});
136134
}).then((resp: any) => {
137-
this.doRequest_('DELETE', 'droplets/' + resp.droplet.id);
138-
return Promise.resolve<void>();
135+
return this.doRequest_('DELETE', 'droplets/' + resp.droplet.id);
136+
}).then((resp: any) => {
137+
if (resp.status.startsWith('204')) {
138+
return Promise.resolve<void>();
139+
}
140+
return Promise.reject(new Error('error deleting droplet'));
139141
});
140142
}
141143

@@ -275,13 +277,21 @@ class Provisioner {
275277
var url = 'https://api.digitalocean.com/v2/' + actionPath;
276278
var xhr = freedom['core.xhr']();
277279
xhr.on('onload', (loadInfo: any) => {
278-
xhr.getResponseText().then((response: string) => {
279-
try {
280-
F(JSON.parse(response));
281-
} catch(e) {
282-
R(e);
283-
}
284-
});
280+
// DELETE method doesn't return a reponse body. Success
281+
// is indicated by 204 response code in header.
282+
if (method === 'DELETE') {
283+
xhr.getResponseHeader('status').then((response: string) => {
284+
F({'status': response});
285+
});
286+
} else {
287+
xhr.getResponseText().then((response: string) => {
288+
try {
289+
F(JSON.parse(response));
290+
} catch (e) {
291+
R(e);
292+
}
293+
});
294+
}
285295
});
286296
xhr.on('onerror', R);
287297
xhr.on('ontimeout', R);

0 commit comments

Comments
 (0)