From 812a592a5d3b80fefbad40cafa062e1c96a7ea39 Mon Sep 17 00:00:00 2001 From: Matt Fulp <8397318+fulpm@users.noreply.github.com> Date: Wed, 8 May 2024 14:34:16 -0400 Subject: [PATCH] 20106: Handle tuple response from engine --- src/client/wasm/core.ts | 51 ++++++++++++++++++---------------------- src/client/wasm/index.ts | 2 +- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/client/wasm/core.ts b/src/client/wasm/core.ts index d941e8d..2aab118 100644 --- a/src/client/wasm/core.ts +++ b/src/client/wasm/core.ts @@ -9,21 +9,10 @@ export class AmalgamCoreError extends AmalgamError { } } -export class AmalgamCoreWarning { - constructor( - public readonly detail: string = "", - public readonly code?: string, - ) {} - - get message(): string { - return this.detail; - } -} - export interface AmalgamCoreResponse { content: R; errors: AmalgamCoreError[]; - warnings: AmalgamCoreWarning[]; + warnings: string[]; } /** @@ -63,33 +52,39 @@ export function prepareCoreResponse( throw new AmalgamError("Null or empty response received from core."); } - if (data?.constructor == Object) { + if (Array.isArray(data) && data.length == 2) { const errors: AmalgamCoreError[] = []; - const warnings: AmalgamCoreWarning[] = []; + const warnings: string[] = []; + const isSuccess = data[0]; + const value = data[1]; - // Collect warnings - if (data.warnings?.length > 0) { - for (const w of data.warnings) { - warnings.push(new AmalgamCoreWarning(w?.detail, w?.code)); + if (isSuccess) { + // Collect warnings + if (Array.isArray(value?.warnings)) { + for (const msg of value.warnings) { + if (msg != null) warnings.push(msg); + } } - } - - // Collect errors - if (data.status !== "ok") { - if (data.errors?.length > 0) { - for (const e of data.errors) { - errors.push(new AmalgamCoreError(e?.detail, e?.code)); + } else { + // Collect errors + if (value?.detail) { + if (Array.isArray(value.detail)) { + for (const msg of value.detail) { + errors.push(new AmalgamCoreError(msg, value.code)); + } + } else { + errors.push(new AmalgamCoreError(value.detail, value.code)); } - } else { + } + if (errors.length == 0) { errors.push(new AmalgamCoreError("An unknown error occurred.")); } - return { errors, warnings, content: data.payload }; } return { errors, warnings, - content: data.payload, + content: isSuccess ? value?.payload : undefined, }; } else if (["string", "number", "bigint", "boolean"].indexOf(typeof data) != -1) { return { errors: [], warnings: [], content: data }; diff --git a/src/client/wasm/index.ts b/src/client/wasm/index.ts index 2e86301..66e0e1e 100644 --- a/src/client/wasm/index.ts +++ b/src/client/wasm/index.ts @@ -2,4 +2,4 @@ export * from "./client.js"; export * from "./files.js"; export * from "../errors.js"; export * from "../../trainees/index.js"; -export { AmalgamCoreError, AmalgamCoreWarning, AmalgamCoreResponse } from "./core.js"; +export { AmalgamCoreError, AmalgamCoreResponse } from "./core.js";