@kumori/aurora-backend-handler 1.1.85 → 1.1.87

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,6 +16,35 @@ interface DregistryBody {
16
16
  docker_password: string;
17
17
  };
18
18
  }
19
+
20
+ /**
21
+ * Extracts a user-facing code/content pair from a failed request.
22
+ * Errors coming back from the WS layer are usually nested under `.error`
23
+ * (e.g. `{ error: { code, content } }`), but some server-side rejections
24
+ * (e.g. plan/limits validation) return the fields flat on the error object
25
+ * itself (`{ code, content }`). Without this fallback those errors were
26
+ * silently reported as "Unknown error" instead of surfacing the real code.
27
+ */
28
+ const extractWsErrorInfo = (
29
+ err: unknown
30
+ ): { code: string; content: string } => {
31
+ const candidate =
32
+ err && typeof err === "object" && "error" in err
33
+ ? (err as any).error
34
+ : err;
35
+
36
+ const code =
37
+ candidate && typeof candidate === "object" && typeof candidate.code === "string"
38
+ ? candidate.code
39
+ : "Unknown error";
40
+ const content =
41
+ candidate && typeof candidate === "object" && typeof candidate.content === "string"
42
+ ? candidate.content
43
+ : "Unknown error";
44
+
45
+ return { code, content };
46
+ };
47
+
19
48
  /**
20
49
  * Function to create a tenant using the global WebSocket connection
21
50
  * @param tenant Tenant object with the data of the tenant
@@ -79,23 +108,8 @@ export const createTenant = async (tenant: Tenant, security: Security) => {
79
108
  tenant: tenant.name,
80
109
  webSocketStatus: getWebSocketStatus(),
81
110
  });
82
- let contentMessage = "Unknown error";
83
- let errorContent = "Unknown error";
84
-
85
- if (
86
- typeof error === "object" &&
87
- error !== null &&
88
- "error" in error &&
89
- typeof (error as any).error === "object" &&
90
- (error as any).error !== null
91
- ) {
92
- if ("code" in (error as any).error) {
93
- contentMessage = (error as any).error.code;
94
- }
95
- if ("content" in (error as any).error) {
96
- errorContent = (error as any).error.content;
97
- }
98
- }
111
+ const { code: contentMessage, content: errorContent } =
112
+ extractWsErrorInfo(error);
99
113
  const tenantCreationErrorNotification: Notification = {
100
114
  type: "error",
101
115
  subtype: errors.tenant.creationError.subtype,
@@ -146,24 +160,8 @@ export const deleteTenant = async (tenant: Tenant, security: string) => {
146
160
  return response;
147
161
  } catch (err) {
148
162
  console.error("Error in tenant deletion WebSocket request:", err);
149
- eventHelper.tenant.publish.deletionError(tenant);
150
- let contentMessage = "Unknown error";
151
- let errorContent = "Unknown error";
152
-
153
- if (
154
- typeof err === "object" &&
155
- err !== null &&
156
- "error" in err &&
157
- typeof (err as any).error === "object" &&
158
- (err as any).error !== null
159
- ) {
160
- if ("code" in (err as any).error) {
161
- contentMessage = (err as any).error.code;
162
- }
163
- if ("content" in (err as any).error) {
164
- errorContent = (err as any).error.content;
165
- }
166
- }
163
+ const { code: contentMessage, content: errorContent } =
164
+ extractWsErrorInfo(err);
167
165
  const tenantDeletionErrorNotification: Notification = {
168
166
  type: "error",
169
167
  subtype: errors.tenant.deletionError.subtype,
@@ -200,7 +198,12 @@ export const updateTenant = async (tenant: Tenant, security: string) => {
200
198
 
201
199
  const updateBody: Record<string, any> = {
202
200
  tenant: tenant.name,
203
- spec: {
201
+ };
202
+ if (hasPlan) {
203
+ updateBody.provider = tenant.plan!.provider || "";
204
+ updateBody.plan = tenant.plan!.name;
205
+ } else {
206
+ updateBody.spec = {
204
207
  registry: !hasNpmRegistry
205
208
  ? undefined
206
209
  : isNpmRegistryEmpty
@@ -212,12 +215,7 @@ export const updateTenant = async (tenant: Tenant, security: string) => {
212
215
  credentials: tenant.npmRegistry!.credentials,
213
216
  public: tenant.npmRegistry!.public,
214
217
  },
215
- },
216
- };
217
-
218
- if (hasPlan) {
219
- updateBody.provider = tenant.plan!.provider || "";
220
- updateBody.plan = tenant.plan!.name;
218
+ };
221
219
  }
222
220
 
223
221
  const response = await makeGlobalWebSocketRequest(
@@ -244,24 +242,8 @@ export const updateTenant = async (tenant: Tenant, security: string) => {
244
242
  return response;
245
243
  } catch (err) {
246
244
  console.error("Error in tenant update WebSocket request:", err);
247
- eventHelper.tenant.publish.updateError(tenant);
248
- let contentMessage = "Unknown error";
249
- let errorContent = "Unknown error";
250
-
251
- if (
252
- typeof err === "object" &&
253
- err !== null &&
254
- "error" in err &&
255
- typeof (err as any).error === "object" &&
256
- (err as any).error !== null
257
- ) {
258
- if ("code" in (err as any).error) {
259
- contentMessage = (err as any).error.code;
260
- }
261
- if ("content" in (err as any).error) {
262
- errorContent = (err as any).error.content;
263
- }
264
- }
245
+ const { code: contentMessage, content: errorContent } =
246
+ extractWsErrorInfo(err);
265
247
  const tenantUpdateErrorNotification: Notification = {
266
248
  type: "error",
267
249
  subtype: errors.tenant.updateError.subtype,
@@ -452,12 +434,11 @@ export const updateTenantHTTP = async (tenant: Tenant) => {
452
434
  tenant.plan?.provider || ""
453
435
  );
454
436
  }
437
+ const hasPlan = tenant.plan && tenant.plan?.name !== "" && tenant.plan?.provider !== "";
455
438
  const tenantBody =
456
- tenant.npmRegistry && tenant.npmRegistry.endpoint !== ""
439
+ !hasPlan && tenant.npmRegistry && tenant.npmRegistry.endpoint !== ""
457
440
  ? {
458
441
  tenant: tenant.name,
459
- provider: tenant.plan?.provider || "",
460
- plan: tenant.plan?.name || "",
461
442
  spec: {
462
443
  registry: {
463
444
  kind: "git",
@@ -468,12 +449,14 @@ export const updateTenantHTTP = async (tenant: Tenant) => {
468
449
  },
469
450
  },
470
451
  }
471
- : {
472
- tenant: tenant.name,
473
- spec: {
474
- registry: undefined,
475
- },
476
- };
452
+ : hasPlan
453
+ ? { tenant: tenant.name }
454
+ : {
455
+ tenant: tenant.name,
456
+ spec: {
457
+ registry: undefined,
458
+ },
459
+ };
477
460
  const response = await fetch(updateTenantUrl, {
478
461
  method: "PATCH",
479
462
  headers: {
@@ -586,23 +569,8 @@ export const createRegistry = async (
586
569
  webSocketStatus: getWebSocketStatus(),
587
570
  });
588
571
 
589
- let contentMessage = "Unknown error";
590
- let errorContent = "Unknown error";
591
-
592
- if (
593
- typeof error === "object" &&
594
- error !== null &&
595
- "error" in error &&
596
- typeof (error as any).error === "object" &&
597
- (error as any).error !== null
598
- ) {
599
- if ("code" in (error as any).error) {
600
- contentMessage = (error as any).error.code;
601
- }
602
- if ("content" in (error as any).error) {
603
- errorContent = (error as any).error.content;
604
- }
605
- }
572
+ const { code: contentMessage, content: errorContent } =
573
+ extractWsErrorInfo(error);
606
574
 
607
575
  const dregistryCreationErrorNotification: Notification = {
608
576
  type: "error",
@@ -689,23 +657,8 @@ export const updateRegistry = async (
689
657
  webSocketStatus: getWebSocketStatus(),
690
658
  });
691
659
 
692
- let contentMessage = "Unknown error";
693
- let errorContent = "Unknown error";
694
-
695
- if (
696
- typeof error === "object" &&
697
- error !== null &&
698
- "error" in error &&
699
- typeof (error as any).error === "object" &&
700
- (error as any).error !== null
701
- ) {
702
- if ("code" in (error as any).error) {
703
- contentMessage = (error as any).error.code;
704
- }
705
- if ("content" in (error as any).error) {
706
- errorContent = (error as any).error.content;
707
- }
708
- }
660
+ const { code: contentMessage, content: errorContent } =
661
+ extractWsErrorInfo(error);
709
662
 
710
663
  const dregistryUpdateErrorNotification: Notification = {
711
664
  type: "error",
@@ -773,23 +726,8 @@ export const deleteRegistry = async (
773
726
  webSocketStatus: getWebSocketStatus(),
774
727
  });
775
728
 
776
- let contentMessage = "Unknown error";
777
- let errorContent = "Unknown error";
778
-
779
- if (
780
- typeof error === "object" &&
781
- error !== null &&
782
- "error" in error &&
783
- typeof (error as any).error === "object" &&
784
- (error as any).error !== null
785
- ) {
786
- if ("code" in (error as any).error) {
787
- contentMessage = (error as any).error.code;
788
- }
789
- if ("content" in (error as any).error) {
790
- errorContent = (error as any).error.content;
791
- }
792
- }
729
+ const { code: contentMessage, content: errorContent } =
730
+ extractWsErrorInfo(error);
793
731
 
794
732
  const dregistryDeletionErrorNotification: Notification = {
795
733
  type: "error",
@@ -859,23 +797,8 @@ export const inviteUser = async (
859
797
  role: role,
860
798
  webSocketStatus: getWebSocketStatus(),
861
799
  });
862
- let contentMessage = "Unknown error";
863
- let errorContent = "Unknown error";
864
-
865
- if (
866
- typeof error === "object" &&
867
- error !== null &&
868
- "error" in error &&
869
- typeof (error as any).error === "object" &&
870
- (error as any).error !== null
871
- ) {
872
- if ("code" in (error as any).error) {
873
- contentMessage = (error as any).error.code;
874
- }
875
- if ("content" in (error as any).error) {
876
- errorContent = (error as any).error.content;
877
- }
878
- }
800
+ const { code: contentMessage, content: errorContent } =
801
+ extractWsErrorInfo(error);
879
802
  const tenantInviteErrorNotification: Notification = {
880
803
  type: "error",
881
804
  subtype: errors.tenant.userInviteError.subtype,
@@ -938,23 +861,8 @@ export const removeUser = async (
938
861
  userId: userId,
939
862
  webSocketStatus: getWebSocketStatus(),
940
863
  });
941
- let contentMessage = "Unknown error";
942
- let errorContent = "Unknown error";
943
-
944
- if (
945
- typeof error === "object" &&
946
- error !== null &&
947
- "error" in error &&
948
- typeof (error as any).error === "object" &&
949
- (error as any).error !== null
950
- ) {
951
- if ("code" in (error as any).error) {
952
- contentMessage = (error as any).error.code;
953
- }
954
- if ("content" in (error as any).error) {
955
- errorContent = (error as any).error.content;
956
- }
957
- }
864
+ const { code: contentMessage, content: errorContent } =
865
+ extractWsErrorInfo(error);
958
866
  const tenantUserRemovedErrorNotification: Notification = {
959
867
  type: "error",
960
868
  subtype: errors.tenant.userRemovedError.subtype,
@@ -1021,23 +929,8 @@ export const updateUserRole = async (
1021
929
  role: role,
1022
930
  webSocketStatus: getWebSocketStatus(),
1023
931
  });
1024
- let contentMessage = "Unknown error";
1025
- let errorContent = "Unknown error";
1026
-
1027
- if (
1028
- typeof error === "object" &&
1029
- error !== null &&
1030
- "error" in error &&
1031
- typeof (error as any).error === "object" &&
1032
- (error as any).error !== null
1033
- ) {
1034
- if ("code" in (error as any).error) {
1035
- contentMessage = (error as any).error.code;
1036
- }
1037
- if ("content" in (error as any).error) {
1038
- errorContent = (error as any).error.content;
1039
- }
1040
- }
932
+ const { code: contentMessage, content: errorContent } =
933
+ extractWsErrorInfo(error);
1041
934
  const tenantUserUpdatedErrorNotification: Notification = {
1042
935
  type: "error",
1043
936
  subtype: errors.tenant.roleUpdateError.subtype,
@@ -1092,23 +985,8 @@ export const acceptInvite = async (tenant: string, security: string) => {
1092
985
  tenant: tenant,
1093
986
  webSocketStatus: getWebSocketStatus(),
1094
987
  });
1095
- let contentMessage = "Unknown error";
1096
- let errorContent = "Unknown error";
1097
-
1098
- if (
1099
- typeof error === "object" &&
1100
- error !== null &&
1101
- "error" in error &&
1102
- typeof (error as any).error === "object" &&
1103
- (error as any).error !== null
1104
- ) {
1105
- if ("code" in (error as any).error) {
1106
- contentMessage = (error as any).error.code;
1107
- }
1108
- if ("content" in (error as any).error) {
1109
- errorContent = (error as any).error.content;
1110
- }
1111
- }
988
+ const { code: contentMessage, content: errorContent } =
989
+ extractWsErrorInfo(error);
1112
990
  const tenantInviteAcceptedErrorNotification: Notification = {
1113
991
  type: "error",
1114
992
  subtype: errors.tenant.inviteAcceptError.subtype,
@@ -1177,23 +1055,8 @@ export const rejectInvite = async (tenant: string, security: string, leave: bool
1177
1055
  tenant: tenant,
1178
1056
  webSocketStatus: getWebSocketStatus(),
1179
1057
  });
1180
- let contentMessage = "Unknown error";
1181
- let errorContent = "Unknown error";
1182
-
1183
- if (
1184
- typeof error === "object" &&
1185
- error !== null &&
1186
- "error" in error &&
1187
- typeof (error as any).error === "object" &&
1188
- (error as any).error !== null
1189
- ) {
1190
- if ("code" in (error as any).error) {
1191
- contentMessage = (error as any).error.code;
1192
- }
1193
- if ("content" in (error as any).error) {
1194
- errorContent = (error as any).error.content;
1195
- }
1196
- }
1058
+ const { code: contentMessage, content: errorContent } =
1059
+ extractWsErrorInfo(error);
1197
1060
  const tenantInviteRejectedErrorNotification: Notification = {
1198
1061
  type: "error",
1199
1062
  subtype: errors.tenant.inviteRejectError.subtype,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumori/aurora-backend-handler",
3
- "version": "1.1.85",
3
+ "version": "1.1.87",
4
4
  "description": "backend handler",
5
5
  "main": "backend-handler.ts",
6
6
  "scripts": {