mirror of https://github.com/grpc/grpc-node.git
Merge pull request #1270 from murgatroid99/unimplemented_message_improvement
Include method name in UNIMPLEMENTED details string
This commit is contained in:
commit
aef71e9e39
|
|
@ -49,11 +49,15 @@ import { ChannelOptions } from './channel-options';
|
||||||
|
|
||||||
function noop(): void {}
|
function noop(): void {}
|
||||||
|
|
||||||
const unimplementedStatusResponse: Partial<ServiceError> = {
|
function getUnimplementedStatusResponse(
|
||||||
code: Status.UNIMPLEMENTED,
|
methodName: string
|
||||||
details: 'The server does not implement this method',
|
): Partial<ServiceError> {
|
||||||
metadata: new Metadata(),
|
return {
|
||||||
};
|
code: Status.UNIMPLEMENTED,
|
||||||
|
details: `The server does not implement the method ${methodName}`,
|
||||||
|
metadata: new Metadata(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// tslint:disable:no-any
|
// tslint:disable:no-any
|
||||||
type UntypedUnaryHandler = UnaryHandler<any, any>;
|
type UntypedUnaryHandler = UnaryHandler<any, any>;
|
||||||
|
|
@ -66,23 +70,37 @@ export interface UntypedServiceImplementation {
|
||||||
[name: string]: UntypedHandleCall;
|
[name: string]: UntypedHandleCall;
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultHandler = {
|
function getDefaultHandler(handlerType: HandlerType, methodName: string) {
|
||||||
unary(call: ServerUnaryCall<any, any>, callback: sendUnaryData<any>): void {
|
const unimplementedStatusResponse = getUnimplementedStatusResponse(
|
||||||
callback(unimplementedStatusResponse as ServiceError, null);
|
methodName
|
||||||
},
|
);
|
||||||
clientStream(
|
switch (handlerType) {
|
||||||
call: ServerReadableStream<any, any>,
|
case 'unary':
|
||||||
callback: sendUnaryData<any>
|
return (
|
||||||
): void {
|
call: ServerUnaryCall<any, any>,
|
||||||
callback(unimplementedStatusResponse as ServiceError, null);
|
callback: sendUnaryData<any>
|
||||||
},
|
) => {
|
||||||
serverStream(call: ServerWritableStream<any, any>): void {
|
callback(unimplementedStatusResponse as ServiceError, null);
|
||||||
call.emit('error', unimplementedStatusResponse);
|
};
|
||||||
},
|
case 'clientStream':
|
||||||
bidi(call: ServerDuplexStream<any, any>): void {
|
return (
|
||||||
call.emit('error', unimplementedStatusResponse);
|
call: ServerReadableStream<any, any>,
|
||||||
},
|
callback: sendUnaryData<any>
|
||||||
};
|
) => {
|
||||||
|
callback(unimplementedStatusResponse as ServiceError, null);
|
||||||
|
};
|
||||||
|
case 'serverStream':
|
||||||
|
return (call: ServerWritableStream<any, any>) => {
|
||||||
|
call.emit('error', unimplementedStatusResponse);
|
||||||
|
};
|
||||||
|
case 'bidi':
|
||||||
|
return (call: ServerDuplexStream<any, any>) => {
|
||||||
|
call.emit('error', unimplementedStatusResponse);
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
throw new Error(`Invalid handlerType ${handlerType}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
// tslint:enable:no-any
|
// tslint:enable:no-any
|
||||||
|
|
||||||
export class Server {
|
export class Server {
|
||||||
|
|
@ -157,7 +175,7 @@ export class Server {
|
||||||
if (implFn !== undefined) {
|
if (implFn !== undefined) {
|
||||||
impl = implFn.bind(implementation);
|
impl = implFn.bind(implementation);
|
||||||
} else {
|
} else {
|
||||||
impl = defaultHandler[methodType];
|
impl = getDefaultHandler(methodType, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
const success = this.register(
|
const success = this.register(
|
||||||
|
|
@ -353,7 +371,7 @@ export class Server {
|
||||||
const handler = this.handlers.get(path);
|
const handler = this.handlers.get(path);
|
||||||
|
|
||||||
if (handler === undefined) {
|
if (handler === undefined) {
|
||||||
throw unimplementedStatusResponse;
|
throw getUnimplementedStatusResponse(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
const call = new Http2ServerCallStream(stream, handler);
|
const call = new Http2ServerCallStream(stream, handler);
|
||||||
|
|
|
||||||
|
|
@ -843,25 +843,34 @@ Server.prototype.forceShutdown = function() {
|
||||||
this._server.forceShutdown();
|
this._server.forceShutdown();
|
||||||
};
|
};
|
||||||
|
|
||||||
var unimplementedStatusResponse = {
|
function getUnimplementedStatusResponse(methodName) {
|
||||||
code: constants.status.UNIMPLEMENTED,
|
return {
|
||||||
details: 'The server does not implement this method'
|
code: constants.status.UNIMPLEMENTED,
|
||||||
};
|
details: 'The server does not implement the method' + methodName
|
||||||
|
|
||||||
var defaultHandler = {
|
|
||||||
unary: function(call, callback) {
|
|
||||||
callback(unimplementedStatusResponse);
|
|
||||||
},
|
|
||||||
client_stream: function(call, callback) {
|
|
||||||
callback(unimplementedStatusResponse);
|
|
||||||
},
|
|
||||||
server_stream: function(call) {
|
|
||||||
call.emit('error', unimplementedStatusResponse);
|
|
||||||
},
|
|
||||||
bidi: function(call) {
|
|
||||||
call.emit('error', unimplementedStatusResponse);
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
function getDefaultHandler(handlerType, methodName) {
|
||||||
|
const unimplementedStatusResponse = getUnimplementedStatusResponse(methodName);
|
||||||
|
switch(handlerType) {
|
||||||
|
case 'unary':
|
||||||
|
return (call, callback) => {
|
||||||
|
callback(unimplementedStatusResponse, null);
|
||||||
|
};
|
||||||
|
case 'client_stream':
|
||||||
|
return (call,callback) => {
|
||||||
|
callback(unimplementedStatusResponse, null);
|
||||||
|
};
|
||||||
|
case 'server_stream':
|
||||||
|
return (call) => {
|
||||||
|
call.emit('error', unimplementedStatusResponse);
|
||||||
|
}
|
||||||
|
case 'bidi':
|
||||||
|
return (call) => {
|
||||||
|
call.emit('error', unimplementedStatusResponse);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function isObject(thing) {
|
function isObject(thing) {
|
||||||
return (typeof thing === 'object' || typeof thing === 'function') && thing !== null;
|
return (typeof thing === 'object' || typeof thing === 'function') && thing !== null;
|
||||||
|
|
@ -908,7 +917,7 @@ Server.prototype.addService = function(service, implementation) {
|
||||||
if (implementation[attrs.originalName] === undefined) {
|
if (implementation[attrs.originalName] === undefined) {
|
||||||
common.log(constants.logVerbosity.ERROR, 'Method handler ' + name +
|
common.log(constants.logVerbosity.ERROR, 'Method handler ' + name +
|
||||||
' for ' + attrs.path + ' expected but not provided');
|
' for ' + attrs.path + ' expected but not provided');
|
||||||
impl = defaultHandler[method_type];
|
impl = getDefaultHandler(method_type, name);
|
||||||
} else {
|
} else {
|
||||||
impl = implementation[attrs.originalName].bind(implementation);
|
impl = implementation[attrs.originalName].bind(implementation);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue