pmxtjs 2.20.3 → 2.21.0

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/pmxt/client.ts CHANGED
@@ -43,6 +43,7 @@ import {
43
43
 
44
44
  import { ServerManager } from "./server-manager.js";
45
45
  import { buildArgsWithOptionalOptions } from "./args.js";
46
+ import { PmxtError, fromServerError } from "./errors.js";
46
47
 
47
48
  // Converter functions
48
49
  function convertMarket(raw: any): UnifiedMarket {
@@ -289,7 +290,7 @@ export abstract class Exchange {
289
290
  });
290
291
  this.api = new DefaultApi(this.config);
291
292
  } catch (error) {
292
- throw new Error(
293
+ throw new PmxtError(
293
294
  `Failed to start PMXT server: ${error}\n\n` +
294
295
  `Please ensure 'pmxt-core' is installed: npm install -g pmxt-core\n` +
295
296
  `Or start the server manually: pmxt-server`
@@ -301,7 +302,10 @@ export abstract class Exchange {
301
302
  protected handleResponse(response: any): any {
302
303
  if (!response.success) {
303
304
  const error = response.error || {};
304
- throw new Error(error.message || "Unknown error");
305
+ if (error && typeof error === "object" && (error.code || error.message)) {
306
+ throw fromServerError(error);
307
+ }
308
+ throw new PmxtError(error.message || "Unknown error");
305
309
  }
306
310
  return response.data;
307
311
  }
@@ -364,14 +368,18 @@ export abstract class Exchange {
364
368
  });
365
369
 
366
370
  if (!response.ok) {
367
- const error = await response.json().catch(() => ({}));
368
- throw new Error(error.error?.message || response.statusText);
371
+ const body = await response.json().catch(() => ({}));
372
+ if (body.error && typeof body.error === "object") {
373
+ throw fromServerError(body.error);
374
+ }
375
+ throw new PmxtError(body.error?.message || response.statusText);
369
376
  }
370
377
 
371
378
  const json = await response.json();
372
379
  return this.handleResponse(json);
373
380
  } catch (error) {
374
- throw new Error(`Failed to call API '${operationId}': ${error}`);
381
+ if (error instanceof PmxtError) throw error;
382
+ throw new PmxtError(`Failed to call API '${operationId}': ${error}`);
375
383
  }
376
384
  }
377
385
 
@@ -388,8 +396,11 @@ export abstract class Exchange {
388
396
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
389
397
  });
390
398
  if (!response.ok) {
391
- const error = await response.json().catch(() => ({}));
392
- throw new Error(error.error?.message || response.statusText);
399
+ const body = await response.json().catch(() => ({}));
400
+ if (body.error && typeof body.error === "object") {
401
+ throw fromServerError(body.error);
402
+ }
403
+ throw new PmxtError(body.error?.message || response.statusText);
393
404
  }
394
405
  const json = await response.json();
395
406
  const data = this.handleResponse(json);
@@ -399,7 +410,8 @@ export abstract class Exchange {
399
410
  }
400
411
  return result;
401
412
  } catch (error) {
402
- throw new Error(`Failed to loadMarkets: ${error}`);
413
+ if (error instanceof PmxtError) throw error;
414
+ throw new PmxtError(`Failed to loadMarkets: ${error}`);
403
415
  }
404
416
  }
405
417
 
@@ -413,14 +425,18 @@ export abstract class Exchange {
413
425
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
414
426
  });
415
427
  if (!response.ok) {
416
- const error = await response.json().catch(() => ({}));
417
- throw new Error(error.error?.message || response.statusText);
428
+ const body = await response.json().catch(() => ({}));
429
+ if (body.error && typeof body.error === "object") {
430
+ throw fromServerError(body.error);
431
+ }
432
+ throw new PmxtError(body.error?.message || response.statusText);
418
433
  }
419
434
  const json = await response.json();
420
435
  const data = this.handleResponse(json);
421
436
  return data.map(convertMarket);
