Skip to content

Commit 7511b98

Browse files
committed
feat: verbose error messages
1 parent 6b52ec5 commit 7511b98

3 files changed

Lines changed: 22 additions & 13 deletions

File tree

src/methods/create.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ module.exports = async (files = [], options = {}) => {
5151
},
5252
});
5353

54-
if (!res) throw new Error('There was a error creating your bin');
54+
if (res.error)
55+
throw (console.error('There was a error creating your bin'), res.error);
5556

56-
const Bin = await get(res.key);
57+
const Bin = await get(res.data.key);
5758
if (!Bin) throw new Error('There was a error getting your bin');
5859

5960
return Bin;

src/methods/get.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,38 @@ module.exports = async (key, options = {}) => {
3030
options = Object.assign(defaultOptions, options);
3131

3232
const binData = await fetch(`https://sourceb.in/api/bins/${key}`);
33-
if (!binData) throw new Error('There was a error in fetching bin data');
33+
if (binData.error)
34+
throw (
35+
(console.error('There was a error in fetching bin data'),
36+
binData.error)
37+
);
3438

3539
if (options.fetchContent) {
36-
for (let i = 0; i < binData.files.length; i++) {
40+
for (let i = 0; i < binData.data.files.length; i++) {
3741
const content = await fetch(
3842
`https://cdn.sourceb.in/bins/${key}/${i}`,
3943
{
4044
responseType: 'text',
4145
},
4246
);
4347

44-
if (!content)
45-
throw new Error(
46-
'There was a error in fetching bin content for file ' + i,
48+
if (content.error)
49+
throw (
50+
(console.error(
51+
'There was a error in fetching bin content for file ' +
52+
i,
53+
),
54+
content.error)
4755
);
4856

49-
binData.files[i] = {
50-
content,
51-
...binData.files[i],
57+
binData.data.files[i] = {
58+
content: content.data,
59+
...binData.data.files[i],
5260
};
5361
}
5462
}
5563

56-
const Bin = new SourceBin(key, binData);
64+
const Bin = new SourceBin(key, binData.data);
5765

5866
return Bin;
5967
};

src/util/fetch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ module.exports = async (url, options = {}) => {
1919
'User-Agent': `Sourcebin/${version} https://www.npmjs.com/package/sourcebin`,
2020
},
2121
})
22-
.then((res) => res.body)
23-
.catch((res) => undefined);
22+
.then((res) => ({ data: res.body }))
23+
.catch((res) => ({ error: res }));
2424
};

0 commit comments

Comments
 (0)