Skip to content

Commit d3a6316

Browse files
committed
error handling
1 parent a96e084 commit d3a6316

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

components/pdf4me/common/utils.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "fs";
2+
import { ConfigurationError } from "@pipedream/platform";
23

34
function normalizeFilePath(path) {
45
return path.startsWith("/tmp/")
@@ -24,8 +25,22 @@ function downloadToTmp(response, filename) {
2425
];
2526
}
2627

28+
function handleErrorMessage(error) {
29+
let errorMessage = error.name;
30+
if (error.response && error.response.data) {
31+
const text = Buffer.from(error.response.data).toString("utf-8");
32+
try {
33+
errorMessage = JSON.stringify(JSON.parse(text));
34+
} catch (parseErr) {
35+
errorMessage = text;
36+
}
37+
}
38+
throw new ConfigurationError(`${errorMessage}`);
39+
}
40+
2741
export default {
2842
normalizeFilePath,
2943
checkForExtension,
3044
downloadToTmp,
45+
handleErrorMessage,
3146
};

components/pdf4me/pdf4me.app.mjs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { axios } from "@pipedream/platform";
2+
import utils from "./common/utils.mjs";
23

34
export default {
45
type: "app",
@@ -19,18 +20,22 @@ export default {
1920
_baseUrl() {
2021
return "https://api.pdf4me.com/api/v2";
2122
},
22-
_makeRequest({
23+
async _makeRequest({
2324
$ = this,
2425
path = "/",
2526
...otherOpts
2627
}) {
27-
return axios($, {
28-
...otherOpts,
29-
url: `${this._baseUrl()}${path}`,
30-
headers: {
31-
authorization: this.$auth.api_key,
32-
},
33-
});
28+
try {
29+
return await axios($, {
30+
...otherOpts,
31+
url: `${this._baseUrl()}${path}`,
32+
headers: {
33+
authorization: this.$auth.api_key,
34+
},
35+
});
36+
} catch (error) {
37+
utils.handleErrorMessage(error);
38+
}
3439
},
3540
convertToPdf(opts = {}) {
3641
return this._makeRequest({

0 commit comments

Comments
 (0)