422
437
  } catch (error) {
423
- throw new Error(`Failed to fetchMarkets: ${error}`);
438
+ if (error instanceof PmxtError) throw error;
439
+ throw new PmxtError(`Failed to fetchMarkets: ${error}`);
424
440
  }
425
441
  }
426
442
 
@@ -434,8 +450,11 @@ export abstract class Exchange {
434
450
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
435
451
  });
436
452
  if (!response.ok) {
437
- const error = await response.json().catch(() => ({}));
438
- throw new Error(error.error?.message || response.statusText);
453
+ const body = await response.json().catch(() => ({}));
454
+ if (body.error && typeof body.error === "object") {
455
+ throw fromServerError(body.error);
456
+ }
457
+ throw new PmxtError(body.error?.message || response.statusText);
439
458
  }
440
459
  const json = await response.json();
441
460
  const data = this.handleResponse(json);
@@ -445,7 +464,8 @@ export abstract class Exchange {
445
464
  nextCursor: data.nextCursor,
446
465
  };
447
466
  } catch (error) {
448
- throw new Error(`Failed to fetchMarketsPaginated: ${error}`);
467
+ if (error instanceof PmxtError) throw error;
468
+ throw new PmxtError(`Failed to fetchMarketsPaginated: ${error}`);
449
469
  }
450
470
  }
451
471
 
@@ -459,14 +479,18 @@ export abstract class Exchange {
459
479
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
460
480
  });
461
481
  if (!response.ok) {
462
- const error = await response.json().catch(() => ({}));
463
- throw new Error(error.error?.message || response.statusText);
482
+ const body = await response.json().catch(() => ({}));
483
+ if (body.error && typeof body.error === "object") {
484
+ throw fromServerError(body.error);
485
+ }
486
+ throw new PmxtError(body.error?.message || response.statusText);
464
487
  }
465
488
  const json = await response.json();
466
489
  const data = this.handleResponse(json);
467
490
  return data.map(convertEvent);
468
491
  } catch (error) {
469
- throw new Error(`Failed to fetchEvents: ${error}`);
492
+ if (error instanceof PmxtError) throw error;
493
+ throw new PmxtError(`Failed to fetchEvents: ${error}`);
470
494
  }
471
495
  }
472
496
 
@@ -480,14 +504,18 @@ export abstract class Exchange {
480
504
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
481
505
  });
482
506
  if (!response.ok) {
483
- const error = await response.json().catch(() => ({}));
484
- throw new Error(error.error?.message || response.statusText);
507
+ const body = await response.json().catch(() => ({}));
508
+ if (body.error && typeof body.error === "object") {
509
+ throw fromServerError(body.error);
510
+ }
511
+ throw new PmxtError(body.error?.message || response.statusText);
485
512
  }
486
513
  const json = await response.json();
487
514
  const data = this.handleResponse(json);
488
515
  return convertMarket(data);
489
516
  } catch (error) {
490
- throw new Error(`Failed to fetchMarket: ${error}`);
517
+ if (error instanceof PmxtError) throw error;
518
+ throw new PmxtError(`Failed to fetchMarket: ${error}`);
491
519
  }
492
520
  }
493
521
 
@@ -501,14 +529,18 @@ export abstract class Exchange {
501
529
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
502
530
  });
503
531
  if (!response.ok) {
504
- const error = await response.json().catch(() => ({}));
505
- throw new Error(error.error?.message || response.statusText);
532
+ const body = await response.json().catch(() => ({}));
533
+ if (body.error && typeof body.error === "object") {
534
+ throw fromServerError(body.error);
535
+ }
536
+ throw new PmxtError(body.error?.message || response.statusText);
506
537
  }
507
538
  const json = await response.json();
508
539
  const data = this.handleResponse(json);
509
540
  return convertEvent(data);
510
541
  } catch (error) {
511
- throw new Error(`Failed to fetchEvent: ${error}`);
542
+ if (error instanceof PmxtError) throw error;
543
+ throw new PmxtError(`Failed to fetchEvent: ${error}`);
512
544
  }
513
545
  }
514
546
 
@@ -523,14 +555,18 @@ export abstract class Exchange {
523
555
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
524
556
  });
525
557
  if (!response.ok) {
526
- const error = await response.json().catch(() => ({}));
527
- throw new Error(error.error?.message || response.statusText);
558
+ const body = await response.json().catch(() => ({}));
559
+ if (body.error && typeof body.error === "object") {
560
+ throw fromServerError(body.error);
561
+ }
562
+ throw new PmxtError(body.error?.message || response.statusText);
528
563
  }
529
564
  const json = await response.json();
530
565
  const data = this.handleResponse(json);
531
566
  return convertOrderBook(data);
532
567
  } catch (error) {
533
- throw new Error(`Failed to fetchOrderBook: ${error}`);
568
+ if (error instanceof PmxtError) throw error;
569
+ throw new PmxtError(`Failed to fetchOrderBook: ${error}`);
534
570
  }
535
571
  }
536
572
 
@@ -546,14 +582,18 @@ export abstract class Exchange {
546
582
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
547
583
  });
548
584
  if (!response.ok) {
549
- const error = await response.json().catch(() => ({}));
550
- throw new Error(error.error?.message || response.statusText);
585
+ const body = await response.json().catch(() => ({}));
586
+ if (body.error && typeof body.error === "object") {
587
+ throw fromServerError(body.error);
588
+ }
589
+ throw new PmxtError(body.error?.message || response.statusText);
551
590
  }
552
591
  const json = await response.json();
553
592
  const data = this.handleResponse(json);
554
593
  return convertOrder(data);
555
594
  } catch (error) {
556
- throw new Error(`Failed to cancelOrder: ${error}`);
595
+ if (error instanceof PmxtError) throw error;
596
+ throw new PmxtError(`Failed to cancelOrder: ${error}`);
557
597
  }
558
598
  }
559
599
 
@@ -568,14 +608,18 @@ export abstract class Exchange {
568
608
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
569
609
  });
570
610
  if (!response.ok) {
571
- const error = await response.json().catch(() => ({}));
572
- throw new Error(error.error?.message || response.statusText);
611
+ const body = await response.json().catch(() => ({}));
612
+ if (body.error && typeof body.error === "object") {
613
+ throw fromServerError(body.error);
614
+ }
615
+ throw new PmxtError(body.error?.message || response.statusText);
573
616
  }
574
617
  const json = await response.json();
575
618
  const data = this.handleResponse(json);
576
619
  return convertOrder(data);
577
620
  } catch (error) {
578
- throw new Error(`Failed to fetchOrder: ${error}`);
621
+ if (error instanceof PmxtError) throw error;
622
+ throw new PmxtError(`Failed to fetchOrder: ${error}`);
579
623
  }
580
624
  }
581
625
 
@@ -589,14 +633,18 @@ export abstract class Exchange {
589
633
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
590
634
  });
591
635
  if (!response.ok) {
592
- const error = await response.json().catch(() => ({}));
593
- throw new Error(error.error?.message || response.statusText);
636
+ const body = await response.json().catch(() => ({}));
637
+ if (body.error && typeof body.error === "object") {
638
+ throw fromServerError(body.error);
639
+ }
640
+ throw new PmxtError(body.error?.message || response.statusText);
594
641
  }
595
642
  const json = await response.json();
596
643
  const data = this.handleResponse(json);
597
644
  return data.map(convertOrder);
598
645
  } catch (error) {
599
- throw new Error(`Failed to fetchOpenOrders: ${error}`);
646
+ if (error instanceof PmxtError) throw error;
647
+ throw new PmxtError(`Failed to fetchOpenOrders: ${error}`);
600
648
  }
601
649
  }
602
650
 
@@ -610,14 +658,18 @@ export abstract class Exchange {
610
658
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
611
659
  });
