Skip to content

Commit 32d5ca1

Browse files
committedJul 14, 2019
feat(): Error Handeling
1 parent e2d8918 commit 32d5ca1

File tree

8 files changed

+53
-21
lines changed

8 files changed

+53
-21
lines changed
 

‎lib/Core.ts

+31-10
Original file line numberDiff line numberDiff line change
@@ -399,29 +399,48 @@ export const Core = {
399399
app: FabrixApp,
400400
spools: Spool[]
401401
) {
402+
// TODO, eliminate waiting on lifecycle events that will not exist
403+
// https://github.com/fabrix-app/fabrix/issues/14
402404
const validatedEvents = spools.map(spool => `spool:${spool.name}:validated`)
403405
const configuredEvents = spools.map(spool => `spool:${spool.name}:configured`)
404406
const initializedEvents = spools.map(spool => `spool:${spool.name}:initialized`)
405407
const sanityEvents = spools.map(spool => `spool:${spool.name}:sane`)
406408

407-
app.after(configuredEvents).then(async () => {
408-
await this.createDefaultPaths(app)
409-
app.emit('spool:all:configured')
410-
})
409+
app.after(configuredEvents)
410+
.then(async () => {
411+
await this.createDefaultPaths(app)
412+
app.emit('spool:all:configured')
413+
})
414+
.catch(err => {
415+
app.log.error(err)
416+
throw err
417+
})
411418

412419
app.after(validatedEvents)
413420
.then(() => app.emit('spool:all:validated'))
421+
.catch(err => {
422+
app.log.error(err)
423+
throw err
424+
})
414425

415426
app.after(initializedEvents)
416427
.then(() => {
417428
app.emit('spool:all:initialized')
418429
})
430+
.catch(err => {
431+
app.log.error(err)
432+
throw err
433+
})
419434

420435
app.after(sanityEvents)
421436
.then(() => {
422437
app.emit('spool:all:sane')
423438
app.emit('fabrix:ready')
424439
})
440+
.catch(err => {
441+
app.log.error(err)
442+
throw err
443+
})
425444
},
426445

427446
/**
@@ -440,39 +459,41 @@ export const Core = {
440459
.then(() => spool.sanity())
441460
.then(() => app.emit(`spool:${spool.name}:sane`))
442461
.then(() => spool.stage = 'sane')
443-
.catch(this.handlePromiseRejection)
462+
.catch(err => this.handlePromiseRejection(app, err))
444463

445464
app.after((lifecycle.initialize.listen).concat('spool:all:configured'))
446465
.then(() => app.log.debug('spool: initializing', spool.name))
447466
.then(() => spool.stage = 'initializing')
448467
.then(() => spool.initialize())
449468
.then(() => app.emit(`spool:${spool.name}:initialized`))
450469
.then(() => spool.stage = 'initialized')
451-
.catch(this.handlePromiseRejection)
470+
.catch(err => this.handlePromiseRejection(app, err))
452471

453472
app.after((lifecycle.configure.listen).concat('spool:all:validated'))
454473
.then(() => app.log.debug('spool: configuring', spool.name))
455474
.then(() => spool.stage = 'configuring')
456475
.then(() => spool.configure())
457476
.then(() => app.emit(`spool:${spool.name}:configured`))
458477
.then(() => spool.stage = 'configured')
459-
.catch(this.handlePromiseRejection)
478+
.catch(err => this.handlePromiseRejection(app, err))
460479

461480
app.after('fabrix:start')
462481
.then(() => app.log.debug('spool: validating', spool.name))
463482
.then(() => spool.stage = 'validating')
464483
.then(() => spool.validate())
465484
.then(() => app.emit(`spool:${spool.name}:validated`))
466485
.then(() => spool.stage = 'validated')
467-
.catch(this.handlePromiseRejection)
486+
.catch(err => this.handlePromiseRejection(app, err))
468487
},
469488

470489
/**
471490
* Handle a promise rejection
472491
*/
473-
handlePromiseRejection (err: Error): void {
474-
console.error(err)
492+
handlePromiseRejection (app: FabrixApp, err: Error): void {
493+
app.log.error(err)
475494
throw err
495+
496+
return
476497
}
477498

478499
}

