+
+ // Call the request handler to build the response payload
+ responsePayload = await this.requestHandlers.get(command)(messageId, requestPayload);
+ } catch (error) {
+ // Log
+ logger.error(`${this.logPrefix(moduleName, 'messageHandler')} Handle request error:`, error);
+ responsePayload = {
+ status: ResponseStatus.FAILURE,
+ command,
+ requestPayload,
+ responsePayload,
+ errorMessage: (error as Error).message,
+ errorStack: (error as Error).stack,
+ };
+ }
+
+ if (responsePayload !== undefined) {
+ // Send the response
+ this.sendResponse(messageId ?? 'error', responsePayload);
+ }
+ }
+
+ public sendRequest(
+ messageId: string,
+ procedureName: ProcedureName,
+ requestPayload: RequestPayload
+ ): void {
+ this.uiServer.sendRequest(
+ this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload)
+ );
+ }
+
+ public sendResponse(messageId: string, responsePayload: ResponsePayload): void {
+ this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(messageId, responsePayload));
+ }
+
+ public logPrefix(modName: string, methodName: string): string {
+ return this.uiServer.logPrefix(modName, methodName);
+ }
+
+ // Validate the raw data received from the UI server
+ private requestValidation(rawData: RawData | JsonType): ProtocolRequest {
+ // logger.debug(
+ // `${this.logPrefix(
+ // moduleName,
+ // 'requestValidation'
+ // )} Data received in string format: ${rawData.toString()}`
+ // );
+
+ const data = JSON.parse(rawData.toString()) as JsonType[];
+
+ if (Array.isArray(data) === false) {
+ throw new BaseError('UI protocol request is not an array');
+ }
+
+ if (data.length !== 3) {
+ throw new BaseError('UI protocol request is malformed');