Skip to content

Commit e44c108

Browse files
authored
NXT-4107: hub features: fix rfcErrors parser and return undefined on error (#61)
* FileExplorer: disable options menu when multiple items are selected NXT-4107 (Hub Space Explorer: Move / Copy items to any target)
1 parent d422eb0 commit e44c108

File tree

8 files changed

+55
-42
lines changed

8 files changed

+55
-42
lines changed

.changeset/dirty-ends-pull.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@knime/components": patch
3+
---
4+
5+
FileExplorer: fix types of OnClickOutside ignores
6+
FileExplorer: disable options menu when multiple items are selected

.changeset/old-rocks-occur.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@knime/hub-features": patch
3+
---
4+
5+
fix rfcErrors parser and return undefined on error

packages/components/src/components/FileExplorer/components/FileExplorer.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ useResizeObserver(containerProps.ref, containerProps.onScroll);
693693
contextMenuAnchor?.openedBy === 'optionsMenu' &&
694694
contextMenuAnchor?.item.id === item.id
695695
"
696+
:disabled="selectedItems.length > 1"
696697
compact
697698
@dblclick.stop
698699
@pointerdown.stop

packages/hub-features/src/components/versions/composables/useVersionsApi.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FetchError, type FetchOptions } from "ofetch";
1+
import { type FetchOptions } from "ofetch";
22

33
import { type HubAvatarData, rfcErrors } from "@knime/hub-features";
44
import { VERSION_DEFAULT_LIMIT } from "@knime/hub-features/versions";
@@ -37,11 +37,7 @@ export const useVersionsApi = ({
3737

3838
return await $ofetch(path, { ...defaults, ...fetchOptions });
3939
} catch (error) {
40-
if (error instanceof FetchError) {
41-
throw rfcErrors.tryParse(error);
42-
}
43-
44-
throw error;
40+
throw rfcErrors.tryParse(error) ?? error;
4541
}
4642
};
4743

packages/hub-features/src/rfcErrors/__tests__/index.test.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import type { FetchError } from "ofetch";
2+
import { FetchError } from "ofetch";
33

44
import { rfcErrors } from "..";
55
import { RFCError } from "../types";
@@ -46,16 +46,15 @@ describe("rfcErrors", () => {
4646
response.headers.set("x-request-id", "request-id");
4747
response.headers.set("x-error-id", "error-id");
4848

49-
const fetchError: FetchError = {
50-
data: {
51-
title: "The title",
52-
details: ["detail 1"],
53-
},
54-
statusCode: 400,
55-
response,
56-
name: "",
57-
message: "",
49+
const fetchError = new FetchError("");
50+
fetchError.data = {
51+
title: "The title",
52+
details: ["detail 1"],
5853
};
54+
fetchError.statusCode = 400;
55+
fetchError.response = response;
56+
fetchError.name = "";
57+
fetchError.message = "";
5958

6059
const expectedError = new RFCError({
6160
title: "The title",
@@ -71,21 +70,27 @@ describe("rfcErrors", () => {
7170
expect(parsedError).toEqual(expectedError);
7271
});
7372

74-
it("should NOT parse into RFCError", () => {
75-
const fetchError: FetchError = {
76-
data: {
77-
title: "The title",
78-
details: ["detail 1"],
79-
},
80-
statusCode: 400,
81-
name: "",
82-
message: "",
73+
it("should NOT parse into RFCError if response is missing", () => {
74+
const fetchError = new FetchError("");
75+
fetchError.data = {
76+
title: "The title",
77+
details: ["detail 1"],
8378
};
79+
fetchError.statusCode = 400;
80+
fetchError.name = "";
81+
fetchError.message = "";
8482

8583
const parsedError = rfcErrors.tryParse(fetchError);
8684

87-
expect(parsedError instanceof rfcErrors.RFCError).toBe(false);
88-
expect(parsedError).toBe(fetchError);
85+
expect(parsedError).toBeUndefined();
86+
});
87+
88+
it("should NOT parse into RFCError if no FetchError is given", () => {
89+
const error = new Error("");
90+
91+
const parsedError = rfcErrors.tryParse(error);
92+
93+
expect(parsedError).toBeUndefined();
8994
});
9095
});
9196
});

packages/hub-features/src/rfcErrors/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,28 @@ const toToast = ({
3535
};
3636

3737
/**
38-
* Try to parse an ofetch's `FetchError` to an `RFCError`. When parsing is not possible
39-
* it returns the same `FetchError` given unchanged.
38+
* Parse an ofetch's `FetchError` to an `RFCError`.
39+
* When parsing is not possible it returns undefined.
4040
*/
41-
const tryParse = (error: FetchError): RFCError | FetchError => {
41+
const tryParse = (error: unknown): RFCError | undefined => {
42+
if (!(error instanceof FetchError)) {
43+
return undefined;
44+
}
45+
4246
if (!error.response) {
43-
return error;
47+
return undefined;
4448
}
4549

4650
const responseDate = error.response.headers.get("date")
4751
? new Date(error.response.headers.get("date")!)
4852
: undefined;
4953

54+
const title =
55+
typeof error.data === "string" ? error.data : error.data?.title ?? "";
56+
5057
const rfcErrorData: RFCErrorData = {
51-
title: error.data.title as string,
52-
details: error.data.details as string[] | undefined,
58+
title: title as string,
59+
details: error.data?.details as string[] | undefined,
5360
status: error.statusCode as number,
5461
date: responseDate,
5562
requestId: error.response.headers.get("x-request-id") ?? undefined,

packages/hub-features/src/useDownloadArtifact/useDownloadArtifact.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,7 @@ export const useDownloadArtifact = (
284284
} catch (error: unknown) {
285285
// here only errors thrown by the initial requests are caught;
286286
// errors occurring while polling will be handled in pollDownloadItem
287-
if (error instanceof FetchError) {
288-
throw rfcErrors.tryParse(error);
289-
}
290-
throw error;
287+
throw rfcErrors.tryParse(error) ?? error;
291288
}
292289
};
293290

packages/hub-features/src/useFileUpload/useFileUpload.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,7 @@ export const useFileUpload = (options: UseFileUploadOptions = {}) => {
287287

288288
useUploadManagerResult.start(parentId, uploadPayload);
289289
} catch (error) {
290-
if (error instanceof FetchError) {
291-
throw rfcErrors.tryParse(error);
292-
}
293-
294-
throw error;
290+
throw rfcErrors.tryParse(error) ?? error;
295291
} finally {
296292
// errors can only be thrown in the prepareUpload call, useUploadManagerResult.start does its own error handling
297293
prepareQueueSize.value -= enqueableFiles.length;

0 commit comments

Comments
 (0)