@reldens/server-utils 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/app-server-factory.js +10 -12
- package/lib/uploader-factory.js +31 -36
- package/package.json +1 -1
|
@@ -51,9 +51,7 @@ class AppServerFactory
|
|
|
51
51
|
process.env.RELDENS_TOO_MANY_REQUESTS_MESSAGE || 'Too many requests, please try again later.'
|
|
52
52
|
);
|
|
53
53
|
this.error = {};
|
|
54
|
-
this.processErrorResponse =
|
|
55
|
-
return { status, message, handled: false };
|
|
56
|
-
};
|
|
54
|
+
this.processErrorResponse = false;
|
|
57
55
|
}
|
|
58
56
|
|
|
59
57
|
createAppServer(appServerConfig)
|
|
@@ -174,21 +172,21 @@ class AppServerFactory
|
|
|
174
172
|
app.get('/', async (req, res, next) => {
|
|
175
173
|
if('/' === req._parsedUrl.pathname){
|
|
176
174
|
if('function' !== typeof homePageLoadCallback){
|
|
177
|
-
let
|
|
178
|
-
if(
|
|
179
|
-
return;
|
|
175
|
+
let errorMessage = 'Homepage contents could not be loaded.';
|
|
176
|
+
if('function' === typeof this.processErrorResponse){
|
|
177
|
+
return this.processErrorResponse(500, errorMessage, req, res);
|
|
180
178
|
}
|
|
181
|
-
return res.status(
|
|
179
|
+
return res.status(500).send(errorMessage);
|
|
182
180
|
}
|
|
183
181
|
try {
|
|
184
182
|
return res.send(await homePageLoadCallback(req));
|
|
185
183
|
} catch(error){
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if(
|
|
189
|
-
return;
|
|
184
|
+
let message = 'Error loading homepage.';
|
|
185
|
+
this.error = {message, error};
|
|
186
|
+
if('function' === typeof this.processErrorResponse){
|
|
187
|
+
return this.processErrorResponse(500, message, req, res);
|
|
190
188
|
}
|
|
191
|
-
return res.status(
|
|
189
|
+
return res.status(500).send(message);
|
|
192
190
|
}
|
|
193
191
|
}
|
|
194
192
|
next();
|
package/lib/uploader-factory.js
CHANGED
|
@@ -18,9 +18,7 @@ class UploaderFactory
|
|
|
18
18
|
this.fileLimit = props.fileLimit || 0;
|
|
19
19
|
this.allowedExtensions = props.allowedExtensions;
|
|
20
20
|
this.applySecureFileNames = props.applySecureFileNames;
|
|
21
|
-
this.processErrorResponse = props.processErrorResponse ||
|
|
22
|
-
return { status, message, handled: false };
|
|
23
|
-
};
|
|
21
|
+
this.processErrorResponse = props.processErrorResponse || false;
|
|
24
22
|
}
|
|
25
23
|
|
|
26
24
|
createUploader(fields, buckets, allowedFileTypes)
|
|
@@ -65,34 +63,34 @@ class UploaderFactory
|
|
|
65
63
|
}
|
|
66
64
|
});
|
|
67
65
|
return (req, res, next) => {
|
|
68
|
-
upload.fields(fields)(req, res, async (
|
|
69
|
-
if(
|
|
70
|
-
if(
|
|
71
|
-
if(
|
|
72
|
-
let
|
|
73
|
-
if(
|
|
74
|
-
return;
|
|
66
|
+
upload.fields(fields)(req, res, async (multerError) => {
|
|
67
|
+
if(multerError){
|
|
68
|
+
if(multerError instanceof multer.MulterError){
|
|
69
|
+
if(multerError.code === 'LIMIT_FILE_SIZE'){
|
|
70
|
+
let messageFile = 'File too large.';
|
|
71
|
+
if('function' === typeof this.processErrorResponse){
|
|
72
|
+
return this.processErrorResponse(413, messageFile, req, res);
|
|
75
73
|
}
|
|
76
|
-
return res.status(
|
|
74
|
+
return res.status(413).send(messageFile);
|
|
77
75
|
}
|
|
78
|
-
if(
|
|
79
|
-
let
|
|
80
|
-
if(
|
|
81
|
-
return;
|
|
76
|
+
if(multerError.code === 'LIMIT_FILE_COUNT'){
|
|
77
|
+
let messageTooMany = 'Too many files.';
|
|
78
|
+
if('function' === typeof this.processErrorResponse){
|
|
79
|
+
return this.processErrorResponse(413, messageTooMany, req, res);
|
|
82
80
|
}
|
|
83
|
-
return res.status(
|
|
81
|
+
return res.status(413).send(messageTooMany);
|
|
84
82
|
}
|
|
85
|
-
let
|
|
86
|
-
if(
|
|
87
|
-
return;
|
|
83
|
+
let messageUpload = 'File upload error.';
|
|
84
|
+
if('function' === typeof this.processErrorResponse){
|
|
85
|
+
return this.processErrorResponse(400, messageUpload, multerError, req, res);
|
|
88
86
|
}
|
|
89
|
-
return res.status(
|
|
87
|
+
return res.status(400).send(messageUpload);
|
|
90
88
|
}
|
|
91
|
-
let
|
|
92
|
-
if(
|
|
93
|
-
return;
|
|
89
|
+
let messageServer = 'Server error during file upload.';
|
|
90
|
+
if('function' === typeof this.processErrorResponse){
|
|
91
|
+
return this.processErrorResponse(500, messageServer, req, res);
|
|
94
92
|
}
|
|
95
|
-
return res.status(
|
|
93
|
+
return res.status(500).send(messageServer);
|
|
96
94
|
}
|
|
97
95
|
if(!req.files){
|
|
98
96
|
return next();
|
|
@@ -104,26 +102,23 @@ class UploaderFactory
|
|
|
104
102
|
if(FileHandler.exists(file.path)){
|
|
105
103
|
FileHandler.remove(file.path);
|
|
106
104
|
}
|
|
107
|
-
let
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
);
|
|
111
|
-
if(result.handled){
|
|
112
|
-
return;
|
|
105
|
+
let messageContents = 'File contents do not match declared type.';
|
|
106
|
+
if('function' === typeof this.processErrorResponse){
|
|
107
|
+
return this.processErrorResponse(415, messageContents, req, res);
|
|
113
108
|
}
|
|
114
|
-
return res.status(
|
|
109
|
+
return res.status(415).send(messageContents);
|
|
115
110
|
}
|
|
116
111
|
}
|
|
117
112
|
}
|
|
118
113
|
next();
|
|
119
114
|
} catch(error){
|
|
120
|
-
|
|
115
|
+
let messageProcessing = 'Error processing uploaded files.';
|
|
116
|
+
this.error = {message: messageProcessing, error};
|
|
121
117
|
this.cleanupFiles(req.files);
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return;
|
|
118
|
+
if('function' === typeof this.processErrorResponse){
|
|
119
|
+
return this.processErrorResponse(500, messageProcessing, req, res);
|
|
125
120
|
}
|
|
126
|
-
return res.status(
|
|
121
|
+
return res.status(500).send(messageProcessing);
|
|
127
122
|
}
|
|
128
123
|
});
|
|
129
124
|
};
|