lesgo 0.7.4 → 0.7.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lesgo",
3
- "version": "0.7.4",
3
+ "version": "0.7.5",
4
4
  "description": "Core framework for lesgo node.js serverless framework.",
5
5
  "main": "./src/index.js",
6
6
  "author": "Sufiyan Rahmat",
@@ -21,7 +21,7 @@ describe('MiddlewareGroup: test errorHandler middleware', () => {
21
21
  expect(dataBody).toHaveProperty('status', 'error');
22
22
  expect(dataBody).toHaveProperty('data', null);
23
23
  expect(dataBody).toHaveProperty('error');
24
- expect(dataBody).toHaveProperty('error.code', 'UNKNOWN_ERROR');
24
+ expect(dataBody).toHaveProperty('error.code', 'UNHANDLED_ERROR');
25
25
  expect(dataBody).toHaveProperty(
26
26
  'error.message',
27
27
  'Error: Test validation error'
@@ -78,7 +78,7 @@ describe('MiddlewareGroup: test errorHandler middleware', () => {
78
78
 
79
79
  expect(data.statusCode).toBe(500);
80
80
 
81
- expect(dataBody).toHaveProperty('error.code', 'UNKNOWN_ERROR');
81
+ expect(dataBody).toHaveProperty('error.code', 'UNHANDLED_ERROR');
82
82
  expect(dataBody).toHaveProperty('error.message', 'Test error message');
83
83
  expect(dataBody).toHaveProperty('error.details', '');
84
84
  });
@@ -126,7 +126,7 @@ describe('MiddlewareGroup: test errorHandler middleware', () => {
126
126
  expect(dataBody).toHaveProperty('status', 'error');
127
127
  expect(dataBody).toHaveProperty('data', null);
128
128
  expect(dataBody).toHaveProperty('error');
129
- expect(dataBody).toHaveProperty('error.code', 'UNKNOWN_ERROR');
129
+ expect(dataBody).toHaveProperty('error.code', 'UNHANDLED_ERROR');
130
130
  expect(dataBody).toHaveProperty('error.message', '');
131
131
  expect(dataBody).toHaveProperty('error.details', '');
132
132
  });
@@ -102,7 +102,6 @@ describe('MiddlewareGroup: test normalizeHttpRequestBeforeHandler', () => {
102
102
  };
103
103
 
104
104
  normalizeHttpRequestBeforeHandler(handler, () => {});
105
- expect(handler.event.requestContext.requestId).toBe('requestId');
106
105
  });
107
106
 
108
107
  it('should return auth.sub if Authorization exists', () => {
@@ -119,6 +118,44 @@ describe('MiddlewareGroup: test normalizeHttpRequestBeforeHandler', () => {
119
118
  expect(handler.event.auth.sub).toBe('f2b5349d-f5e3-44f5-9c08-ae6b01e95434');
120
119
  });
121
120
 
121
+ it.each`
122
+ version | tags
123
+ ${'1.0'} | ${{ path: '/v1/path', httpMethod: 'GET' }}
124
+ ${'2.0'} | ${{ path: '/v2/path', httpMethod: 'POST' }}
125
+ ${'3.0'} | ${{ path: '/v2/path', httpMethod: 'POST' }}
126
+ `(
127
+ 'should identify path and httpMethod based on version',
128
+ ({ version, tags }) => {
129
+ const handler = {
130
+ event: {
131
+ version,
132
+ headers: {},
133
+ ...tags,
134
+ requestContext: {
135
+ http: {
136
+ ...tags,
137
+ method: tags.httpMethod,
138
+ },
139
+ },
140
+ },
141
+ };
142
+ normalizeHttpRequestBeforeHandler(handler, () => {});
143
+ expect(logger.meta.tags).toStrictEqual(tags);
144
+ }
145
+ );
146
+
147
+ it('should not set tags when using API Gateway v2 and requestContext is empty', () => {
148
+ const handler = {
149
+ event: {
150
+ version: '2.0',
151
+ headers: {},
152
+ },
153
+ };
154
+
155
+ normalizeHttpRequestBeforeHandler(handler, () => {});
156
+ expect(logger.meta.tags).toStrictEqual({});
157
+ });
158
+
122
159
  it('should not set meta on debug', () => {
123
160
  app.debug = true;
124
161
  const handler = {
@@ -133,6 +170,7 @@ describe('MiddlewareGroup: test normalizeHttpRequestBeforeHandler', () => {
133
170
  },
134
171
  },
135
172
  };
173
+
136
174
  normalizeHttpRequestBeforeHandler(handler, () => {});
137
175
  expect(logger.meta.auth).toBe(handler.event.auth);
138
176
  expect(logger.meta.queryStringParameters).toStrictEqual(
@@ -1,6 +1,8 @@
1
1
  import logger from '../utils/logger';
2
2
  import isEmpty from '../utils/isEmpty';
3
3
 
4
+ const FILE = 'Lesgo/middlewares/errorHttpResponseMiddleware';
5
+
4
6
  export const errorHttpResponseHandler = async opts => {
5
7
  const defaults = {
6
8
  response: '',
@@ -29,7 +31,7 @@ export const errorHttpResponseHandler = async opts => {
29
31
  status: 'error',
30
32
  data: null,
31
33
  error: {
32
- code: options.error.code || 'UNKNOWN_ERROR',
34
+ code: options.error.code || 'UNHANDLED_ERROR',
33
35
  message: options.error.name
34
36
  ? `${options.error.name}: ${options.error.message}`
35
37
  : options.error.message || options.error,
@@ -40,18 +42,18 @@ export const errorHttpResponseHandler = async opts => {
40
42
 
41
43
  const statusCode = options.error.statusCode || options.statusCode;
42
44
 
43
- /* istanbul ignore next */
44
- if (statusCode === 500) {
45
- // this is likely an unhandled exception, log it
46
- logger.error(options.error);
45
+ if (!isEmpty(options.error)) {
46
+ logger.log(statusCode === 500 ? 'error' : 'warn', options.error);
47
47
  } else {
48
- logger.warn(options.error);
48
+ logger.log(statusCode === 500 ? 'error' : 'warn', jsonBody.error.message, {
49
+ error: jsonBody.error,
50
+ });
49
51
  }
50
52
 
51
53
  try {
52
54
  if (!isEmpty(opts.db)) await opts.db.end();
53
55
  } catch (err) {
54
- // do nothing
56
+ logger.error(`${FILE}::Failed to end db connection`, err);
55
57
  }
56
58
 
57
59
  return {
@@ -53,14 +53,26 @@ export const normalizeHttpRequestBeforeHandler = (handler, next) => {
53
53
  // eslint-disable-next-line no-param-reassign
54
54
  handler.event.auth = auth;
55
55
 
56
+ const tags = {};
57
+ switch (handler.event.version) {
58
+ case '2.0': {
59
+ if (handler.event.requestContext && handler.event.requestContext.http) {
60
+ tags.path = handler.event.requestContext.http.path;
61
+ tags.httpMethod = handler.event.requestContext.http.method;
62
+ }
63
+ break;
64
+ }
65
+ default:
66
+ tags.path = handler.event.path;
67
+ tags.httpMethod = handler.event.httpMethod;
68
+ break;
69
+ }
70
+
56
71
  logger.addMeta({
57
72
  requestId: handler.event.requestContext
58
73
  ? handler.event.requestContext.requestId
59
74
  : null,
60
- tags: {
61
- path: handler.event.path,
62
- httpMethod: handler.event.httpMethod,
63
- },
75
+ tags,
64
76
  });
65
77
 
66
78
  if (app.debug) {