@jcbuisson/express-x 1.6.17 → 1.7.0
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 +1 -1
- package/src/server.mjs +21 -15
- package/test/index.test.js +30 -0
package/package.json
CHANGED
package/src/server.mjs
CHANGED
|
@@ -3,14 +3,6 @@ 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
|
-
|
|
14
6
|
/*
|
|
15
7
|
* Enhance `app` express application with services and real-time features
|
|
16
8
|
*/
|
|
@@ -182,7 +174,7 @@ export function expressX(prisma, options = {}) {
|
|
|
182
174
|
function service(name) {
|
|
183
175
|
// get service from `services` cache
|
|
184
176
|
if (name in services) return services[name]
|
|
185
|
-
|
|
177
|
+
app.log('error', `there is no service named '${name}'`, 'missing-service')
|
|
186
178
|
}
|
|
187
179
|
|
|
188
180
|
function configure(callback) {
|
|
@@ -389,6 +381,7 @@ export function expressX(prisma, options = {}) {
|
|
|
389
381
|
|
|
390
382
|
try {
|
|
391
383
|
const result = await serviceMethod(context, ...args)
|
|
384
|
+
|
|
392
385
|
const trimmedResult = result ? JSON.stringify(result).slice(0, 300) : ''
|
|
393
386
|
app.log('verbose', `client-response ${connection.id} ${uid} ${trimmedResult}`)
|
|
394
387
|
socket.emit('client-response', {
|
|
@@ -396,30 +389,43 @@ export function expressX(prisma, options = {}) {
|
|
|
396
389
|
result,
|
|
397
390
|
})
|
|
398
391
|
} catch(err) {
|
|
399
|
-
|
|
392
|
+
console.log('!!!!!!error', err.code, err)
|
|
400
393
|
app.log('verbose', err.stack)
|
|
401
394
|
socket.emit('client-response', {
|
|
402
395
|
uid,
|
|
403
|
-
error:
|
|
396
|
+
error: {
|
|
397
|
+
code: err.code || 'unknown-error',
|
|
398
|
+
message: err.stack,
|
|
399
|
+
}
|
|
404
400
|
})
|
|
405
401
|
}
|
|
406
402
|
} else {
|
|
407
403
|
socket.emit('client-response', {
|
|
408
404
|
uid,
|
|
409
|
-
error:
|
|
405
|
+
error: {
|
|
406
|
+
code: 'missing-method',
|
|
407
|
+
message: `there is no method named '${action}' for service '${name}'`,
|
|
408
|
+
}
|
|
410
409
|
})
|
|
411
410
|
}
|
|
412
411
|
} catch(error) {
|
|
412
|
+
console.log('err', err)
|
|
413
413
|
app.log('verbose', error.stack)
|
|
414
414
|
socket.emit('client-response', {
|
|
415
415
|
uid,
|
|
416
|
-
error:
|
|
417
|
-
|
|
416
|
+
error: {
|
|
417
|
+
code: err.code || 'unknown-error',
|
|
418
|
+
message: error.stack,
|
|
419
|
+
}
|
|
420
|
+
})
|
|
418
421
|
}
|
|
419
422
|
} else {
|
|
420
423
|
socket.emit('client-response', {
|
|
421
424
|
uid,
|
|
422
|
-
error:
|
|
425
|
+
error: {
|
|
426
|
+
code: 'missing-service',
|
|
427
|
+
message: `there is no service named '${name}'`,
|
|
428
|
+
}
|
|
423
429
|
})
|
|
424
430
|
}
|
|
425
431
|
})
|
package/test/index.test.js
CHANGED
|
@@ -137,3 +137,33 @@ describe('HTTP/REST API', () => {
|
|
|
137
137
|
app.server.close()
|
|
138
138
|
})
|
|
139
139
|
})
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
describe('Error codes', () => {
|
|
143
|
+
|
|
144
|
+
before(async () => {
|
|
145
|
+
app.createDatabaseService('User')
|
|
146
|
+
app.createDatabaseService('Post')
|
|
147
|
+
|
|
148
|
+
await app.service('User').deleteMany()
|
|
149
|
+
await app.service('Post').deleteMany()
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
it("can detect 'missing-service' error", async () => {
|
|
153
|
+
try {
|
|
154
|
+
await app.service('users').findMany({})
|
|
155
|
+
} catch(err) {
|
|
156
|
+
assert(err.code === 'missing-service')
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
// it("can detect 'missing-method' error", async () => {
|
|
161
|
+
// try {
|
|
162
|
+
// await app.service('User').dummyMethod({})
|
|
163
|
+
// } catch(err) {
|
|
164
|
+
// console.log('err', err)
|
|
165
|
+
// assert(err.code === 'missing-method')
|
|
166
|
+
// }
|
|
167
|
+
// })
|
|
168
|
+
|
|
169
|
+
})
|