Skip to content
This repository was archived by the owner on Dec 15, 2019. It is now read-only.

Commit eab9ac5

Browse files
committed
Allow error objects; Send error codes if upload failed
1 parent bc2a8d3 commit eab9ac5

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

src/api/api.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ const mapHandler = mod => async (req, res) => {
1212
const response = await mod(req, res).then(res => {
1313
return {data: res, error: null};
1414
}).catch(reason => {
15-
16-
if (typeof reason !== 'string') {
17-
console.warn('Suspicious error', reason); // eslint-disable-line no-console
18-
reason = 'Internal error.';
19-
}
20-
2115
return {data: null, error: reason};
2216
});
2317

src/api/endpoints/data/upload/upload.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ module.exports = async req => {
1515
fs.unlink(path, () => 0);
1616
}
1717

18-
throw reason;
18+
throw {code: -1, text: reason};
1919
});
2020

2121
const contentLength = Number(req.get('content-length'));
2222

2323
// Validate header
2424
if (isNaN(contentLength)) {
25-
throw 'Invalid content-length header';
25+
throw {code: 1, text: 'Invalid content-length header'};
2626
}
2727

2828
// Compare current storage size and upload size with limit
2929
const {totalStorageLimitPerUser} = _config.server;
3030
if (~totalStorageLimitPerUser && (await usedSpaceBy(user.id)) + contentLength > totalStorageLimitPerUser) {
31-
throw `Storage limit of ${totalStorageLimitPerUser} bytes exceed`;
31+
throw {code: 2, text: `Storage limit of ${totalStorageLimitPerUser} bytes exceed`};
3232
}
3333

3434
// Rename files and create nodes
@@ -45,14 +45,14 @@ module.exports = async req => {
4545
const parent = fieldname.substring(0, fieldname.indexOf('-'));
4646
if (!parent) {
4747
fs.unlinkSync(path);
48-
throw 'Invalid node key';
48+
throw {code: 10, text: 'Invalid node key'};
4949
}
5050

5151
// Check if destination exists and is a folder
5252
const destNode = await nodeModel.findOne({owner: user.id, id: parent}).exec();
5353
if (!destNode || destNode.type !== 'dir') {
5454
fs.unlinkSync(path);
55-
throw 'Invalid parent node';
55+
throw {code: 11, text: 'Invalid parent node'};
5656
}
5757

5858
// Create and push new node

0 commit comments

Comments
 (0)