| @@ -61,4 +61,50 @@ describe('app.currentContext', () => { |
|
|
| 61 |
61 |
|
| 62 |
62 |
await request(app.callback()).get('/').expect('ok') |
| 63 |
63 |
}) |
|
64 |
+ |
|
65 |
+it('should get currentContext return context in error handler when asyncLocalStorage enable', async () => { |
|
66 |
+const app = new Koa({ asyncLocalStorage: true }) |
|
67 |
+ |
|
68 |
+app.use(async () => { |
|
69 |
+throw new Error('error message') |
|
70 |
+}) |
|
71 |
+ |
|
72 |
+const handleError = new Promise((resolve, reject) => { |
|
73 |
+app.on('error', (err, ctx) => { |
|
74 |
+try { |
|
75 |
+assert.strictEqual(err.message, 'error message') |
|
76 |
+assert.strictEqual(app.currentContext, ctx) |
|
77 |
+resolve() |
|
78 |
+} catch (e) { |
|
79 |
+reject(e) |
|
80 |
+} |
|
81 |
+}) |
|
82 |
+}) |
|
83 |
+ |
|
84 |
+await request(app.callback()).get('/').expect('Internal Server Error') |
|
85 |
+await handleError |
|
86 |
+}) |
|
87 |
+ |
|
88 |
+it('should get currentContext return undefined in error handler when asyncLocalStorage disable', async () => { |
|
89 |
+const app = new Koa() |
|
90 |
+ |
|
91 |
+app.use(async () => { |
|
92 |
+throw new Error('error message') |
|
93 |
+}) |
|
94 |
+ |
|
95 |
+const handleError = new Promise((resolve, reject) => { |
|
96 |
+app.on('error', (err, ctx) => { |
|
97 |
+try { |
|
98 |
+assert.strictEqual(err.message, 'error message') |
|
99 |
+assert.strictEqual(app.currentContext, undefined) |
|
100 |
+resolve() |
|
101 |
+} catch (e) { |
|
102 |
+reject(e) |
|
103 |
+} |
|
104 |
+}) |
|
105 |
+}) |
|
106 |
+ |
|
107 |
+await request(app.callback()).get('/').expect('Internal Server Error') |
|
108 |
+await handleError |
|
109 |
+}) |
| 64 |
110 |
}) |