612
660
  if (!response.ok) {
613
- const error = await response.json().catch(() => ({}));
614
- throw new Error(error.error?.message || response.statusText);
661
+ const body = await response.json().catch(() => ({}));
662
+ if (body.error && typeof body.error === "object") {
663
+ throw fromServerError(body.error);
664
+ }
665
+ throw new PmxtError(body.error?.message || response.statusText);
615
666
  }
616
667
  const json = await response.json();
617
668
  const data = this.handleResponse(json);
618
669
  return data.map(convertUserTrade);
619
670
  } catch (error) {
620
- throw new Error(`Failed to fetchMyTrades: ${error}`);
671
+ if (error instanceof PmxtError) throw error;
672
+ throw new PmxtError(`Failed to fetchMyTrades: ${error}`);
621
673
  }
622
674
  }
623
675
 
@@ -631,14 +683,18 @@ export abstract class Exchange {
631
683
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
632
684
  });
633
685
  if (!response.ok) {
634
- const error = await response.json().catch(() => ({}));
635
- throw new Error(error.error?.message || response.statusText);
686
+ const body = await response.json().catch(() => ({}));
687
+ if (body.error && typeof body.error === "object") {
688
+ throw fromServerError(body.error);
689
+ }
690
+ throw new PmxtError(body.error?.message || response.statusText);
636
691
  }
637
692
  const json = await response.json();
638
693
  const data = this.handleResponse(json);
639
694
  return data.map(convertOrder);
640
695
  } catch (error) {
641
- throw new Error(`Failed to fetchClosedOrders: ${error}`);
696
+ if (error instanceof PmxtError) throw error;
697
+ throw new PmxtError(`Failed to fetchClosedOrders: ${error}`);
642
698
  }
643
699
  }
644
700
 
@@ -652,14 +708,18 @@ export abstract class Exchange {
652
708
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
653
709
  });
654
710
  if (!response.ok) {
655
- const error = await response.json().catch(() => ({}));
656
- throw new Error(error.error?.message || response.statusText);
711
+ const body = await response.json().catch(() => ({}));
712
+ if (body.error && typeof body.error === "object") {
713
+ throw fromServerError(body.error);
714
+ }
715
+ throw new PmxtError(body.error?.message || response.statusText);
657
716
  }
658
717
  const json = await response.json();
659
718
  const data = this.handleResponse(json);
660
719
  return data.map(convertOrder);
661
720
  } catch (error) {
662
- throw new Error(`Failed to fetchAllOrders: ${error}`);
721
+ if (error instanceof PmxtError) throw error;
722
+ throw new PmxtError(`Failed to fetchAllOrders: ${error}`);
663
723
  }
664
724
  }
665
725
 
@@ -673,14 +733,18 @@ export abstract class Exchange {
673
733
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
674
734
  });
675
735
  if (!response.ok) {
676
- const error = await response.json().catch(() => ({}));
677
- throw new Error(error.error?.message || response.statusText);
736
+ const body = await response.json().catch(() => ({}));
737
+ if (body.error && typeof body.error === "object") {
738
+ throw fromServerError(body.error);
739
+ }
740
+ throw new PmxtError(body.error?.message || response.statusText);
678
741
  }
679
742
  const json = await response.json();
680
743
  const data = this.handleResponse(json);
681
744
  return data.map(convertPosition);
682
745
  } catch (error) {
683
- throw new Error(`Failed to fetchPositions: ${error}`);
746
+ if (error instanceof PmxtError) throw error;
747
+ throw new PmxtError(`Failed to fetchPositions: ${error}`);
684
748
  }
685
749
  }
686
750
 
@@ -694,14 +758,18 @@ export abstract class Exchange {
694
758
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
695
759
  });
696
760
  if (!response.ok) {
697
- const error = await response.json().catch(() => ({}));
698
- throw new Error(error.error?.message || response.statusText);
761
+ const body = await response.json().catch(() => ({}));
762
+ if (body.error && typeof body.error === "object") {
763
+ throw fromServerError(body.error);
764
+ }
765
+ throw new PmxtError(body.error?.message || response.statusText);
699
766
  }
