@felloh-org/lambda-wrapper 1.0.19 → 1.0.23

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.
@@ -41,7 +41,7 @@ class Index extends _.default {
41
41
  super();
42
42
  this.body = {
43
43
  data: data !== null ? data : {},
44
- errors: {},
44
+ errors: [],
45
45
  meta: {
46
46
  code: code !== null ? code : {},
47
47
  reason: _statusCodes.default[code].reason,
@@ -85,6 +85,23 @@ class Index extends _.default {
85
85
  this.code = code;
86
86
  this.body.meta.code = code;
87
87
  }
88
+ /**
89
+ * Get errors
90
+ */
91
+
92
+
93
+ getErrors() {
94
+ return this.body.errors;
95
+ }
96
+ /**
97
+ * Set errors
98
+ * @param error
99
+ */
100
+
101
+
102
+ addError(error) {
103
+ this.body.errors.push(error);
104
+ }
88
105
  /**
89
106
  * Set a meta variable
90
107
  * @param key
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = exports.REQUEST_TYPES = exports.HTTP_METHODS_WITH_PAYLOADS = exports.HTTP_METHODS_WITHOUT_PAYLOADS = exports.ERROR_TYPES = void 0;
6
+ exports.default = exports.REQUEST_TYPES = exports.HTTP_METHODS_WITH_PAYLOADS = exports.HTTP_METHODS_WITHOUT_PAYLOADS = void 0;
7
7
 
8
8
  var _querystring = _interopRequireDefault(require("querystring"));
9
9
 
@@ -36,17 +36,12 @@ const REQUEST_TYPES = {
36
36
  exports.REQUEST_TYPES = REQUEST_TYPES;
37
37
  const HTTP_METHODS_WITHOUT_PAYLOADS = [REQUEST_TYPES.DELETE, REQUEST_TYPES.GET, REQUEST_TYPES.HEAD, REQUEST_TYPES.OPTIONS];
38
38
  exports.HTTP_METHODS_WITHOUT_PAYLOADS = HTTP_METHODS_WITHOUT_PAYLOADS;
39
- const HTTP_METHODS_WITH_PAYLOADS = [REQUEST_TYPES.PATCH, REQUEST_TYPES.POST, REQUEST_TYPES.PUT]; // Define action specific error types
40
-
41
- exports.HTTP_METHODS_WITH_PAYLOADS = HTTP_METHODS_WITH_PAYLOADS;
42
- const ERROR_TYPES = {
43
- VALIDATION_ERROR: new _response.default({}, 400, 'required fields are missing')
44
- };
39
+ const HTTP_METHODS_WITH_PAYLOADS = [REQUEST_TYPES.PATCH, REQUEST_TYPES.POST, REQUEST_TYPES.PUT];
45
40
  /**
46
41
  * RequestService class
47
42
  */
48
43
 
49
- exports.ERROR_TYPES = ERROR_TYPES;
44
+ exports.HTTP_METHODS_WITH_PAYLOADS = HTTP_METHODS_WITH_PAYLOADS;
50
45
 
51
46
  class Request extends _dependencyAware.default {
52
47
  /**
@@ -262,8 +257,18 @@ class Request extends _dependencyAware.default {
262
257
  resolve();
263
258
  } else {
264
259
  Logger.label('request-validation-failed');
265
- const validationErrorResponse = ERROR_TYPES.VALIDATION_ERROR;
266
- validationErrorResponse.setBodyVariable('validation_errors', validation);
260
+ const validationErrorResponse = new _response.default({}, 422);
261
+ Object.keys(validation).forEach(validationKey => {
262
+ validation[validationKey].forEach(message => {
263
+ validationErrorResponse.addError({
264
+ title: 'Validation Error',
265
+ message,
266
+ documentation_url: 'https://developers.felloh.com/errors#error-responses',
267
+ type: 'validation',
268
+ code: `validation.${validationKey}`
269
+ });
270
+ });
271
+ });
267
272
  reject(validationErrorResponse);
268
273
  }
269
274
  });
@@ -82,12 +82,13 @@ const handleError = (di, error, throwError = false) => {
82
82
  return new Error(error);
83
83
  }
84
84
 
85
- const responseDetails = {
86
- body: error.body || {},
87
- code: error.code || 500,
88
- details: error.details || 'unknown error'
89
- };
90
- return _response.default.generate(responseDetails.body, responseDetails.code, responseDetails.details);
85
+ if (error instanceof _response.default) {
86
+ return error.generate();
87
+ }
88
+
89
+ const response = new _response.default({}, error.code || 500);
90
+ response.setErrors(error.body || {});
91
+ return response.generate();
91
92
  };
92
93
  /**
93
94
  * Lambda Wrapper.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@felloh-org/lambda-wrapper",
3
- "version": "1.0.19",
3
+ "version": "1.0.23",
4
4
  "description": "Lambda wrapper for all Felloh Serverless Projects",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {