piper-utils 1.1.26 → 1.1.27

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.
@@ -223,21 +223,75 @@ describe('requestResponse', () => {
223
223
  describe('detectSequelizeError', () => {
224
224
  it('should find sequelize errors', () => {
225
225
  const body = {
226
- errors: [
227
- {
228
- message: 'I'
229
- }
230
- ],
226
+ errors: [{ message: 'I' }],
231
227
  parent: 'eggs',
232
- original: {
233
- detail: 'like'
234
- },
228
+ original: { detail: 'like' },
235
229
  TypeError: 'green'
236
230
  };
237
231
 
238
232
  const res = detectSequelizeError(body);
239
233
  expect(res).toEqual({ message: 'Ilikegreeneggs' });
240
234
  });
235
+
236
+ it('should return a friendly message for foreign key constraint errors', () => {
237
+ const body = {
238
+ name: 'SequelizeForeignKeyConstraintError',
239
+ parent: { detail: 'Key (id)=(5) is still referenced from table "orders".' },
240
+ original: { detail: 'Key (id)=(5) is still referenced from table "orders".' }
241
+ };
242
+
243
+ const res = detectSequelizeError(body);
244
+ expect(res).toEqual({
245
+ message: 'You cannot delete this item as it is in use',
246
+ statusCode: 409,
247
+ errorCode: '4090'
248
+ });
249
+ });
250
+
251
+ it('should return a friendly message for unique constraint errors with field name', () => {
252
+ const body = {
253
+ name: 'SequelizeUniqueConstraintError',
254
+ errors: [{ message: 'sku must be unique', path: 'sku' }]
255
+ };
256
+
257
+ const res = detectSequelizeError(body);
258
+ expect(res).toEqual({
259
+ message: 'A record with this sku already exists',
260
+ statusCode: 409,
261
+ errorCode: '4091'
262
+ });
263
+ });
264
+
265
+ it('should return a generic message for unique constraint errors with no field path', () => {
266
+ const body = {
267
+ name: 'SequelizeUniqueConstraintError',
268
+ errors: [{ message: 'must be unique' }]
269
+ };
270
+
271
+ const res = detectSequelizeError(body);
272
+ expect(res).toEqual({
273
+ message: 'A record with this value already exists',
274
+ statusCode: 409,
275
+ errorCode: '4091'
276
+ });
277
+ });
278
+
279
+ it('should return validation messages for validation errors', () => {
280
+ const body = {
281
+ name: 'SequelizeValidationError',
282
+ errors: [
283
+ { message: 'name cannot be null' },
284
+ { message: 'price must be a number' }
285
+ ]
286
+ };
287
+
288
+ const res = detectSequelizeError(body);
289
+ expect(res).toEqual({
290
+ message: 'name cannot be null, price must be a number',
291
+ statusCode: 400,
292
+ errorCode: '4001'
293
+ });
294
+ });
241
295
  });
242
296
  describe('detectJoyError', () => {
243
297
  it('should find nested Joy errors', () => {