700
767
  const json = await response.json();
701
768
  const data = this.handleResponse(json);
702
769
  return data.map(convertBalance);
703
770
  } catch (error) {
704
- throw new Error(`Failed to fetchBalance: ${error}`);
771
+ if (error instanceof PmxtError) throw error;
772
+ throw new PmxtError(`Failed to fetchBalance: ${error}`);
705
773
  }
706
774
  }
707
775
 
@@ -715,13 +783,17 @@ export abstract class Exchange {
715
783
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
716
784
  });
717
785
  if (!response.ok) {
718
- const error = await response.json().catch(() => ({}));
719
- throw new Error(error.error?.message || response.statusText);
786
+ const body = await response.json().catch(() => ({}));
787
+ if (body.error && typeof body.error === "object") {
788
+ throw fromServerError(body.error);
789
+ }
790
+ throw new PmxtError(body.error?.message || response.statusText);
720
791
  }
721
792
  const json = await response.json();
722
793
  this.handleResponse(json);
723
794
  } catch (error) {
724
- throw new Error(`Failed to close: ${error}`);
795
+ if (error instanceof PmxtError) throw error;
796
+ throw new PmxtError(`Failed to close: ${error}`);
725
797
  }
726
798
  }
727
799
 
@@ -774,7 +846,8 @@ export abstract class Exchange {
774
846
  const data = this.handleResponse(response);
775
847
  return data.map(convertCandle);
776
848
  } catch (error) {
777
- throw new Error(`Failed to fetch OHLCV: ${error}`);
849
+ if (error instanceof PmxtError) throw error;
850
+ throw new PmxtError(`Failed to fetch OHLCV: ${error}`);
778
851
  }
779
852
  }
780
853
 
@@ -811,7 +884,8 @@ export abstract class Exchange {
811
884
  const data = this.handleResponse(response);
812
885
  return data.map(convertTrade);
813
886
  } catch (error) {
814
- throw new Error(`Failed to fetch trades: ${error}`);
887
+ if (error instanceof PmxtError) throw error;
888
+ throw new PmxtError(`Failed to fetch trades: ${error}`);
815
889
  }
816
890
  }
817
891
 
@@ -858,7 +932,8 @@ export abstract class Exchange {
858
932
  const data = this.handleResponse(response);
859
933
  return convertOrderBook(data);
860
934
  } catch (error) {
861
- throw new Error(`Failed to watch order book: ${error}`);
935
+ if (error instanceof PmxtError) throw error;
936
+ throw new PmxtError(`Failed to watch order book: ${error}`);
862
937
  }
863
938
  }
864
939
 
@@ -917,7 +992,8 @@ export abstract class Exchange {
917
992
  const data = this.handleResponse(response);
918
993
  return data.map(convertTrade);
919
994
  } catch (error) {
920
- throw new Error(`Failed to watch trades: ${error}`);
995
+ if (error instanceof PmxtError) throw error;
996
+ throw new PmxtError(`Failed to watch trades: ${error}`);
921
997
  }
922
998
  }
923
999
 
@@ -965,7 +1041,8 @@ export abstract class Exchange {
965
1041
  const data = this.handleResponse(response);
966
1042
  return convertSubscriptionSnapshot(data);
967
1043
  } catch (error) {
968
- throw new Error(`Failed to watch address: ${error}`);
1044
+ if (error instanceof PmxtError) throw error;
1045
+ throw new PmxtError(`Failed to watch address: ${error}`);
969
1046
  }
970
1047
  }
971
1048
 
@@ -994,7 +1071,8 @@ export abstract class Exchange {
994
1071
 
995
1072
  return this.handleResponse(response);
996
1073
  } catch (error) {
997
- throw new Error(`Failed to unwatch address: ${error}`);
1074
+ if (error instanceof PmxtError) throw error;
1075
+ throw new PmxtError(`Failed to unwatch address: ${error}`);
998
1076
  }
