@reldens/server-utils 0.11.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.
@@ -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 = function(status, message, req, res) {
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 result = this.processErrorResponse(500, 'Homepage contents could not be loaded.', req, res);
178
- if(result.handled){
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(result.status).send(result.message);
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
- this.error = {message: 'Error loading homepage.', error};
187
- let result = this.processErrorResponse(500, 'Error loading homepage.', req, res);
188
- if(result.handled){
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(result.status).send(result.message);
189
+ return res.status(500).send(message);
192
190
  }
193
191
  }
194
192
  next();
@@ -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 || function(status, message, req, res) {
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 (err) => {
69
- if(err){
70
- if(err instanceof multer.MulterError){
71
- if(err.code === 'LIMIT_FILE_SIZE'){
72
- let result = this.processErrorResponse(413, 'File too large.', req, res);
73
- if(result.handled){
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(result.status).send(result.message);
74
+ return res.status(413).send(messageFile);
77
75
  }
78
- if(err.code === 'LIMIT_FILE_COUNT'){
79
- let result = this.processErrorResponse(413, 'Too many files.', req, res);
80
- if(result.handled){
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(result.status).send(result.message);
81
+ return res.status(413).send(messageTooMany);
84
82
  }
85
- let result = this.processErrorResponse(400, 'File upload error: ' + err.message, req, res);
86
- if(result.handled){
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(result.status).send(result.message);
87
+ return res.status(400).send(messageUpload);
90
88
  }
91
- let result = this.processErrorResponse(500, 'Server error during file upload.', req, res);
92
- if(result.handled){
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(result.status).send(result.message);
93
+ return res.status(500).send(messageServer);
96
94
  }
97
95
  if(!req.files){
98
96
  return next();
@@ -104,23 +102,23 @@ class UploaderFactory
104
102
  if(FileHandler.exists(file.path)){
105
103
  FileHandler.remove(file.path);
106
104
  }
107
- let result = this.processErrorResponse(415, 'File contents do not match declared type.', req, res);
108
- if(result.handled){
109
- 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);
110
108
  }
111
- return res.status(result.status).send(result.message);
109
+ return res.status(415).send(messageContents);
112
110
  }
113
111
  }
114
112
  }
115
113
  next();
116
114
  } catch(error){
117
- this.error = {message: 'File validation error.', error};
115
+ let messageProcessing = 'Error processing uploaded files.';
116
+ this.error = {message: messageProcessing, error};
118
117
  this.cleanupFiles(req.files);
119
- let result = this.processErrorResponse(500, 'Error processing uploaded files.', req, res);
120
- if(result.handled){
121
- return;
118
+ if('function' === typeof this.processErrorResponse){
119
+ return this.processErrorResponse(500, messageProcessing, req, res);
122
120
  }
123
- return res.status(result.status).send(result.message);
121
+ return res.status(500).send(messageProcessing);
124
122
  }
125
123
  });
126
124
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/server-utils",
3
3
  "scope": "@reldens",
4
- "version": "0.11.0",
4
+ "version": "0.13.0",
5
5
  "description": "Reldens - Server Utils",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",