Catch empty string error is parseKubeApi (#3358)

* Catch empty string error is parseKubeApi

- Add more debugging lines

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add some unit tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Use resolved pathname

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-07-14 08:48:32 +03:00
1 parent 2059034eab
commit f2cd0aa8e8
4 files changed
+54 -11

No files matched your search

@@ -129,8 +129,18 @@ const tests: KubeApiParseTestData[] = [
}],
];
const throwtests = [
undefined,
"",
"ajklsmh"
];
describe("parseApi unit tests", () => {
it.each(tests)("testing %s", (url, expected) => {
it.each(tests)("testing %j", (url, expected) => {
expect(parseKubeApi(url)).toStrictEqual(expected);
});
it.each(throwtests)("testing %j should throw", (url) => {
expect(() => parseKubeApi(url)).toThrowError("invalid apiPath");
});
});
+8 -2
View File
@@ -58,8 +58,14 @@ export class ApiManager {
}
}
protected resolveApi(api: string | KubeApi): KubeApi {
if (typeof api === "string") return this.getApi(api);
protected resolveApi(api?: string | KubeApi): KubeApi | undefined {
if (!api) {
return undefined;
}
if (typeof api === "string") {
return this.getApi(api);
}
return api;
}
+27 -4
View File
@@ -24,6 +24,9 @@
import type { KubeObject } from "./kube-object";
import { splitArray } from "../../common/utils";
import { apiManager } from "./api-manager";
import { isDebugging } from "../../common/vars";
import logger from "../../main/logger";
import { inspect } from "util";
export interface IKubeObjectRef {
kind: string;
@@ -47,8 +50,26 @@ export interface IKubeApiParsed extends IKubeApiLinkRef {
}
export function parseKubeApi(path: string): IKubeApiParsed {
path = new URL(path, location.origin).pathname;
const [, prefix, ...parts] = path.split("/");
if (!isDebugging) {
return _parseKubeApi(path);
}
try {
const res = _parseKubeApi(path);
logger.debug(`parseKubeApi(${inspect(path, false, null, false)}) -> ${inspect(res, false, null, false)}`);
return res;
} catch (error) {
logger.debug(`parseKubeApi(${inspect(path, false, null, false)}) threw: ${error}`);
throw error;
}
}
function _parseKubeApi(path: string): IKubeApiParsed {
const apiPath = new URL(path, location.origin).pathname;
const [, prefix, ...parts] = apiPath.split("/");
const apiPrefix = `/${prefix}`;
const [left, right, namespaced] = splitArray(parts, "namespaces");
let apiGroup, apiVersion, namespace, resource, name;
@@ -70,12 +91,14 @@ export function parseKubeApi(path: string): IKubeApiParsed {
apiGroup = left.join("/");
} else {
switch (left.length) {
case 0:
throw new Error(`invalid apiPath: ${apiPath}`);
case 4:
[apiGroup, apiVersion, resource, name] = left;
break;
case 2:
resource = left.pop();
// fallthrough
// fallthrough
case 1:
apiVersion = left.pop();
apiGroup = "";
@@ -113,7 +136,7 @@ export function parseKubeApi(path: string): IKubeApiParsed {
const apiBase = [apiPrefix, apiGroup, apiVersion, resource].filter(v => v).join("/");
if (!apiBase) {
throw new Error(`invalid apiPath: ${path}`);
throw new Error(`invalid apiPath: ${apiPath}`);
}
return {
@@ -34,6 +34,7 @@ import { CrdResourceDetails } from "../+custom-resources";
import { KubeObjectMenu } from "./kube-object-menu";
import type { CustomResourceDefinition } from "../../api/endpoints";
import { KubeObjectDetailRegistry } from "../../api/kube-object-detail-registry";
import logger from "../../../main/logger";
/**
* Used to store `object.selfLink` to show more info about resource in the details panel.
@@ -67,6 +68,7 @@ export function hideDetails() {
}
export function getDetailsUrl(selfLink: string, resetSelected = false, mergeGlobals = true) {
logger.debug("getDetailsUrl", { selfLink, resetSelected, mergeGlobals });
const params = new URLSearchParams(mergeGlobals ? navigation.searchParams : "");
params.set(kubeDetailsUrlParam.name, selfLink);
@@ -100,10 +102,12 @@ export class KubeObjectDetails extends React.Component {
}
@computed get object() {
const store = apiManager.getStore(this.path);
if (store) {
return store.getByPath(this.path);
try {
return apiManager
.getStore(this.path)
?.getByPath(this.path);
} catch (error) {
logger.error(`[KUBE-OBJECT-DETAILS]: failed to get store or object: ${error}`, { path: this.path });
}
}