@jcbuisson/express-x 1.6.10 → 1.6.12

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/server.mjs +17 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.6.10",
3
+ "version": "1.6.12",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/server.mjs CHANGED
@@ -3,6 +3,14 @@ import http from 'http'
3
3
  import { Server } from "socket.io"
4
4
  import express from 'express'
5
5
 
6
+ export class MyCustomError extends Error {
7
+ constructor(message, code) {
8
+ super(message);
9
+ this.name = 'MyCustomError'
10
+ this.code = code
11
+ }
12
+ }
13
+
6
14
  /*
7
15
  * Enhance `app` express application with services and real-time features
8
16
  */
@@ -379,7 +387,7 @@ export function expressX(prisma, options = {}) {
379
387
  * Emit in return a 'client-response' message
380
388
  */
381
389
  socket.on('client-request', async ({ uid, name, action, args }) => {
382
- app.log('verbose', `client-request ${uid} ${name} ${action} ${JSON.stringify(args)}`)
390
+ app.log('verbose', `client-request ${connection.id} ${uid} ${name} ${action} ${JSON.stringify(args)}`)
383
391
  if (name in services) {
384
392
  const service = services[name]
385
393
  try {
@@ -394,34 +402,36 @@ export function expressX(prisma, options = {}) {
394
402
  try {
395
403
  const result = await serviceMethod(context, ...args)
396
404
  const trimmedResult = result ? JSON.stringify(result).slice(0, 300) : ''
397
- app.log('verbose', `client-response ${uid} ${trimmedResult}`)
405
+ app.log('verbose', `client-response ${connection.id} ${uid} ${trimmedResult}`)
398
406
  socket.emit('client-response', {
399
407
  uid,
400
408
  result,
401
409
  })
402
410
  } catch(err) {
403
411
  app.log('error', err.toString())
412
+ app.log('verbose', err.stack)
404
413
  socket.emit('client-response', {
405
414
  uid,
406
- error: err.toString(),
415
+ error: new MyCustomError(err.message, err.code),
407
416
  })
408
417
  }
409
418
  } else {
410
419
  socket.emit('client-response', {
411
420
  uid,
412
- error: `there is no method named '${action}' for service '${name}'`,
421
+ error: new MyCustomError(`there is no method named '${action}' for service '${name}'`, missing-method),
413
422
  })
414
423
  }
415
424
  } catch(error) {
425
+ app.log('verbose', error.stack)
416
426
  socket.emit('client-response', {
417
427
  uid,
418
- error,
419
- })
428
+ error: new MyCustomError("unknown error", 'unknown-error'),
429
+ })
420
430
  }
421
431
  } else {
422
432
  socket.emit('client-response', {
423
433
  uid,
424
- error: `there is no service named '${name}'`,
434
+ error: new MyCustomError(`there is no service named '${name}'`, 'missing-service'),
425
435
  })
426
436
  }
427
437
  })