Skip to content

Commit

Permalink
typescript: Add sendServiceCallFailure to server impl (#735)
Browse files Browse the repository at this point in the history
### Changelog
Typescript: Add function `sendServiceCallFailure` to send service call
failure to client

### Description
Forgot to add that in #733
  • Loading branch information
achim-k authored Apr 24, 2024
1 parent aa7d846 commit f699ac6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
24 changes: 22 additions & 2 deletions typescript/ws-protocol-examples/src/examples/service-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,34 @@ async function main(): Promise<void> {
server.on("serviceCallRequest", (request, clientConnection) => {
const service = serviceById.get(request.serviceId);
if (!service) {
throw new Error(`Received invalid serviceId: "${request.serviceId}"`);
const err = new Error(`Received invalid serviceId: "${request.serviceId}"`);
server.sendServiceCallFailure(
{
op: "serviceCallFailure",
serviceId: request.serviceId,
callId: request.callId,
message: err.message,
},
clientConnection,
);
throw err;
}
if (service.request!.encoding !== request.encoding) {
throw new Error(
const err = new Error(
`Service ${service.name} called with invalid message encoding. Expected ${
service.request!.encoding
}, got ${request.encoding}`,
);
server.sendServiceCallFailure(
{
op: "serviceCallFailure",
serviceId: request.serviceId,
callId: request.callId,
message: err.message,
},
clientConnection,
);
throw err;
}

log("Received service call request with %d bytes", request.data.byteLength);
Expand Down
10 changes: 10 additions & 0 deletions typescript/ws-protocol/src/FoxgloveServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ServerCapability,
ServerMessage,
Service,
ServiceCallFailure,
ServiceCallPayload,
ServiceCallRequest,
ServiceId,
Expand Down Expand Up @@ -273,6 +274,15 @@ export default class FoxgloveServer {
connection.send(payload);
}

/**
* Send a service call failure response to the client
* @param response Response to send to the client
* @param connection Connection of the client that called the service
*/
sendServiceCallFailure(response: ServiceCallFailure, connection: IWebSocket): void {
this.#send(connection, response);
}

/**
* Publish parameter values.
* @param parameters Parameter values
Expand Down

0 comments on commit f699ac6

Please sign in to comment.