Error: VMContext Bot exception stopping Medplum se...
# support
u
Hello guys, I've added a bot(vm context) which is triggered when a Resource is created. Functionality: Bot creates a tcp connection(using Hl7Connection from @medplum/hl7) and sends a message to a listener. When tcp connection is refused it throws an unhandled ex. [ERROR] 13:58:15 Error: connect ECONNREFUSED host:PORT In this case server is stopped. I was wondering if this is an expected behavior or something that needs to be fixed. Thanks in advance.
r
Hi @96wutang - yes I think throwing an error if it cannot connect would be expected behavior. I'll note that connecting to an HL7 server from inside of a Bot is a pretty novel use case case, and there might be some rough edges. Do you mind sharing a bit more about your use case? ?
u
throwing an error if it cannot connect would be expected behavior
Yes but stopping the whole server .. that was my confusion. About the use case, when a resource is created I need to convert it to HL7v2 and synchronize with another system(using TCP/MLLP protocol). Sharing a code snippet that might be helpful aswell.
Copy code
export async function handler(medplum: MedplumClient, event: BotEvent): Promise<any> {
  const encounter = event.input as Encounter;
  
  const message : HL7Message = mapResource(encounter);
 
  const config : Hl7ClientOptions = { ... };
 
  const client : Hl7Client = new Hl7Client(config);
  await client.send(message);
}
r
Oh i see, the MEDPLUM server is stopped. Yes, VM context bots offer very limited isolation, are mainly useful for prototyping. We strongly recommend against using them for any produciton use case
For VM context bots, we recommend wrapping the bot code in a try/catch
u
Tried it, it didn't work 😦
The thing is even the wrapper (in thje execute.ts file) won't catch it. But thanks for heads up.
Copy code
const wrappedCode = `
  const exports = {};

  // Start user code
  ${code}
  // End user code

  (async () => {
    const { baseUrl, accessToken, contentType, secrets } = event;
    const medplum = new MedplumClient({
      baseUrl,
      fetch,
    });
    medplum.setAccessToken(accessToken);
    try {
      let input = event.input;
      if (contentType === ContentType.HL7_V2 && input) {
        input = Hl7Message.parse(input);
      }
      let result = await exports.handler(medplum, { input, contentType, secrets });
      if (contentType === ContentType.HL7_V2 && result) {
        result = result.toString();
      }
      return result;
    } catch (err) {
      if (err instanceof Error) {
        console.log("Unhandled error: " + err.message + "\\n" + err.stack);
      } else if (typeof err === "object") {
        console.log("Unhandled error: " + JSON.stringify(err, undefined, 2));
      } else {
        console.log("Unhandled error: " + err);
      }
      throw err;
    }
  })();
  `;
r
Thanks for bring it to our attention - we can do a deeper investigation and get back to you. Sorry about the inconvenience!
u
Thank you for your time sir, otherwise great platform!
170 Views