@phala/cloud 0.1.1 → 0.1.2
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.
- package/dist/actions/cvms/get_cvm_attestation.d.ts +4 -4
- package/dist/actions/cvms/provision_cvm_compose_file_update.d.ts +1 -3
- package/dist/client.d.ts +79 -11
- package/dist/index.js +316 -77
- package/dist/index.mjs +306 -77
- package/dist/types/client.d.ts +3 -82
- package/dist/utils/errors.d.ts +261 -0
- package/dist/utils/index.d.ts +1 -1
- package/package.json +2 -1
- package/dist/utils/get_error_message.d.ts +0 -2
package/dist/index.js
CHANGED
|
@@ -32,7 +32,9 @@ var index_exports = {};
|
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
AddComposeHashSchema: () => AddComposeHashSchema,
|
|
34
34
|
ApiErrorSchema: () => ApiErrorSchema,
|
|
35
|
+
AuthError: () => AuthError,
|
|
35
36
|
AvailableNodesSchema: () => AvailableNodesSchema,
|
|
37
|
+
BusinessError: () => BusinessError,
|
|
36
38
|
CommitCvmComposeFileUpdateRequestSchema: () => CommitCvmComposeFileUpdateRequestSchema,
|
|
37
39
|
CommitCvmComposeFileUpdateSchema: () => CommitCvmComposeFileUpdateSchema,
|
|
38
40
|
CommitCvmProvisionRequestSchema: () => CommitCvmProvisionRequestSchema,
|
|
@@ -74,6 +76,7 @@ __export(index_exports, {
|
|
|
74
76
|
NetworkError: () => NetworkError,
|
|
75
77
|
PaginatedInstanceTypesSchema: () => PaginatedInstanceTypesSchema,
|
|
76
78
|
PaginationMetadataSchema: () => PaginationMetadataSchema,
|
|
79
|
+
PhalaCloudError: () => PhalaCloudError,
|
|
77
80
|
ProvisionCvmComposeFileUpdateRequestSchema: () => ProvisionCvmComposeFileUpdateRequestSchema,
|
|
78
81
|
ProvisionCvmComposeFileUpdateResultSchema: () => ProvisionCvmComposeFileUpdateResultSchema,
|
|
79
82
|
ProvisionCvmRequestSchema: () => ProvisionCvmRequestSchema,
|
|
@@ -81,13 +84,16 @@ __export(index_exports, {
|
|
|
81
84
|
RequestError: () => RequestError,
|
|
82
85
|
RestartCvmRequestSchema: () => RestartCvmRequestSchema,
|
|
83
86
|
SUPPORTED_CHAINS: () => SUPPORTED_CHAINS,
|
|
87
|
+
ServerError: () => ServerError,
|
|
84
88
|
ShutdownCvmRequestSchema: () => ShutdownCvmRequestSchema,
|
|
85
89
|
StartCvmRequestSchema: () => StartCvmRequestSchema,
|
|
86
90
|
StopCvmRequestSchema: () => StopCvmRequestSchema,
|
|
87
91
|
TransactionError: () => TransactionError,
|
|
92
|
+
UnknownError: () => UnknownError,
|
|
88
93
|
UpdateCvmResourcesRequestSchema: () => UpdateCvmResourcesRequestSchema,
|
|
89
94
|
UpdateCvmVisibilityRequestSchema: () => UpdateCvmVisibilityRequestSchema,
|
|
90
95
|
VMSchema: () => VMSchema,
|
|
96
|
+
ValidationError: () => ValidationError,
|
|
91
97
|
VmInfoSchema: () => VmInfoSchema,
|
|
92
98
|
WalletError: () => WalletError,
|
|
93
99
|
WorkspaceResponseSchema: () => WorkspaceResponseSchema,
|
|
@@ -116,6 +122,8 @@ __export(index_exports, {
|
|
|
116
122
|
executeTransaction: () => executeTransaction,
|
|
117
123
|
executeTransactionWithRetry: () => executeTransactionWithRetry,
|
|
118
124
|
extractNetworkClients: () => extractNetworkClients,
|
|
125
|
+
formatErrorMessage: () => formatErrorMessage,
|
|
126
|
+
formatValidationErrors: () => formatValidationErrors,
|
|
119
127
|
getAppEnvEncryptPubKey: () => getAppEnvEncryptPubKey,
|
|
120
128
|
getAvailableNodes: () => getAvailableNodes,
|
|
121
129
|
getComposeHash: () => import_get_compose_hash3.getComposeHash,
|
|
@@ -131,9 +139,11 @@ __export(index_exports, {
|
|
|
131
139
|
getErrorMessage: () => getErrorMessage,
|
|
132
140
|
getKmsInfo: () => getKmsInfo,
|
|
133
141
|
getKmsList: () => getKmsList,
|
|
142
|
+
getValidationFields: () => getValidationFields,
|
|
134
143
|
getWorkspace: () => getWorkspace,
|
|
135
144
|
listInstanceTypes: () => listInstanceTypes,
|
|
136
145
|
listWorkspaces: () => listWorkspaces,
|
|
146
|
+
parseApiError: () => parseApiError,
|
|
137
147
|
parseEnv: () => parseEnv,
|
|
138
148
|
parseEnvVars: () => parseEnvVars,
|
|
139
149
|
preprocessAppCompose: () => preprocessAppCompose,
|
|
@@ -189,8 +199,9 @@ module.exports = __toCommonJS(index_exports);
|
|
|
189
199
|
// src/client.ts
|
|
190
200
|
var import_ofetch = require("ofetch");
|
|
191
201
|
var import_debug = __toESM(require("debug"));
|
|
202
|
+
var import_mitt = __toESM(require("mitt"));
|
|
192
203
|
|
|
193
|
-
// src/
|
|
204
|
+
// src/utils/errors.ts
|
|
194
205
|
var import_zod = require("zod");
|
|
195
206
|
var ApiErrorSchema = import_zod.z.object({
|
|
196
207
|
detail: import_zod.z.union([
|
|
@@ -203,21 +214,34 @@ var ApiErrorSchema = import_zod.z.object({
|
|
|
203
214
|
})
|
|
204
215
|
),
|
|
205
216
|
import_zod.z.record(import_zod.z.unknown())
|
|
206
|
-
]),
|
|
217
|
+
]).optional(),
|
|
207
218
|
type: import_zod.z.string().optional(),
|
|
208
219
|
code: import_zod.z.string().optional()
|
|
209
220
|
});
|
|
210
|
-
var
|
|
211
|
-
constructor(message,
|
|
221
|
+
var PhalaCloudError = class extends Error {
|
|
222
|
+
constructor(message, data) {
|
|
212
223
|
super(message);
|
|
224
|
+
this.name = this.constructor.name;
|
|
225
|
+
this.status = data.status;
|
|
226
|
+
this.statusText = data.statusText;
|
|
227
|
+
this.detail = data.detail;
|
|
228
|
+
if (Error.captureStackTrace) {
|
|
229
|
+
Error.captureStackTrace(this, this.constructor);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
var RequestError = class _RequestError extends PhalaCloudError {
|
|
234
|
+
constructor(message, options) {
|
|
235
|
+
super(message, {
|
|
236
|
+
status: options?.status ?? 0,
|
|
237
|
+
statusText: options?.statusText ?? "Unknown Error",
|
|
238
|
+
detail: options?.detail || message
|
|
239
|
+
});
|
|
213
240
|
this.name = "RequestError";
|
|
214
241
|
this.isRequestError = true;
|
|
215
|
-
this.status = options?.status;
|
|
216
|
-
this.statusText = options?.statusText;
|
|
217
242
|
this.data = options?.data;
|
|
218
243
|
this.request = options?.request;
|
|
219
244
|
this.response = options?.response;
|
|
220
|
-
this.detail = options?.detail || message;
|
|
221
245
|
this.code = options?.code;
|
|
222
246
|
this.type = options?.type;
|
|
223
247
|
}
|
|
@@ -258,9 +282,172 @@ var RequestError = class _RequestError extends Error {
|
|
|
258
282
|
});
|
|
259
283
|
}
|
|
260
284
|
};
|
|
285
|
+
var ValidationError = class extends PhalaCloudError {
|
|
286
|
+
constructor(message, data) {
|
|
287
|
+
super(message, data);
|
|
288
|
+
this.isValidationError = true;
|
|
289
|
+
this.validationErrors = data.validationErrors;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
var AuthError = class extends PhalaCloudError {
|
|
293
|
+
constructor() {
|
|
294
|
+
super(...arguments);
|
|
295
|
+
this.isAuthError = true;
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
var BusinessError = class extends PhalaCloudError {
|
|
299
|
+
constructor() {
|
|
300
|
+
super(...arguments);
|
|
301
|
+
this.isBusinessError = true;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
var ServerError = class extends PhalaCloudError {
|
|
305
|
+
constructor() {
|
|
306
|
+
super(...arguments);
|
|
307
|
+
this.isServerError = true;
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
var UnknownError = class extends PhalaCloudError {
|
|
311
|
+
constructor() {
|
|
312
|
+
super(...arguments);
|
|
313
|
+
this.isUnknownError = true;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
function extractFieldPath(loc) {
|
|
317
|
+
const filtered = loc.filter((part) => {
|
|
318
|
+
if (typeof part === "string") {
|
|
319
|
+
return !["body", "query", "path", "header"].includes(part);
|
|
320
|
+
}
|
|
321
|
+
return true;
|
|
322
|
+
});
|
|
323
|
+
return filtered.length > 0 ? filtered.join(".") : "unknown";
|
|
324
|
+
}
|
|
325
|
+
function parseValidationErrors(detail) {
|
|
326
|
+
if (!Array.isArray(detail)) {
|
|
327
|
+
return {
|
|
328
|
+
errors: [],
|
|
329
|
+
message: typeof detail === "string" ? detail : "Validation error"
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
const errors = detail.map((item) => ({
|
|
333
|
+
field: extractFieldPath(item.loc),
|
|
334
|
+
message: item.msg,
|
|
335
|
+
type: item.type,
|
|
336
|
+
context: item.ctx
|
|
337
|
+
}));
|
|
338
|
+
const count = errors.length;
|
|
339
|
+
const message = count === 1 ? `Validation failed: ${errors[0].message}` : `Validation failed (${count} issue${count > 1 ? "s" : ""})`;
|
|
340
|
+
return { errors, message };
|
|
341
|
+
}
|
|
342
|
+
function categorizeErrorType(status) {
|
|
343
|
+
if (status === 422) {
|
|
344
|
+
return "validation";
|
|
345
|
+
}
|
|
346
|
+
if (status === 401) {
|
|
347
|
+
return "auth";
|
|
348
|
+
}
|
|
349
|
+
if (status === 403) {
|
|
350
|
+
return "auth";
|
|
351
|
+
}
|
|
352
|
+
if (status >= 400 && status < 500) {
|
|
353
|
+
return "business";
|
|
354
|
+
}
|
|
355
|
+
if (status >= 500) {
|
|
356
|
+
return "server";
|
|
357
|
+
}
|
|
358
|
+
return "unknown";
|
|
359
|
+
}
|
|
360
|
+
function extractPrimaryMessage(status, detail, defaultMessage) {
|
|
361
|
+
if (status === 422 && Array.isArray(detail)) {
|
|
362
|
+
const { message } = parseValidationErrors(detail);
|
|
363
|
+
return message;
|
|
364
|
+
}
|
|
365
|
+
if (typeof detail === "string") {
|
|
366
|
+
return detail;
|
|
367
|
+
}
|
|
368
|
+
if (detail && typeof detail === "object" && "message" in detail) {
|
|
369
|
+
const msg = detail.message;
|
|
370
|
+
if (typeof msg === "string") {
|
|
371
|
+
return msg;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return defaultMessage;
|
|
375
|
+
}
|
|
376
|
+
function parseApiError(requestError) {
|
|
377
|
+
const status = requestError.status ?? 0;
|
|
378
|
+
const statusText = requestError.statusText ?? "Unknown Error";
|
|
379
|
+
const detail = requestError.detail;
|
|
380
|
+
const errorType = categorizeErrorType(status);
|
|
381
|
+
const message = extractPrimaryMessage(status, detail, requestError.message);
|
|
382
|
+
const commonData = {
|
|
383
|
+
status,
|
|
384
|
+
statusText,
|
|
385
|
+
detail
|
|
386
|
+
};
|
|
387
|
+
if (errorType === "validation" && Array.isArray(detail)) {
|
|
388
|
+
const { errors } = parseValidationErrors(detail);
|
|
389
|
+
return new ValidationError(message, {
|
|
390
|
+
...commonData,
|
|
391
|
+
validationErrors: errors
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
if (errorType === "auth") {
|
|
395
|
+
return new AuthError(message, commonData);
|
|
396
|
+
}
|
|
397
|
+
if (errorType === "business") {
|
|
398
|
+
return new BusinessError(message, commonData);
|
|
399
|
+
}
|
|
400
|
+
if (errorType === "server") {
|
|
401
|
+
return new ServerError(message, commonData);
|
|
402
|
+
}
|
|
403
|
+
return new UnknownError(message, commonData);
|
|
404
|
+
}
|
|
405
|
+
function getValidationFields(error) {
|
|
406
|
+
if (error instanceof ValidationError) {
|
|
407
|
+
return error.validationErrors.map((e) => e.field);
|
|
408
|
+
}
|
|
409
|
+
return [];
|
|
410
|
+
}
|
|
411
|
+
function formatValidationErrors(errors, options) {
|
|
412
|
+
const { numbered = true, indent = 2, showFields = true } = options ?? {};
|
|
413
|
+
const indentStr = " ".repeat(indent);
|
|
414
|
+
return errors.map((error, index) => {
|
|
415
|
+
const prefix = numbered ? `${index + 1}. ` : "\u2022 ";
|
|
416
|
+
const field = showFields ? `${error.field}: ` : "";
|
|
417
|
+
return `${indentStr}${prefix}${field}${error.message}`;
|
|
418
|
+
}).join("\n");
|
|
419
|
+
}
|
|
420
|
+
function formatErrorMessage(error, options) {
|
|
421
|
+
const { showFields = true, showType = false } = options ?? {};
|
|
422
|
+
const parts = [];
|
|
423
|
+
if (showType) {
|
|
424
|
+
parts.push(`[${error.constructor.name.toUpperCase()}]`);
|
|
425
|
+
}
|
|
426
|
+
parts.push(error.message);
|
|
427
|
+
if (error instanceof ValidationError && error.validationErrors.length > 0) {
|
|
428
|
+
parts.push("");
|
|
429
|
+
parts.push(formatValidationErrors(error.validationErrors, { showFields }));
|
|
430
|
+
}
|
|
431
|
+
return parts.join("\n");
|
|
432
|
+
}
|
|
433
|
+
function getErrorMessage(error) {
|
|
434
|
+
if (typeof error.detail === "string") {
|
|
435
|
+
return error.detail;
|
|
436
|
+
}
|
|
437
|
+
if (Array.isArray(error.detail)) {
|
|
438
|
+
if (error.detail.length > 0) {
|
|
439
|
+
return error.detail[0]?.msg || "Validation error";
|
|
440
|
+
}
|
|
441
|
+
return "Validation error";
|
|
442
|
+
}
|
|
443
|
+
if (typeof error.detail === "object" && error.detail !== null) {
|
|
444
|
+
return JSON.stringify(error.detail);
|
|
445
|
+
}
|
|
446
|
+
return "Unknown error occurred";
|
|
447
|
+
}
|
|
261
448
|
|
|
262
449
|
// src/client.ts
|
|
263
|
-
var SUPPORTED_API_VERSIONS = ["2025-05-31"];
|
|
450
|
+
var SUPPORTED_API_VERSIONS = ["2025-05-31", "2025-10-28"];
|
|
264
451
|
var logger = (0, import_debug.default)("phala::api-client");
|
|
265
452
|
function formatHeaders(headers) {
|
|
266
453
|
return Object.entries(headers).map(([key, value]) => ` -H "${key}: ${value}"`).join("\n");
|
|
@@ -286,18 +473,14 @@ function formatResponse(status, statusText, headers, body) {
|
|
|
286
473
|
}
|
|
287
474
|
var Client = class {
|
|
288
475
|
constructor(config = {}) {
|
|
476
|
+
this.emitter = (0, import_mitt.default)();
|
|
289
477
|
const resolvedConfig = {
|
|
290
478
|
...config,
|
|
291
479
|
apiKey: config.apiKey || process?.env?.PHALA_CLOUD_API_KEY,
|
|
292
480
|
baseURL: config.baseURL || process?.env?.PHALA_CLOUD_API_PREFIX || "https://cloud-api.phala.network/api/v1"
|
|
293
481
|
};
|
|
294
|
-
const version = resolvedConfig.version && SUPPORTED_API_VERSIONS.includes(resolvedConfig.version) ? resolvedConfig.version : SUPPORTED_API_VERSIONS[
|
|
482
|
+
const version = resolvedConfig.version && SUPPORTED_API_VERSIONS.includes(resolvedConfig.version) ? resolvedConfig.version : SUPPORTED_API_VERSIONS[SUPPORTED_API_VERSIONS.length - 1];
|
|
295
483
|
this.config = resolvedConfig;
|
|
296
|
-
if (!resolvedConfig.useCookieAuth && !resolvedConfig.apiKey) {
|
|
297
|
-
throw new Error(
|
|
298
|
-
"API key is required. Provide it via config.apiKey or set PHALA_CLOUD_API_KEY environment variable."
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
484
|
const { apiKey, baseURL, timeout, headers, useCookieAuth, onResponseError, ...fetchOptions } = resolvedConfig;
|
|
302
485
|
const requestHeaders = {
|
|
303
486
|
"X-Phala-Version": version,
|
|
@@ -380,75 +563,137 @@ var Client = class {
|
|
|
380
563
|
get raw() {
|
|
381
564
|
return this.fetchInstance;
|
|
382
565
|
}
|
|
566
|
+
on(type, handler) {
|
|
567
|
+
this.emitter.on(type, handler);
|
|
568
|
+
}
|
|
569
|
+
off(type, handler) {
|
|
570
|
+
this.emitter.off(type, handler);
|
|
571
|
+
}
|
|
572
|
+
once(type, handler) {
|
|
573
|
+
const wrappedHandler = (event) => {
|
|
574
|
+
handler(event);
|
|
575
|
+
this.emitter.off(type, wrappedHandler);
|
|
576
|
+
};
|
|
577
|
+
this.emitter.on(type, wrappedHandler);
|
|
578
|
+
}
|
|
383
579
|
// ===== Direct methods (throw on error) =====
|
|
384
580
|
/**
|
|
385
|
-
* Perform GET request (throws on error)
|
|
581
|
+
* Perform GET request (throws PhalaCloudError on error)
|
|
386
582
|
*/
|
|
387
583
|
async get(request, options) {
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
584
|
+
try {
|
|
585
|
+
return await this.fetchInstance(request, {
|
|
586
|
+
...options,
|
|
587
|
+
method: "GET"
|
|
588
|
+
});
|
|
589
|
+
} catch (error) {
|
|
590
|
+
const requestError = this.convertToRequestError(error);
|
|
591
|
+
const phalaCloudError = this.emitError(requestError);
|
|
592
|
+
throw phalaCloudError;
|
|
593
|
+
}
|
|
392
594
|
}
|
|
393
595
|
/**
|
|
394
|
-
* Perform POST request (throws on error)
|
|
596
|
+
* Perform POST request (throws PhalaCloudError on error)
|
|
395
597
|
*/
|
|
396
598
|
async post(request, body, options) {
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
599
|
+
try {
|
|
600
|
+
return await this.fetchInstance(request, {
|
|
601
|
+
...options,
|
|
602
|
+
method: "POST",
|
|
603
|
+
body
|
|
604
|
+
});
|
|
605
|
+
} catch (error) {
|
|
606
|
+
const requestError = this.convertToRequestError(error);
|
|
607
|
+
const phalaCloudError = this.emitError(requestError);
|
|
608
|
+
throw phalaCloudError;
|
|
609
|
+
}
|
|
402
610
|
}
|
|
403
611
|
/**
|
|
404
|
-
* Perform PUT request (throws on error)
|
|
612
|
+
* Perform PUT request (throws PhalaCloudError on error)
|
|
405
613
|
*/
|
|
406
614
|
async put(request, body, options) {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
615
|
+
try {
|
|
616
|
+
return await this.fetchInstance(request, {
|
|
617
|
+
...options,
|
|
618
|
+
method: "PUT",
|
|
619
|
+
body
|
|
620
|
+
});
|
|
621
|
+
} catch (error) {
|
|
622
|
+
const requestError = this.convertToRequestError(error);
|
|
623
|
+
const phalaCloudError = this.emitError(requestError);
|
|
624
|
+
throw phalaCloudError;
|
|
625
|
+
}
|
|
412
626
|
}
|
|
413
627
|
/**
|
|
414
|
-
* Perform PATCH request (throws on error)
|
|
628
|
+
* Perform PATCH request (throws PhalaCloudError on error)
|
|
415
629
|
*/
|
|
416
630
|
async patch(request, body, options) {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
631
|
+
try {
|
|
632
|
+
return await this.fetchInstance(request, {
|
|
633
|
+
...options,
|
|
634
|
+
method: "PATCH",
|
|
635
|
+
body
|
|
636
|
+
});
|
|
637
|
+
} catch (error) {
|
|
638
|
+
const requestError = this.convertToRequestError(error);
|
|
639
|
+
const phalaCloudError = this.emitError(requestError);
|
|
640
|
+
throw phalaCloudError;
|
|
641
|
+
}
|
|
422
642
|
}
|
|
423
643
|
/**
|
|
424
|
-
* Perform DELETE request (throws on error)
|
|
644
|
+
* Perform DELETE request (throws PhalaCloudError on error)
|
|
425
645
|
*/
|
|
426
646
|
async delete(request, options) {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
647
|
+
try {
|
|
648
|
+
return await this.fetchInstance(request, {
|
|
649
|
+
...options,
|
|
650
|
+
method: "DELETE"
|
|
651
|
+
});
|
|
652
|
+
} catch (error) {
|
|
653
|
+
const requestError = this.convertToRequestError(error);
|
|
654
|
+
const phalaCloudError = this.emitError(requestError);
|
|
655
|
+
throw phalaCloudError;
|
|
656
|
+
}
|
|
431
657
|
}
|
|
432
658
|
// ===== Safe methods (return SafeResult) =====
|
|
659
|
+
/**
|
|
660
|
+
* Convert any error to RequestError
|
|
661
|
+
*/
|
|
662
|
+
convertToRequestError(error) {
|
|
663
|
+
if (error && typeof error === "object" && "data" in error) {
|
|
664
|
+
return RequestError.fromFetchError(error);
|
|
665
|
+
}
|
|
666
|
+
if (error instanceof Error) {
|
|
667
|
+
return RequestError.fromError(error);
|
|
668
|
+
}
|
|
669
|
+
return new RequestError("Unknown error occurred", {
|
|
670
|
+
detail: "Unknown error occurred"
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Broadcast error to event listeners (fire-and-forget)
|
|
675
|
+
* @param requestError - The request error to handle
|
|
676
|
+
* @returns PhalaCloudError instance to throw immediately
|
|
677
|
+
*/
|
|
678
|
+
emitError(requestError) {
|
|
679
|
+
const phalaCloudError = parseApiError(requestError);
|
|
680
|
+
this.emitter.emit("error", phalaCloudError);
|
|
681
|
+
return phalaCloudError;
|
|
682
|
+
}
|
|
433
683
|
/**
|
|
434
684
|
* Safe wrapper for any request method (zod-style result)
|
|
685
|
+
* Returns PhalaCloudError (all errors extend this base class)
|
|
435
686
|
*/
|
|
436
687
|
async safeRequest(fn) {
|
|
437
688
|
try {
|
|
438
689
|
const data = await fn();
|
|
439
690
|
return { success: true, data };
|
|
440
691
|
} catch (error) {
|
|
441
|
-
if (error
|
|
442
|
-
|
|
443
|
-
return { success: false, error: requestError2 };
|
|
692
|
+
if (error instanceof PhalaCloudError) {
|
|
693
|
+
return { success: false, error };
|
|
444
694
|
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
return { success: false, error: requestError2 };
|
|
448
|
-
}
|
|
449
|
-
const requestError = new RequestError("Unknown error occurred", {
|
|
450
|
-
detail: "Unknown error occurred"
|
|
451
|
-
});
|
|
695
|
+
const requestError = this.convertToRequestError(error);
|
|
696
|
+
this.emitError(requestError);
|
|
452
697
|
return { success: false, error: requestError };
|
|
453
698
|
}
|
|
454
699
|
}
|
|
@@ -571,7 +816,7 @@ function defineSimpleAction(schema, fn) {
|
|
|
571
816
|
const data = await fn(client);
|
|
572
817
|
return { success: true, data };
|
|
573
818
|
} catch (error) {
|
|
574
|
-
if (error && typeof error === "object" && "
|
|
819
|
+
if (error && typeof error === "object" && "status" in error) {
|
|
575
820
|
return { success: false, error };
|
|
576
821
|
}
|
|
577
822
|
if (error && typeof error === "object" && "issues" in error) {
|
|
@@ -628,7 +873,7 @@ function defineAction(schema, fn) {
|
|
|
628
873
|
const data = await fn(client, params);
|
|
629
874
|
return { success: true, data };
|
|
630
875
|
} catch (error) {
|
|
631
|
-
if (error && typeof error === "object" && "
|
|
876
|
+
if (error && typeof error === "object" && "status" in error) {
|
|
632
877
|
return { success: false, error };
|
|
633
878
|
}
|
|
634
879
|
if (error && typeof error === "object" && "issues" in error) {
|
|
@@ -1213,12 +1458,13 @@ var ProvisionCvmComposeFileUpdateRequestSchema = import_zod13.z.object({
|
|
|
1213
1458
|
}).refine(
|
|
1214
1459
|
(data) => !!(data.id || data.uuid || data.app_id || data.instance_id),
|
|
1215
1460
|
"One of id, uuid, app_id, or instance_id must be provided"
|
|
1216
|
-
).transform((data) =>
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
}
|
|
1461
|
+
).transform((data) => {
|
|
1462
|
+
return {
|
|
1463
|
+
cvmId: data.id || data.uuid || data.app_id || data.instance_id,
|
|
1464
|
+
request: { ...data.app_compose, update_env_vars: data.update_env_vars },
|
|
1465
|
+
_raw: data
|
|
1466
|
+
};
|
|
1467
|
+
});
|
|
1222
1468
|
var ProvisionCvmComposeFileUpdateResultSchema = import_zod13.z.object({
|
|
1223
1469
|
app_id: import_zod13.z.string().nullable(),
|
|
1224
1470
|
device_id: import_zod13.z.string().nullable(),
|
|
@@ -1640,23 +1886,6 @@ var import_accounts2 = require("viem/accounts");
|
|
|
1640
1886
|
// src/utils/index.ts
|
|
1641
1887
|
var import_encrypt_env_vars = require("@phala/dstack-sdk/encrypt-env-vars");
|
|
1642
1888
|
|
|
1643
|
-
// src/utils/get_error_message.ts
|
|
1644
|
-
function getErrorMessage(error) {
|
|
1645
|
-
if (typeof error.detail === "string") {
|
|
1646
|
-
return error.detail;
|
|
1647
|
-
}
|
|
1648
|
-
if (Array.isArray(error.detail)) {
|
|
1649
|
-
if (error.detail.length > 0) {
|
|
1650
|
-
return error.detail[0]?.msg || "Validation error";
|
|
1651
|
-
}
|
|
1652
|
-
return "Validation error";
|
|
1653
|
-
}
|
|
1654
|
-
if (typeof error.detail === "object" && error.detail !== null) {
|
|
1655
|
-
return JSON.stringify(error.detail);
|
|
1656
|
-
}
|
|
1657
|
-
return "Unknown error occurred";
|
|
1658
|
-
}
|
|
1659
|
-
|
|
1660
1889
|
// src/utils/as-hex.ts
|
|
1661
1890
|
var import_viem = require("viem");
|
|
1662
1891
|
function asHex(value) {
|
|
@@ -2897,7 +3126,9 @@ var import_verify_env_encrypt_public_key = require("@phala/dstack-sdk/verify-env
|
|
|
2897
3126
|
0 && (module.exports = {
|
|
2898
3127
|
AddComposeHashSchema,
|
|
2899
3128
|
ApiErrorSchema,
|
|
3129
|
+
AuthError,
|
|
2900
3130
|
AvailableNodesSchema,
|
|
3131
|
+
BusinessError,
|
|
2901
3132
|
CommitCvmComposeFileUpdateRequestSchema,
|
|
2902
3133
|
CommitCvmComposeFileUpdateSchema,
|
|
2903
3134
|
CommitCvmProvisionRequestSchema,
|
|
@@ -2939,6 +3170,7 @@ var import_verify_env_encrypt_public_key = require("@phala/dstack-sdk/verify-env
|
|
|
2939
3170
|
NetworkError,
|
|
2940
3171
|
PaginatedInstanceTypesSchema,
|
|
2941
3172
|
PaginationMetadataSchema,
|
|
3173
|
+
PhalaCloudError,
|
|
2942
3174
|
ProvisionCvmComposeFileUpdateRequestSchema,
|
|
2943
3175
|
ProvisionCvmComposeFileUpdateResultSchema,
|
|
2944
3176
|
ProvisionCvmRequestSchema,
|
|
@@ -2946,13 +3178,16 @@ var import_verify_env_encrypt_public_key = require("@phala/dstack-sdk/verify-env
|
|
|
2946
3178
|
RequestError,
|
|
2947
3179
|
RestartCvmRequestSchema,
|
|
2948
3180
|
SUPPORTED_CHAINS,
|
|
3181
|
+
ServerError,
|
|
2949
3182
|
ShutdownCvmRequestSchema,
|
|
2950
3183
|
StartCvmRequestSchema,
|
|
2951
3184
|
StopCvmRequestSchema,
|
|
2952
3185
|
TransactionError,
|
|
3186
|
+
UnknownError,
|
|
2953
3187
|
UpdateCvmResourcesRequestSchema,
|
|
2954
3188
|
UpdateCvmVisibilityRequestSchema,
|
|
2955
3189
|
VMSchema,
|
|
3190
|
+
ValidationError,
|
|
2956
3191
|
VmInfoSchema,
|
|
2957
3192
|
WalletError,
|
|
2958
3193
|
WorkspaceResponseSchema,
|
|
@@ -2981,6 +3216,8 @@ var import_verify_env_encrypt_public_key = require("@phala/dstack-sdk/verify-env
|
|
|
2981
3216
|
executeTransaction,
|
|
2982
3217
|
executeTransactionWithRetry,
|
|
2983
3218
|
extractNetworkClients,
|
|
3219
|
+
formatErrorMessage,
|
|
3220
|
+
formatValidationErrors,
|
|
2984
3221
|
getAppEnvEncryptPubKey,
|
|
2985
3222
|
getAvailableNodes,
|
|
2986
3223
|
getComposeHash,
|
|
@@ -2996,9 +3233,11 @@ var import_verify_env_encrypt_public_key = require("@phala/dstack-sdk/verify-env
|
|
|
2996
3233
|
getErrorMessage,
|
|
2997
3234
|
getKmsInfo,
|
|
2998
3235
|
getKmsList,
|
|
3236
|
+
getValidationFields,
|
|
2999
3237
|
getWorkspace,
|
|
3000
3238
|
listInstanceTypes,
|
|
3001
3239
|
listWorkspaces,
|
|
3240
|
+
parseApiError,
|
|
3002
3241
|
parseEnv,
|
|
3003
3242
|
parseEnvVars,
|
|
3004
3243
|
preprocessAppCompose,
|