999
1077
  }
1000
1078
 
@@ -1044,13 +1122,13 @@ export abstract class Exchange {
1044
1122
 
1045
1123
  if (params.outcome) {
1046
1124
  if (marketId !== undefined || outcomeId !== undefined) {
1047
- throw new Error(
1125
+ throw new PmxtError(
1048
1126
  "Cannot specify both 'outcome' and 'marketId'/'outcomeId'. Use one or the other."
1049
1127
  );
1050
1128
  }
1051
1129
  const outcome: MarketOutcome = params.outcome;
1052
1130
  if (!outcome.marketId) {
1053
- throw new Error(
1131
+ throw new PmxtError(
1054
1132
  "outcome.marketId is not set. Ensure the outcome comes from a fetched market."
1055
1133
  );
1056
1134
  }
@@ -1085,7 +1163,8 @@ export abstract class Exchange {
1085
1163
  const data = this.handleResponse(response);
1086
1164
  return data as BuiltOrder;
1087
1165
  } catch (error) {
1088
- throw new Error(`Failed to build order: ${error}`);
1166
+ if (error instanceof PmxtError) throw error;
1167
+ throw new PmxtError(`Failed to build order: ${error}`);
1089
1168
  }
1090
1169
  }
1091
1170
 
@@ -1124,7 +1203,8 @@ export abstract class Exchange {
1124
1203
  const data = this.handleResponse(response);
1125
1204
  return convertOrder(data);
1126
1205
  } catch (error) {
1127
- throw new Error(`Failed to submit order: ${error}`);
1206
+ if (error instanceof PmxtError) throw error;
1207
+ throw new PmxtError(`Failed to submit order: ${error}`);
1128
1208
  }
1129
1209
  }
1130
1210
 
@@ -1155,13 +1235,13 @@ export abstract class Exchange {
1155
1235
 
1156
1236
  if (params.outcome) {
1157
1237
  if (marketId !== undefined || outcomeId !== undefined) {
1158
- throw new Error(
1238
+ throw new PmxtError(
1159
1239
  "Cannot specify both 'outcome' and 'marketId'/'outcomeId'. Use one or the other."
1160
1240
  );
1161
1241
  }
1162
1242
  const outcome: MarketOutcome = params.outcome;
1163
1243
  if (!outcome.marketId) {
1164
- throw new Error(
1244
+ throw new PmxtError(
1165
1245
  "outcome.marketId is not set. Ensure the outcome comes from a fetched market."
1166
1246
  );
1167
1247
  }
@@ -1196,7 +1276,8 @@ export abstract class Exchange {
1196
1276
  const data = this.handleResponse(response);
1197
1277
  return convertOrder(data);
1198
1278
  } catch (error) {
1199
- throw new Error(`Failed to create order: ${error}`);
1279
+ if (error instanceof PmxtError) throw error;
1280
+ throw new PmxtError(`Failed to create order: ${error}`);
1200
1281
  }
1201
1282
  }
1202
1283
 
@@ -1250,14 +1331,18 @@ export abstract class Exchange {
1250
1331
  });
1251
1332
 
1252
1333
  if (!response.ok) {
1253
- const error = await response.json().catch(() => ({}));
1254
- throw new Error(error.error?.message || response.statusText);
1334
+ const body = await response.json().catch(() => ({}));
1335
+ if (body.error && typeof body.error === "object") {
1336
+ throw fromServerError(body.error);
1337
+ }
1338
+ throw new PmxtError(body.error?.message || response.statusText);
1255
1339
  }
1256
1340
 
1257
1341
  const json = await response.json();
1258
1342
  return this.handleResponse(json);
1259
1343
  } catch (error) {
1260
- throw new Error(`Failed to get execution price: ${error}`);
1344
+ if (error instanceof PmxtError) throw error;
1345
+ throw new PmxtError(`Failed to get execution price: ${error}`);
1261
1346
  }
1262
1347
  }
1263
1348