Skip to content

Commit

Permalink
Merge pull request #259 from KxSystems/KXI-37284
Browse files Browse the repository at this point in the history
KXI-37284 - Additional Logging for Data Sources to help diagnose issues
  • Loading branch information
Philip-Carneiro-KX authored Mar 7, 2024
2 parents da9db0e + db13599 commit c05622c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/commands/dataSourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export async function runDataSource(
dataSourceForm.insightsNode = getConnectedInsightsNode();
const fileContent = dataSourceForm;

ext.outputChannel.appendLine(`Running ${fileContent.name} datasource...`);
let res: any;
const selectedType = getSelectedType(fileContent);
ext.isDatasourceExecution = true;
Expand All @@ -259,13 +260,19 @@ export async function runDataSource(
window.showErrorMessage(res.error);
addDStoQueryHistory(dataSourceForm, false);
} else if (ext.resultsViewProvider.isVisible()) {
ext.outputChannel.appendLine(
`Results: ${typeof res === "string" ? "0" : res.rows.length} rows`,
);
addDStoQueryHistory(dataSourceForm, true);
writeQueryResultsToView(
res,
getQuery(fileContent, selectedType),
selectedType,
);
} else {
ext.outputChannel.appendLine(
`Results is a string with length: ${res.length}`,
);
addDStoQueryHistory(dataSourceForm, true);
writeQueryResultsToConsole(
res,
Expand Down Expand Up @@ -477,6 +484,7 @@ function parseError(error: GetDataError) {
if (error instanceof Object && error.buffer) {
return handleWSError(error.buffer);
} else {
ext.outputChannel.appendLine(`Error: ${error}`);
return {
error,
};
Expand Down
6 changes: 5 additions & 1 deletion src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ export async function getMeta(): Promise<MetaObjectPayload | undefined> {
}
return undefined;
}

/* istanbul ignore next */
export async function getDataInsights(
targetUrl: string,
body: string,
Expand Down Expand Up @@ -408,6 +408,7 @@ export async function getDataInsights(

return await axios(options)
.then((response: any) => {
ext.outputChannel.appendLine(`request status: ${response.status}`);
if (isCompressed(response.data)) {
response.data = uncompress(response.data);
}
Expand All @@ -419,6 +420,9 @@ export async function getDataInsights(
};
})
.catch((error: any) => {
ext.outputChannel.appendLine(
`request status: ${error.response.status}`,
);
return {
error: { buffer: error.response.data },
arrayBuffer: undefined,
Expand Down
31 changes: 23 additions & 8 deletions src/utils/queryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,37 @@ export function handleWSError(ab: ArrayBuffer): any {
// last char is string terminator
raw[raw.byteLength - 1] === 0 &&
// error message size, message can be clipped (always less than 256 chars)
raw[5] * 256 + raw[4] === raw.byteLength &&
// 128 - start of string/error ?
raw.subarray(6, 9).toString() === "0,0,128"
raw[5] * 256 + raw[4] === raw.byteLength
// TODO: need to check if this is needed
// &&
// // 128 - start of string/error ?
// raw.subarray(6, 9).toString() === "0,0,128"
) {
errorString = {
// eslint-disable-next-line prefer-spread
ipc: String.fromCharCode.apply(
// eslint-disable-next-line prefer-spread
const translated = String.fromCharCode
.apply(
String,
raw.subarray(9, raw.byteLength - 1) as unknown as number[],
),
};
)
.split("\n")
.slice(-1);
errorString = translated.join("").trim();

// TODO: need to check if this is needed
// errorString = {
// // eslint-disable-next-line prefer-spread
// ipc: String.fromCharCode.apply(
// String,
// raw.subarray(9, raw.byteLength - 1) as unknown as number[],
// ),
// };
} else {
errorString = "Query error";
}
}

ext.outputChannel.appendLine(`Error : ${errorString}`);

return { error: errorString };
}

Expand Down
34 changes: 33 additions & 1 deletion test/suite/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
} from "../../src/ipc/cClasses";
import { DataSourceTypes } from "../../src/models/dataSource";
import { InsightDetails } from "../../src/models/insights";
import { Parse } from "../../src/ipc/parse.qlist";

interface ITestItem extends vscode.QuickPickItem {
id: number;
Expand Down Expand Up @@ -969,7 +970,30 @@ describe("Utils", () => {

describe("handleWSError", () => {
let sandbox: sinon.SinonSandbox;

const abTest = new Uint8Array([
1, 2, 0, 0, 114, 1, 0, 0, 0, 0, 2, 0, 0, 0, 99, 11, 0, 17, 0, 0, 0, 0,
114, 99, 118, 84, 83, 0, 99, 111, 114, 114, 0, 112, 114, 111, 116, 111,
99, 111, 108, 0, 108, 111, 103, 67, 111, 114, 114, 0, 99, 108, 105, 101,
110, 116, 0, 104, 116, 116, 112, 0, 97, 112, 105, 0, 117, 115, 101, 114,
78, 97, 109, 101, 0, 117, 115, 101, 114, 73, 68, 0, 116, 105, 109, 101,
111, 117, 116, 0, 116, 111, 0, 112, 118, 86, 101, 114, 0, 114, 101, 102,
86, 105, 110, 116, 97, 103, 101, 0, 114, 99, 0, 97, 99, 0, 97, 105, 0,
0, 0, 17, 0, 0, 0, 101, 0, 244, 0, 168, 200, 104, 217, 55, 151, 10, 254,
148, 230, 16, 238, 61, 245, 76, 4, 178, 72, 184, 185, 138, 80, 65, 216,
245, 103, 119, 0, 10, 0, 36, 0, 0, 0, 57, 52, 101, 54, 49, 48, 101, 101,
45, 51, 100, 102, 53, 45, 52, 99, 48, 52, 45, 98, 50, 52, 56, 45, 98,
56, 98, 57, 56, 97, 53, 48, 52, 49, 100, 56, 245, 58, 49, 48, 46, 57,
46, 49, 51, 56, 46, 49, 57, 50, 58, 53, 48, 53, 48, 0, 245, 111, 99,
116, 101, 116, 115, 116, 114, 101, 97, 109, 0, 245, 46, 107, 120, 105,
46, 113, 115, 113, 108, 0, 245, 0, 254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 249, 96, 234, 0, 0, 0, 0, 0, 0, 244, 0, 0, 16, 97, 231,
55, 151, 10, 249, 134, 0, 0, 0, 0, 0, 0, 0, 249, 1, 0, 0, 0, 0, 0, 0,
128, 251, 10, 0, 251, 10, 0, 10, 0, 54, 0, 0, 0, 85, 110, 101, 120, 112,
101, 99, 116, 101, 100, 32, 101, 114, 114, 111, 114, 32, 40, 110, 49,
48, 41, 32, 101, 110, 99, 111, 117, 110, 116, 101, 114, 101, 100, 32,
101, 120, 101, 99, 117, 116, 105, 110, 103, 32, 46, 107, 120, 105, 46,
113, 115, 113, 108, 0, 0, 0, 0, 0, 0,
]);
beforeEach(() => {
sandbox = sinon.createSandbox();
});
Expand Down Expand Up @@ -999,6 +1023,14 @@ describe("Utils", () => {
// Assert the result
assert.deepStrictEqual(result, { error: "Query error" });
});

it("should handle qe/sql error", () => {
const result = queryUtils.handleWSError(abTest);
assert.deepStrictEqual(result, {
error:
"\u00006\u0000\u0000\u0000Unexpected error (n10) encountered executing .kxi.qsql\u0000\u0000\u0000\u0000\u0000",
});
});
});
});

Expand Down

0 comments on commit c05622c

Please sign in to comment.