Skip to content

Commit b875f02

Browse files
Merge pull request #543 from KxSystems/KXI-58254-errors
add error workarounds
2 parents 5aa731f + a9ebe2e commit b875f02

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

src/classes/insightsConnection.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,25 @@ export class InsightsConnection {
524524
return token;
525525
}
526526

527+
public async isUDAAvailable(udaName: string): Promise<boolean> {
528+
if (!this.meta || !this.meta.payload.api) {
529+
return false;
530+
}
531+
532+
if (!this.connected || !this.connEndpoints) {
533+
return false;
534+
}
535+
536+
if (this.meta.payload.api && Array.isArray(this.meta.payload.api)) {
537+
return this.meta.payload.api.some(
538+
(apiItem: { api: string; custom: boolean }) =>
539+
apiItem.api === udaName && apiItem.custom === true,
540+
);
541+
}
542+
543+
return false;
544+
}
545+
527546
public async getUDAScratchpadQuery(
528547
udaReqBody: UDARequestBody,
529548
): Promise<any | undefined> {

src/commands/dataSourceCommand.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,26 +456,38 @@ export async function runUDADataSource(
456456
): Promise<any> {
457457
const UDA = fileContent.dataSource.uda;
458458
const returnFormat = ext.isResultsTabVisible ? "structuredText" : "text";
459+
459460
if (!UDA) {
460461
return { error: "UDA not found" };
461462
}
463+
464+
const isAvailable = await selectedConn.isUDAAvailable(UDA.name);
465+
462466
if (UDA.name === "") {
463467
return { error: "UDA name not found" };
464468
}
465469

470+
if (!isAvailable) {
471+
return { error: `UDA ${UDA.name} is not available in this connection` };
472+
}
473+
466474
const params: { [key: string]: any } = {};
467475
const parameterTypes: { [key: string]: any } = {};
468476

469477
if (UDA.params && UDA.params.length > 0) {
470-
UDA.params.forEach((param) => {
478+
for (const param of UDA.params) {
479+
if (param.isReq && (!param.value || param.value === "")) {
480+
return { error: `The UDA required the parameter ${param.name}.` };
481+
}
471482
if (param.isVisible) {
472483
params[param.name] = param.value;
473484
parameterTypes[param.name] = param.selectedMultiTypeString
474485
? retrieveDataTypeByString(param.selectedMultiTypeString)
475486
: param.type;
476487
}
477-
});
488+
}
478489
}
490+
479491
const udaReqBody: UDARequestBody = {
480492
language: "q",
481493
name: UDA.name,

test/suite/commands.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,14 @@ describe("dataSourceCommand2", () => {
530530
let getDataInsightsStub,
531531
handleWSResultsStub,
532532
handleScratchpadTableRes,
533+
isUDAAvailableStub,
533534
parseErrorStub: sinon.SinonStub;
534535

535536
beforeEach(() => {
536537
getDataInsightsStub = sinon.stub(insightsConn, "getDatasourceQuery");
537538
handleWSResultsStub = sinon.stub(queryUtils, "handleWSResults");
538539
parseErrorStub = sinon.stub(dataSourceCommand, "parseError");
540+
isUDAAvailableStub = sinon.stub(insightsConn, "isUDAAvailable");
539541
handleScratchpadTableRes = sinon.stub(
540542
queryUtils,
541543
"handleScratchpadTableRes",
@@ -547,6 +549,7 @@ describe("dataSourceCommand2", () => {
547549
});
548550

549551
it("should call the API and handle the results", async () => {
552+
isUDAAvailableStub.resolves(true);
550553
getDataInsightsStub.resolves({ arrayBuffer: true });
551554
handleWSResultsStub.resolves([
552555
{ a: "2", b: "3" },
@@ -574,6 +577,7 @@ describe("dataSourceCommand2", () => {
574577
});
575578

576579
it("should call the API and handle the error results", async () => {
580+
isUDAAvailableStub.resolves(true);
577581
getDataInsightsStub.resolves({ error: "error test" });
578582
parseErrorStub.resolves({ error: "error test" });
579583

@@ -586,6 +590,7 @@ describe("dataSourceCommand2", () => {
586590
});
587591

588592
it("should call the API and handle undefined response ", async () => {
593+
isUDAAvailableStub.resolves(true);
589594
getDataInsightsStub.resolves(undefined);
590595

591596
const result = await dataSourceCommand.runUDADataSource(
@@ -596,6 +601,46 @@ describe("dataSourceCommand2", () => {
596601
assert.deepStrictEqual(result, { error: "UDA call failed" });
597602
});
598603

604+
it("should handle if the UDA doesn't exist in the connection", async () => {
605+
isUDAAvailableStub.resolves(false);
606+
getDataInsightsStub.resolves(undefined);
607+
608+
const result = await dataSourceCommand.runUDADataSource(
609+
dummyDataSourceFiles,
610+
insightsConn,
611+
);
612+
613+
assert.deepStrictEqual(result, {
614+
error: "UDA test query is not available in this connection",
615+
});
616+
});
617+
618+
it("should handle if a required param is empty", async () => {
619+
isUDAAvailableStub.resolves(true);
620+
getDataInsightsStub.resolves(undefined);
621+
622+
const dummyDSFiles2 = dummyDataSourceFiles;
623+
dummyDSFiles2.dataSource.uda.params = [
624+
{
625+
name: "param1",
626+
description: "test param",
627+
default: "",
628+
isReq: true,
629+
type: [0],
630+
value: "",
631+
},
632+
];
633+
634+
const result = await dataSourceCommand.runUDADataSource(
635+
dummyDSFiles2,
636+
insightsConn,
637+
);
638+
639+
assert.deepStrictEqual(result, {
640+
error: "The UDA required the parameter param1.",
641+
});
642+
});
643+
599644
it("should handle undefined UDA ", async () => {
600645
dummyDataSourceFiles.dataSource.uda = undefined;
601646

0 commit comments

Comments
 (0)