piper-utils 1.1.68 → 1.1.70

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.
@@ -125,6 +125,12 @@ export function failure(body = {}, options) {
125
125
  }
126
126
  const newBody = _.merge(NORMAL_ERROR, cleanedErrorBody);
127
127
 
128
+ const debugLogging = process.env.UTIL_LOG === 'LOG_ALL' || process.env.BUILD_ENV === 'test';
129
+ if (!debugLogging && newBody.statusCode >= 500) {
130
+ // Log the raw error object so thrown Error stacks survive (JSON.stringify drops them)
131
+ console.error('------->UTIL ERROR:', newBody.statusCode, _.get(body, 'message', ''), body);
132
+ }
133
+
128
134
  return buildResponse(newBody.statusCode, newBody);
129
135
  }
130
136
 
@@ -266,7 +272,6 @@ export function detectJoyError(body) {
266
272
  return acc;
267
273
  }, '');
268
274
 
269
- console.error('USER VALIDATION ERROR:', body);
270
275
  const msg = (joyError?.message || '') + v;
271
276
 
272
277
  if (msg) {
@@ -135,6 +135,64 @@ describe('requestResponse', () => {
135
135
  expect(res.headers['Access-Control-Allow-Origin']).toEqual('*');
136
136
  expect(res.headers['Referrer-Policy']).toEqual('strict-origin-when-cross-origin');
137
137
  });
138
+ it('should NOT log a known/handled 4xx error (errorCode + 4xx)', () => {
139
+ const prevBuildEnv = process.env.BUILD_ENV;
140
+ const prevUtilLog = process.env.UTIL_LOG;
141
+ process.env.BUILD_ENV = 'production';
142
+ delete process.env.UTIL_LOG;
143
+ const errorSpy = spyOn(console, 'error');
144
+
145
+ const res = failure({ statusCode: 400, errorCode: '4007', message: 'Insufficient funds' }, {});
146
+
147
+ expect(errorSpy).not.toHaveBeenCalled();
148
+ expect(res.statusCode).toEqual(400);
149
+
150
+ process.env.BUILD_ENV = prevBuildEnv;
151
+ if (prevUtilLog === undefined) {
152
+ delete process.env.UTIL_LOG;
153
+ } else {
154
+ process.env.UTIL_LOG = prevUtilLog;
155
+ }
156
+ });
157
+ it('should log the full object for a 5xx error', () => {
158
+ const prevBuildEnv = process.env.BUILD_ENV;
159
+ const prevUtilLog = process.env.UTIL_LOG;
160
+ process.env.BUILD_ENV = 'production';
161
+ delete process.env.UTIL_LOG;
162
+ const errorSpy = spyOn(console, 'error');
163
+ const body = { statusCode: 500, errorCode: '5001', message: 'boom' };
164
+
165
+ failure(body, {});
166
+
167
+ expect(errorSpy).toHaveBeenCalledWith('------->UTIL ERROR:', 500, 'boom', body);
168
+
169
+ process.env.BUILD_ENV = prevBuildEnv;
170
+ if (prevUtilLog === undefined) {
171
+ delete process.env.UTIL_LOG;
172
+ } else {
173
+ process.env.UTIL_LOG = prevUtilLog;
174
+ }
175
+ });
176
+ it('should log a concise one-liner for an unmapped 4xx error (no deliberate errorCode)', () => {
177
+ const prevBuildEnv = process.env.BUILD_ENV;
178
+ const prevUtilLog = process.env.UTIL_LOG;
179
+ process.env.BUILD_ENV = 'production';
180
+ delete process.env.UTIL_LOG;
181
+ const errorSpy = spyOn(console, 'error');
182
+
183
+ // A Joi-style error (has `details`, no errorCode) maps to a 4xx via detectJoyError,
184
+ // so the original body is not a known client error -> concise branch, no object dump.
185
+ failure({ details: 'bar' }, {});
186
+
187
+ expect(errorSpy).toHaveBeenCalledWith('------->UTIL ERROR:', 400, 'INTERNAL UTIL ERROR');
188
+
189
+ process.env.BUILD_ENV = prevBuildEnv;
190
+ if (prevUtilLog === undefined) {
191
+ delete process.env.UTIL_LOG;
192
+ } else {
193
+ process.env.UTIL_LOG = prevUtilLog;
194
+ }
195
+ });
138
196
  });
139
197
  describe('parseBody', () => {
140
198
  it('parse a json string', () => {