Merge pull request #1195 from ortoo/master

grpc-js: fix error messages truncating at commas
This commit is contained in:
Michael Lumish 2019-11-21 15:31:07 -08:00 committed by GitHub
commit 9ec428bdd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 4 deletions

View File

@ -36,6 +36,10 @@ function isBinaryKey(key: string): boolean {
return key.endsWith('-bin');
}
function isCustomMetadata(key: string): boolean {
return !key.startsWith('grpc-');
}
function normalizeKey(key: string): string {
return key.toLowerCase();
}
@ -260,9 +264,13 @@ export class Metadata {
result.add(key, Buffer.from(value, 'base64'));
});
} else if (values !== undefined) {
values.split(',').forEach(v => {
result.add(key, Buffer.from(v.trim(), 'base64'));
});
if (isCustomMetadata(key)) {
values.split(',').forEach(v => {
result.add(key, Buffer.from(v.trim(), 'base64'));
});
} else {
result.add(key, Buffer.from(values, 'base64'));
}
}
} else {
if (Array.isArray(values)) {
@ -270,7 +278,11 @@ export class Metadata {
result.add(key, value);
});
} else if (values !== undefined) {
values.split(',').forEach(v => result.add(key, v.trim()));
if (isCustomMetadata(key)) {
values.split(',').forEach(v => result.add(key, v.trim()));
} else {
result.add(key, values);
}
}
}
} catch (error) {

View File

@ -699,6 +699,18 @@ describe('Other conditions', () => {
}
);
});
it('for an error message with a comma', done => {
client.unary(
{ error: true, message: 'an error message, with a comma' },
(err: ServiceError, data: any) => {
assert(err);
assert.strictEqual(err.code, grpc.status.UNKNOWN);
assert.strictEqual(err.details, 'an error message, with a comma');
done();
}
);
});
});
});

View File

@ -553,6 +553,14 @@ describe(`${anyGrpc.clientName} client -> ${anyGrpc.serverName} server`, functio
done();
});
});
it('for an error message with a comma', function(done) {
client.unary({error: true, message: 'a message, with a comma'}, function(err, data) {
assert(err);
assert.strictEqual(err.code, clientGrpc.status.UNKNOWN);
assert.strictEqual(err.details, 'a message, with a comma');
done();
});
});
});
});
});