‎lib/common/Controller.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { FabrixGeneric } from './Generic'
88
* Fabrix Controller Class.
99
*/
1010
export class FabrixController extends FabrixGeneric {
11-
public app: FabrixApp
1211

1312
constructor (app: FabrixApp) {
13+
super(app)
14+
1415
if (!(app instanceof EventEmitter)) {
1516
throw new Error('The "app" argument must be of type EventEmitter')
1617
}
17-
super(app)
1818
this.app.emit(`controller:${this.id}:constructed`, this)
1919
}
2020

‎lib/common/Generic.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { EventEmitter } from 'events'
12
import { FabrixApp } from '../index'
23

34
export interface FabrixGeneric {
@@ -9,9 +10,16 @@ export interface FabrixGeneric {
910
* Fabrix Generic Class.
1011
*/
1112
export class FabrixGeneric {
12-
public app: FabrixApp
1313
public methods: string[] = []
14-
constructor (app: FabrixApp) {
14+
15+
constructor (
16+
public app: FabrixApp
17+
) {
18+
19+
if (!(app instanceof EventEmitter)) {
20+
throw new Error('The "app" argument must be of type EventEmitter')
21+
}
22+
1523
Object.defineProperties(this, {
1624
app: {
1725
value: app,

‎lib/common/Model.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { FabrixGeneric } from './Generic'
2222
* Fabrix Model Class.
2323
*/
2424
export class FabrixModel extends FabrixGeneric {
25-
public app: FabrixApp
2625
private _datastore: any
2726
private _instance: any
2827
private _config: {[key: string]: any}
@@ -72,11 +71,12 @@ export class FabrixModel extends FabrixGeneric {
7271
* Construct the model and bind the Resolver
7372
*/
7473
constructor (app: FabrixApp, datastore?) {
74+
super(app)
75+
7576
if (!(app instanceof EventEmitter)) {
7677
throw new Error('The "app" argument must be of type EventEmitter')
7778
}
7879

79-
super(app)
8080

8181
this._datastore = datastore
8282

‎lib/common/Policy.ts

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import { FabrixGeneric } from './Generic'
99
* Fabrix Policy Class.
1010
*/
1111
export class FabrixPolicy extends FabrixGeneric {
12-
public app: FabrixApp
13-
1412

1513
/**
1614
* Policy configuration

‎lib/common/Service.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ import { FabrixGeneric } from './Generic'
1010
*/
1111
export class FabrixService extends FabrixGeneric {
1212

13-
constructor (app: FabrixApp) {
13+
constructor (
14+
app: FabrixApp
15+
) {
16+
1417
super(app)
1518

1619
if (!(app instanceof EventEmitter)) {
1720
throw new Error('The "app" argument must be of type EventEmitter')
1821
}
22+
1923
this.app.emit(`service:${this.id}:constructed`, this)
2024
}
2125

‎lib/common/Spool.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export interface Spool {
1414
[key: string]: any
1515
}
1616
export class Spool extends FabrixGeneric {
17-
public app: FabrixApp
1817
private _stage = 'pre'
1918
private _config: ISpoolConfig
2019
private _pkg: any // IPkg
@@ -92,7 +91,7 @@ export class Spool extends FabrixGeneric {
9291
pkg = null,
9392
config = { },
9493
api = { }
95-
}: { pkg?: any, config?: ISpoolConfig, api?: IApi }
94+
}: { pkg?: {[key: string]: any}, config?: ISpoolConfig, api?: IApi }
9695
) {
9796
super(app)
9897
if (!(app instanceof EventEmitter)) {

‎lib/tsconfig.json

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"moduleResolution": "node",
1010
"declaration": true,
1111
"strict": false,
12+
"esModuleInterop": false,
13+
"sourceMap": true,
1214
"noImplicitAny": false,
1315
"typeRoots": [
1416
"node_modules/@types"

0 commit comments

Comments
 (0)
Please sign in to comment.