5htp-core 0.3.7-1 → 0.3.7-3

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "5htp-core",
3
3
  "description": "Convenient TypeScript framework designed for Performance and Productivity.",
4
- "version": "0.3.7-1",
4
+ "version": "0.3.7-3",
5
5
  "author": "Gaetan Le Gac (https://github.com/gaetanlegac)",
6
6
  "repository": "git://github.com/gaetanlegac/5htp-core.git",
7
7
  "license": "MIT",
@@ -96,16 +96,20 @@ export class Application<
96
96
  // Application itself doesnt have configuration
97
97
  // Configuration must be handled by application services
98
98
  super(self, {}, {}, self);
99
-
100
- // We can't pass this in super so we assign here
101
- this.parent = this;
102
- this.app = this;
103
-
99
+
104
100
  // Handle unhandled crash
101
+ this.on('error', e => this.unhandledRejection(e));
102
+
105
103
  process.on('unhandledRejection', (error: any, promise: any) => {
104
+ console.log("unhandledRejection");
106
105
  // We don't log the error here because it's the role of the app to decidehiw to log errors
107
106
  this.runHook('error', error);
108
107
  });
108
+
109
+ // We can't pass this in super so we assign here
110
+ this.parent = this;
111
+ this.app = this;
112
+
109
113
  }
110
114
 
111
115
  /*----------------------------------
@@ -126,12 +130,8 @@ export class Application<
126
130
 
127
131
  console.log("Build date", BUILD_DATE);
128
132
  console.log("Core version", CORE_VERSION);
129
- console.log(`5HTP Core`, process.env.npm_package_version);
130
133
  const startTime = Date.now();
131
134
 
132
- // Handle errors & crashs
133
- this.on('error', e => this.Console.createBugReport(e))
134
-
135
135
  console.info(`[boot] Start services`);
136
136
  await this.startServices();
137
137
  this.debug && console.info(`[boot] Services are ready`);
@@ -147,13 +147,6 @@ export class Application<
147
147
  public async ready() {
148
148
 
149
149
 
150
- }
151
-
152
- // Default error handler
153
- public async reportBug( bug: ServerBug ) {
154
-
155
- console.error( bug.error );
156
-
157
150
  }
158
151
 
159
152
  public async shutdown() {
@@ -208,6 +201,35 @@ export class Application<
208
201
  unused.join(', '));
209
202
  }
210
203
 
204
+ /*----------------------------------
205
+ - ERROR HANDLING
206
+ ----------------------------------*/
207
+ private async unhandledRejection(rejection: Error) {
208
+ if (this.Console) {
209
+ try {
210
+
211
+ this.Console.createBugReport(rejection);
212
+
213
+ } catch (consoleError) {
214
+ console.error(
215
+ "Unhandled rejection", rejection,
216
+ "Failed to transmiss the previous error to console:", consoleError
217
+ );
218
+ process.exit(1);
219
+ }
220
+ } else {
221
+ console.error("Unhandled rejection", rejection);
222
+ process.exit(1);
223
+ }
224
+ }
225
+
226
+ // Default error handler
227
+ public async reportBug( bug: ServerBug ) {
228
+
229
+ console.error( bug.error );
230
+
231
+ }
232
+
211
233
  }
212
234
 
213
235
  export default Application
@@ -144,6 +144,7 @@ export default abstract class Service<
144
144
  // this.use immediatly instanciate the subservice for few reasons:
145
145
  // - The subservice instance can be accesses from another service in the constructor, no matter the order of loading of the services
146
146
  // - Consistency: the subserviuce proprties shouldn't be assogned to two different values according depending on the app lifecycle
147
+ // Don't throw errors here since process.on('unhandledRejection') has not been configurated at this step (constructor ran after properties initialization)
147
148
  public use<
148
149
  TInstalledServices extends this["app"]["servicesContainer"]["allServices"],
149
150
  TServiceId extends keyof TInstalledServices,
@@ -172,8 +173,10 @@ export default abstract class Service<
172
173
  if (registered === undefined) {
173
174
  if (serviceUseOptions.optional)
174
175
  return undefined;
175
- else
176
- throw new Error(`Unable to use service "${serviceId}": This one hasn't been setup.`);
176
+ else {
177
+ console.error(`Unable to use service "${serviceId}": This one hasn't been setup.`);
178
+ process.exit(1);
179
+ }
177
180
  }
178
181
 
179
182
  // Bind subservices
@@ -198,10 +201,11 @@ export default abstract class Service<
198
201
  ServiceClass = registered.metas.class().default;
199
202
  } catch (error) {
200
203
  console.error("Failed to get the class of the", registered.metas.id, "service:", error);
201
- process.exit();
204
+ process.exit(1);
202
205
  }
203
206
 
204
207
  // Create class instance
208
+ this.config.debug && console.log(`[app] Instanciate service`, registered.metas.id);
205
209
  let service;
206
210
  try {
207
211
  service = new ServiceClass(
@@ -212,21 +216,23 @@ export default abstract class Service<
212
216
  )
213
217
  } catch (error) {
214
218
  console.error("Failed to instanciate class of the", registered.metas.id, "service:", error);
215
- process.exit();
219
+ process.exit(1);
216
220
  }
217
221
 
218
222
  // Hande custom instance getter (ex: SQL callable class)
223
+ this.config.debug && console.log(`[app] Get service instance for`, registered.metas.id);
219
224
  let serviceInstance;
220
225
  try {
221
226
  serviceInstance = service.getServiceInstance();
222
227
  } catch (error) {
223
228
  console.error("Failed to get service instance for the ", registered.metas.id, "service:", error);
224
- process.exit();
229
+ process.exit(1);
225
230
  }
226
231
 
227
232
  // Bind his own metas
228
233
  service.metas = registered.metas;
229
234
  ServicesContainer.allServices[ registered.metas.id ] = serviceInstance;
235
+ this.config.debug && console.log(`[app] Service`, registered.metas.id, 'Loaded');
230
236
 
231
237
  return serviceInstance;
232
238
  }
@@ -270,7 +276,7 @@ export default abstract class Service<
270
276
  await service.started.catch(e => {
271
277
  console.error("Catched error while starting service " + serviceScope, e);
272
278
  if (this.app.env.profile === 'prod')
273
- process.exit();
279
+ process.exit(1);
274
280
  else
275
281
  throw e;
276
282
  })
@@ -134,8 +134,13 @@ export default class Console extends Service<Config, Hooks, Application, Service
134
134
  /*----------------------------------
135
135
  - LIFECYCLE
136
136
  ----------------------------------*/
137
+ /*
138
+ WARN: This service should depend on the less services as possible, and be usable ASAP.
139
+ So bug reports can be sent at any state of the app, includoing thre most early
140
+ */
141
+ public constructor( parent: Application, config: Config, subservices: {}, app: Application ) {
137
142
 
138
- protected async start() {
143
+ super(parent, config, subservices, app);
139
144
 
140
145
  const origLog = console.log
141
146
 
@@ -201,13 +206,10 @@ export default class Console extends Service<Config, Hooks, Application, Service
201
206
  setInterval(() => this.clean(), 10000);
202
207
  }
203
208
 
204
- public async ready() {
205
-
206
- }
207
-
208
- public async shutdown() {
209
-
210
- }
209
+ // Avoid to use lifecycle functions
210
+ protected async start() {}
211
+ public async ready() {}
212
+ public async shutdown() {}
211
213
 
212
214
  /*----------------------------------
213
215
  - LOGS FORMATTING