Skip to content

Commit

Permalink
Merge pull request #9 from jaumeMR/jaumeMR-try-catch
Browse files Browse the repository at this point in the history
Try/catch for JSON.parse
  • Loading branch information
apexdodge authored Nov 15, 2020
2 parents 407e25d + f1535de commit 0744a5b
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,41 @@ module.exports = class Api2Pdf {
return this._makeRequest(API2PDF_LIBREOFFICE_CONVERT, payload);
}

delete(responseId) {
delete(responseId) {
var endpoint = API2PDF_BASE_ENDPOINT + `/pdf/${responseId}`;
return new Promise((resolve, reject) => {
request.delete(endpoint, { headers: { Authorization: this.apiKey } }, function(e, r, body) {
var result = JSON.parse(body);
if (r.statusCode == 200 && result.success == true) {
return resolve(result);
}
else {
return reject(result);
try {
var result = JSON.parse(body);
if (r.statusCode == 200 && result.success == true) {
return resolve(result);
}
else {
return reject(result);
}
}
catch (e) {
return reject(result)
};
});
});
}

_makeRequest(endpoint, payload) {
return new Promise((resolve, reject) => {
request.post({ url: endpoint, body: JSON.stringify(payload), headers: { Authorization: this.apiKey } }, function(e, r, body) {
var result = JSON.parse(body);
if (r.statusCode == 200 && result.success == true) {
return resolve(result);
}
else {
return reject(result);
try {
var result = JSON.parse(body);
if (r.statusCode == 200 && result.success == true) {
return resolve(result);
}
else {
return reject(result);
}
}
catch (e) {
return reject(result)
};
});
});
}
Expand Down

0 comments on commit 0744a5b

Please sign in to comment.