@reldens/server-utils 0.10.0 → 0.11.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.
@@ -47,7 +47,13 @@ class AppServerFactory
47
47
  this.corsOrigin = String(process.env.RELDENS_CORS_ORIGIN || '*');
48
48
  this.corsMethods = String(process.env.RELDENS_CORS_METHODS || 'GET,POST').split(',');
49
49
  this.corsHeaders = String(process.env.RELDENS_CORS_HEADERS || 'Content-Type,Authorization').split(',');
50
- this.errorMessage = '';
50
+ this.tooManyRequestsMessage = String(
51
+ process.env.RELDENS_TOO_MANY_REQUESTS_MESSAGE || 'Too many requests, please try again later.'
52
+ );
53
+ this.error = {};
54
+ this.processErrorResponse = function(status, message, req, res) {
55
+ return { status, message, handled: false };
56
+ };
51
57
  }
52
58
 
53
59
  createAppServer(appServerConfig)
@@ -72,7 +78,7 @@ class AppServerFactory
72
78
  max: this.maxRequests,
73
79
  standardHeaders: true,
74
80
  legacyHeaders: false,
75
- message: 'Too many requests from this IP, please try again later'
81
+ message: this.tooManyRequestsMessage
76
82
  };
77
83
  if(this.applyKeyGenerator){
78
84
  limiterParams.keyGenerator = function(req){
@@ -168,13 +174,21 @@ class AppServerFactory
168
174
  app.get('/', async (req, res, next) => {
169
175
  if('/' === req._parsedUrl.pathname){
170
176
  if('function' !== typeof homePageLoadCallback){
171
- return res.send('Homepage contents could not be loaded.');
177
+ let result = this.processErrorResponse(500, 'Homepage contents could not be loaded.', req, res);
178
+ if(result.handled){
179
+ return;
180
+ }
181
+ return res.status(result.status).send(result.message);
172
182
  }
173
183
  try {
174
184
  return res.send(await homePageLoadCallback(req));
175
185
  } catch(error){
176
- this.errorMessage = 'Error loading homepage.';
177
- return res.status(500).send('Error loading homepage.');
186
+ this.error = {message: 'Error loading homepage.', error};
187
+ let result = this.processErrorResponse(500, 'Error loading homepage.', req, res);
188
+ if(result.handled){
189
+ return;
190
+ }
191
+ return res.status(result.status).send(result.message);
178
192
  }
179
193
  }
180
194
  next();
@@ -184,7 +198,7 @@ class AppServerFactory
184
198
  async serveStatics(app, statics)
185
199
  {
186
200
  if(!FileHandler.isValidPath(statics)){
187
- this.errorMessage = 'Invalid statics path.';
201
+ this.error = {message: 'Invalid statics path.'};
188
202
  return;
189
203
  }
190
204
  let staticOptions = {
@@ -202,7 +216,7 @@ class AppServerFactory
202
216
  async serveStaticsPath(app, staticsPath, statics)
203
217
  {
204
218
  if(!FileHandler.isValidPath(staticsPath) || !FileHandler.isValidPath(statics)){
205
- this.errorMessage = 'Invalid statics path to be served.';
219
+ this.error = {message: 'Invalid statics path to be served.'};
206
220
  return;
207
221
  }
208
222
  let staticOptions = {
@@ -18,6 +18,9 @@ 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 || function(status, message, req, res) {
22
+ return { status, message, handled: false };
23
+ };
21
24
  }
22
25
 
23
26
  createUploader(fields, buckets, allowedFileTypes)
@@ -66,14 +69,30 @@ class UploaderFactory
66
69
  if(err){
67
70
  if(err instanceof multer.MulterError){
68
71
  if(err.code === 'LIMIT_FILE_SIZE'){
69
- return res.status(413).send('File too large');
72
+ let result = this.processErrorResponse(413, 'File too large.', req, res);
73
+ if(result.handled){
74
+ return;
75
+ }
76
+ return res.status(result.status).send(result.message);
70
77
  }
71
78
  if(err.code === 'LIMIT_FILE_COUNT'){
72
- return res.status(413).send('Too many files');
79
+ let result = this.processErrorResponse(413, 'Too many files.', req, res);
80
+ if(result.handled){
81
+ return;
82
+ }
83
+ return res.status(result.status).send(result.message);
84
+ }
85
+ let result = this.processErrorResponse(400, 'File upload error: ' + err.message, req, res);
86
+ if(result.handled){
87
+ return;
73
88
  }
74
- return res.status(400).send('File upload error: ' + err.message);
89
+ return res.status(result.status).send(result.message);
75
90
  }
76
- return res.status(500).send('Server error during file upload');
91
+ let result = this.processErrorResponse(500, 'Server error during file upload.', req, res);
92
+ if(result.handled){
93
+ return;
94
+ }
95
+ return res.status(result.status).send(result.message);
77
96
  }
78
97
  if(!req.files){
79
98
  return next();
@@ -85,7 +104,11 @@ class UploaderFactory
85
104
  if(FileHandler.exists(file.path)){
86
105
  FileHandler.remove(file.path);
87
106
  }
88
- return res.status(415).send('File contents do not match declared type.');
107
+ let result = this.processErrorResponse(415, 'File contents do not match declared type.', req, res);
108
+ if(result.handled){
109
+ return;
110
+ }
111
+ return res.status(result.status).send(result.message);
89
112
  }
90
113
  }
91
114
  }
@@ -93,7 +116,11 @@ class UploaderFactory
93
116
  } catch(error){
94
117
  this.error = {message: 'File validation error.', error};
95
118
  this.cleanupFiles(req.files);
96
- return res.status(500).send('Error processing uploaded files');
119
+ let result = this.processErrorResponse(500, 'Error processing uploaded files.', req, res);
120
+ if(result.handled){
121
+ return;
122
+ }
123
+ return res.status(result.status).send(result.message);
97
124
  }
98
125
  });
99
126
  };
@@ -118,11 +145,11 @@ class UploaderFactory
118
145
  this.error = {message: 'Field name is invalid'};
119
146
  return false;
120
147
  }
121
- if(!Object.prototype.hasOwnProperty.call(buckets, field.name)){
148
+ if(!buckets[field.name]){
122
149
  this.error = {message: `Missing bucket for field: ${field.name}`};
123
150
  return false;
124
151
  }
125
- if(!Object.prototype.hasOwnProperty.call(allowedFileTypes, field.name)){
152
+ if(!allowedFileTypes[field.name]){
126
153
  this.error = {message: `Missing allowedFileType for field: ${field.name}`};
127
154
  return false;
128
155
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/server-utils",
3
3
  "scope": "@reldens",
4
- "version": "0.10.0",
4
+ "version": "0.11.0",
5
5
  "description": "Reldens - Server Utils",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",