Skip to content

Commit 29d8399

Browse files
authored
fix: send response metadata even if error occurred (#337)
1 parent 53c8e96 commit 29d8399

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

lib/run.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ async function execDuplex (ctx, handler) {
7777

7878
function onerror (error, ctx) {
7979
ctx.app.emit('error', error, ctx)
80+
ctx.response.sendMetadata()
8081
}
8182

8283
function exec (ctx, handler, callback) {

test/metadata.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,61 @@ test.cb('req/res: header metadata set', t => {
135135
})
136136
})
137137

138+
test.cb('req/res: header metadata set even if error occurred', t => {
139+
t.plan(16)
140+
const APP_HOST = tu.getHost()
141+
142+
function sayHello (ctx) {
143+
ctx.set('foo', 'bar')
144+
throw Error('boom')
145+
}
146+
147+
const app = new Mali(PROTO_PATH, 'Greeter')
148+
t.truthy(app)
149+
150+
app.on('error', (err, _ctx) => {
151+
t.is(err.message, 'boom')
152+
})
153+
154+
app.use({ sayHello })
155+
app.start(APP_HOST).then(server => {
156+
t.truthy(server)
157+
158+
let metadata
159+
let status
160+
161+
const client = new helloproto.Greeter(APP_HOST, grpc.credentials.createInsecure())
162+
const call = client.sayHello({ name: 'Bob' }, (err, response) => {
163+
setTimeout(() => {
164+
t.truthy(err)
165+
t.true(err.message.indexOf('boom') >= 0)
166+
t.falsy(response)
167+
t.truthy(metadata)
168+
t.true(metadata instanceof grpc.Metadata)
169+
const header = metadata.getMap()
170+
t.is(header.foo, 'bar')
171+
t.is(header['content-type'], 'application/grpc+proto')
172+
t.truthy(header.date)
173+
t.truthy(status)
174+
t.true(typeof status.code === 'number')
175+
t.truthy(status.metadata)
176+
t.true(status.metadata instanceof grpc.Metadata)
177+
const trailer = status.metadata.getMap()
178+
t.deepEqual(trailer, {})
179+
app.close().then(() => t.end())
180+
}, 250)
181+
})
182+
183+
call.on('metadata', md => {
184+
metadata = md
185+
})
186+
187+
call.on('status', s => {
188+
status = s
189+
})
190+
})
191+
})
192+
138193
test.cb('req/res: header metadata sent using ctx.sendMetadata', t => {
139194
t.plan(15)
140195
const APP_HOST = tu.getHost()

0 commit comments

Comments
 (0)