Skip to content

Commit

Permalink
fix: send response metadata even if error occurred (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanziw authored Aug 21, 2022
1 parent 53c8e96 commit 29d8399
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ async function execDuplex (ctx, handler) {

function onerror (error, ctx) {
ctx.app.emit('error', error, ctx)
ctx.response.sendMetadata()
}

function exec (ctx, handler, callback) {
Expand Down
55 changes: 55 additions & 0 deletions test/metadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,61 @@ test.cb('req/res: header metadata set', t => {
})
})

test.cb('req/res: header metadata set even if error occurred', t => {
t.plan(16)
const APP_HOST = tu.getHost()

function sayHello (ctx) {
ctx.set('foo', 'bar')
throw Error('boom')
}

const app = new Mali(PROTO_PATH, 'Greeter')
t.truthy(app)

app.on('error', (err, _ctx) => {
t.is(err.message, 'boom')
})

app.use({ sayHello })
app.start(APP_HOST).then(server => {
t.truthy(server)

let metadata
let status

const client = new helloproto.Greeter(APP_HOST, grpc.credentials.createInsecure())
const call = client.sayHello({ name: 'Bob' }, (err, response) => {
setTimeout(() => {
t.truthy(err)
t.true(err.message.indexOf('boom') >= 0)
t.falsy(response)
t.truthy(metadata)
t.true(metadata instanceof grpc.Metadata)
const header = metadata.getMap()
t.is(header.foo, 'bar')
t.is(header['content-type'], 'application/grpc+proto')
t.truthy(header.date)
t.truthy(status)
t.true(typeof status.code === 'number')
t.truthy(status.metadata)
t.true(status.metadata instanceof grpc.Metadata)
const trailer = status.metadata.getMap()
t.deepEqual(trailer, {})
app.close().then(() => t.end())
}, 250)
})

call.on('metadata', md => {
metadata = md
})

call.on('status', s => {
status = s
})
})
})

test.cb('req/res: header metadata sent using ctx.sendMetadata', t => {
t.plan(15)
const APP_HOST = tu.getHost()
Expand Down

0 comments on commit 29d8399

Please sign in to comment.