@futdevpro/nts-dynamo 1.7.15 → 1.7.16

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.
@@ -200,13 +200,21 @@ export class DynamoNTS_EndpointParams{
200
200
  msg += this.getPathParamsLogContent(req);
201
201
 
202
202
  Dynamo_Log.error(msg);
203
- Dynamo_Log.log('ERROR:',
203
+ Dynamo_Log.error(
204
+ 'ERROR:',
204
205
  (error as Dynamo_Error)?.flag?.includes('DYNAMO') ?
205
206
  (error as Dynamo_Error).getErrorSimplified() :
206
207
  error,
207
208
  '\n'
208
209
  );
209
210
 
211
+ if ((error as Dynamo_Error)?.flag?.includes('DYNAMO')) {
212
+ if (!(error as Dynamo_Error).additionalContent) {
213
+ (error as Dynamo_Error).additionalContent = {};
214
+ }
215
+ (error as Dynamo_Error).additionalContent.endpointInfo = msg;
216
+ }
217
+
210
218
  await DynamoNTS_GlobalService.globalErrorHandler?.(error, req, res, issuer).catch(err => {
211
219
  Dynamo_Log.warn('DynamoNTS_GlobalService.globalErrorHandler failed to handle error: ', err);
212
220
  Dynamo_Log.warn('It will proceed as normal.');
@@ -3,7 +3,6 @@
3
3
 
4
4
  export class DynamoNTS_SystemControl {
5
5
  init: boolean = false;
6
- /* failed: boolean = false; */
7
6
  started: boolean = false;
8
7
 
9
8
  getReady(): boolean {
@@ -63,7 +63,11 @@ export class DynamoNTS_GlobalService extends DynamoNTS_SingletonService {
63
63
 
64
64
  this.instance.authService = set.authService;
65
65
  this.instance.emailServiceCollection = set.emailServiceCollection;
66
- DynamoNTS_GlobalService.globalErrorHandler = set.errorHandler;
66
+
67
+ DynamoNTS_GlobalService.globalErrorHandler = set.errorHandler ?? (async (error: any) => {
68
+ Dynamo_Log.warn(`globalErrorHandler not set!`);
69
+ Dynamo_Log.error(`ERROR:\n`, error)
70
+ });
67
71
  } catch (error) {
68
72
  Dynamo_Log.error(`Failed to create DynamoNTS_GlobalService.`, error);
69
73
  /* error = new Dynamo_Error({
@@ -222,6 +222,7 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
222
222
  protected logSetup: boolean;
223
223
  protected deepLog: boolean;
224
224
  protected logFn: boolean;
225
+ protected debugLog: boolean;
225
226
 
226
227
  constructor(extended?: boolean){
227
228
  super();
@@ -525,38 +526,60 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
525
526
  */
526
527
  private async startDB(): Promise<void> {
527
528
  if (this.logFn && this.deepLog) console.log('\nfn:. startDB');
529
+
528
530
  await new Promise<void>(
529
531
  async (resolve, reject) => {
530
532
  this.systemControls.mongoose.init = true;
533
+
531
534
  this.mongoose.connection
535
+ .once('open', () => {
536
+ this.systemControls.mongoose.started = true;
537
+ Dynamo_Log.success('\nConnected to MongoDB\n');
538
+
539
+ resolve();
540
+ })
532
541
  .on('error', (error) => {
533
- this.systemControls.mongoose.started = false;
534
- this.constructErrors.push(error);
535
- Dynamo_Log.error('\nUnable to connect to MongoDB server, ERROR: ', error);
536
- reject(
537
- new Dynamo_Error({
542
+ if (!this.systemControls.mongoose.started) {
543
+ this.constructErrors.push(error);
544
+
545
+ if (this.debugLog) Dynamo_Log.error('\nUnable to connect to MongoDB server, ERROR: ', error);
546
+
547
+ const d_error: Dynamo_Error = new Dynamo_Error({
538
548
  ...this._getDefaultErrorSettings(
539
549
  'startDB',
540
550
  error
541
551
  ),
542
-
552
+
543
553
  errorCode: 'NTS-AS0-SDB1',
544
- message: `Unable to connect to MongoDB server, ERROR: ${error}`,
545
- })
546
- );
547
- })
548
- .once('open', () => {
549
- this.systemControls.mongoose.started = true;
550
- Dynamo_Log.success('\nConnected to MongoDB\n');
551
- resolve();
554
+ message: `Unable to start connection to MongoDB server, ERROR: ${error}`,
555
+ });
556
+
557
+ DynamoNTS_GlobalService.globalErrorHandler?.(d_error);
558
+ reject(d_error);
559
+
560
+ } else {
561
+ if (this.debugLog) Dynamo_Log.error('\nMongoDB ERROR: ', error);
562
+
563
+ const d_error: Dynamo_Error = new Dynamo_Error({
564
+ ...this._getDefaultErrorSettings(
565
+ 'mongoose.connection.on(error)',
566
+ error
567
+ ),
568
+
569
+ errorCode: 'NTS-AS0-SDB2',
570
+ message: `MongoDB ERROR: ${error}`,
571
+ });
572
+
573
+ DynamoNTS_GlobalService.globalErrorHandler?.(d_error);
574
+ }
552
575
  });
553
576
 
554
577
  this.mongoose.connect(
555
- this._params.dbUri,
556
- {
557
- useNewUrlParser: true,
558
- useUnifiedTopology: true
559
- }
578
+ this._params.dbUri,
579
+ {
580
+ useNewUrlParser: true,
581
+ useUnifiedTopology: true
582
+ }
560
583
  );
561
584
  }
562
585
  );
@@ -567,6 +590,7 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
567
590
  */
568
591
  private async initExpresses(): Promise<void> {
569
592
  if (this.logFn && this.deepLog) console.log('\nfn:. initExpresses');
593
+
570
594
  if (this._security && this._security !== DynamoNTS_RouteSecurity.secure) {
571
595
  if (!this._portSettings.httpPort) {
572
596
  let errorMsg: string =
@@ -604,11 +628,12 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
604
628
  `\nYou have secure routes, but the certification paths or httpsPort are not set!` +
605
629
  `\nsecurity: ${this._security}` +
606
630
  `\nset...` +
607
- `\n httpsPort and` +
631
+ `\n(missing exact howto...)` +
632
+ /* `\n httpsPort and` +
608
633
  `\n cert: {` +
609
634
  `\n keyPath: FileSystem.PathLike,` +
610
635
  `\n certPath: FileSystem.PathLike,` +
611
- `\n }` +
636
+ `\n }` + */
612
637
  `\nin DynamoBEServer - getRoutingModules() to enable secure routes.`;
613
638
 
614
639
  errorMsg += '\n\nThe routes setted to use secure server:';
@@ -675,37 +700,56 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
675
700
  .listen(this._portSettings.httpsPort, 'localhost', 0, () => {
676
701
  this.systemControls.httpsServer.started = true;
677
702
  Dynamo_Log.success(`\nHTTPS (secure) server is listening on port: ${this._portSettings.httpsPort}`);
703
+
678
704
  resolve();
679
705
  })
680
706
  .on('error', (error) => {
681
- this.systemControls.httpsServer.started = false;
682
- this.constructErrors.push(error);
683
- Dynamo_Log.error(`\nHTTPS (secure) server ERROR`, error);
684
- reject(
685
- new Dynamo_Error({
707
+ if (this.debugLog) Dynamo_Log.error(`\nHTTPS (secure) server ERROR`, error);
708
+
709
+ if (!this.systemControls.httpsServer.started) {
710
+ const d_error: Dynamo_Error = new Dynamo_Error({
686
711
  ...this._getDefaultErrorSettings(
687
712
  'startExpresses',
688
713
  error
689
714
  ),
690
715
 
691
716
  errorCode: 'NTS-AS0-SE1',
692
- message: `HTTPS (secure) server ERROR`,
693
- })
694
- );
695
- })
696
- .on('uncaughtException', (ex) => {
697
- Dynamo_Log.warn(
698
- `\nHTTPS (secure) server uncaughtException`,
699
- new Dynamo_Error({
717
+ message: `HTTPS (secure) start server ERROR`,
718
+ });
719
+
720
+ this.constructErrors.push(d_error);
721
+ DynamoNTS_GlobalService.globalErrorHandler?.(d_error);
722
+
723
+ reject(d_error);
724
+
725
+ } else {
726
+ const d_error: Dynamo_Error = new Dynamo_Error({
700
727
  ...this._getDefaultErrorSettings(
701
- 'startExpresses',
702
- ex
728
+ 'httpsServer.on(error)',
729
+ error
703
730
  ),
704
731
 
705
732
  errorCode: 'NTS-AS0-SE2',
706
- message: `HTTPS (secure) server uncaughtException`,
707
- })
708
- );
733
+ message: `HTTPS (secure) server ERROR`,
734
+ });
735
+
736
+ DynamoNTS_GlobalService.globalErrorHandler?.(d_error);
737
+ }
738
+ })
739
+ .on('uncaughtException', (ex) => {
740
+ const d_error: Dynamo_Error = new Dynamo_Error({
741
+ ...this._getDefaultErrorSettings(
742
+ 'httpsServer.on(uncaughtException)',
743
+ ex
744
+ ),
745
+
746
+ errorCode: 'NTS-AS0-SE3',
747
+ message: `HTTPS (secure) server uncaughtException`,
748
+ });
749
+
750
+ if (this.debugLog) Dynamo_Log.warn(`\nHTTPS (secure) server uncaughtException`, d_error);
751
+
752
+ DynamoNTS_GlobalService.globalErrorHandler?.(d_error);
709
753
  });
710
754
  });
711
755
  }
@@ -717,38 +761,56 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
717
761
  .listen(this._portSettings.httpPort, () => {
718
762
  this.systemControls.httpServer.started = true;
719
763
  Dynamo_Log.success(`\nHTTP (open) server is listening on port: ${this._portSettings.httpPort}`);
764
+
720
765
  resolve();
721
766
  })
722
767
  .on('error', (error) => {
723
- this.systemControls.httpServer.started = false;
724
- this.constructErrors.push(error);
768
+ if (this.debugLog) Dynamo_Log.error(`\nHTTP (open) server ERROR`, error);
725
769
 
726
- Dynamo_Log.error(`\nHTTP (open) server ERROR`, error);
727
- reject(
728
- new Dynamo_Error({
770
+ if (!this.systemControls.httpServer.started) {
771
+ const d_error: Dynamo_Error = new Dynamo_Error({
729
772
  ...this._getDefaultErrorSettings(
730
773
  'startExpresses',
731
774
  error
732
775
  ),
733
776
 
734
777
  errorCode: 'NTS-AS0-SE3',
735
- message: `HTTP (open) server ERROR`,
736
- })
737
- );
738
- })
739
- .on('uncaughtException', (ex) => {
740
- Dynamo_Log.warn(
741
- `\nHTTP (open) server uncaughtException`,
742
- new Dynamo_Error({
778
+ message: `HTTP (open) start server ERROR`,
779
+ });
780
+
781
+ this.constructErrors.push(d_error);
782
+ DynamoNTS_GlobalService.globalErrorHandler?.(d_error);
783
+
784
+ reject(d_error);
785
+
786
+ } else {
787
+ const d_error: Dynamo_Error = new Dynamo_Error({
743
788
  ...this._getDefaultErrorSettings(
744
- 'startExpresses',
745
- ex
789
+ 'httpServer.on(error)',
790
+ error
746
791
  ),
747
792
 
748
793
  errorCode: 'NTS-AS0-SE4',
749
- message: `HTTP (open) server uncaughtException`,
750
- })
751
- );
794
+ message: `HTTP (open) server ERROR`,
795
+ });
796
+
797
+ DynamoNTS_GlobalService.globalErrorHandler?.(d_error);
798
+ }
799
+ })
800
+ .on('uncaughtException', (ex) => {
801
+ const d_error: Dynamo_Error = new Dynamo_Error({
802
+ ...this._getDefaultErrorSettings(
803
+ 'httpServer.on(uncaughtException)',
804
+ ex
805
+ ),
806
+
807
+ errorCode: 'NTS-AS0-SE5',
808
+ message: `HTTP (open) server uncaughtException`,
809
+ });
810
+
811
+ if (this.debugLog) Dynamo_Log.warn(`\nHTTP (open) server uncaughtException`, d_error);
812
+
813
+ DynamoNTS_GlobalService.globalErrorHandler?.(d_error);
752
814
  });
753
815
  });
754
816
  }
@@ -761,19 +823,29 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
761
823
  /**
762
824
  *
763
825
  */
764
- private async expressErrorHandling(err, req, res, next): Promise<void> {
826
+ private async expressErrorHandling(error, req, res, next): Promise<void> {
765
827
  try {
766
- if (err) {
767
- if (DynamoNTS_GlobalService.globalErrorHandler) {
768
- Dynamo_Log.error('unhandled express ERROR (must be a parsing error...)');
769
- await DynamoNTS_GlobalService.globalErrorHandler?.(err, req, res);
770
- } else {
771
- Dynamo_Log.error('unhandled express ERROR (must be a parsing error...)', err);
772
- }
828
+ if (error) {
829
+ const d_error: Dynamo_Error = new Dynamo_Error({
830
+ ...this._getDefaultErrorSettings(
831
+ 'expressErrorHandling',
832
+ error
833
+ ),
834
+
835
+ errorCode: 'NTS-AS0-EEH1',
836
+ message: `Express ERROR`,
837
+ additionalContent: {
838
+ req,
839
+ res,
840
+ },
841
+ });
842
+
843
+ await DynamoNTS_GlobalService.globalErrorHandler?.(d_error, req, res);
844
+
773
845
  } else {
774
846
  Dynamo_Log.H_error(
775
847
  'WTF??? express error; without error?...' +
776
- '\nerr:', err,
848
+ '\nerr:', error,
777
849
  '\nreq:', req,
778
850
  '\nres:', res
779
851
  );