Skip to content

Commit 146e203

Browse files
committed
error handling improvements
1 parent 50705ec commit 146e203

File tree

12 files changed

+136
-142
lines changed

12 files changed

+136
-142
lines changed

__test__/use-cases/make-relations.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ function requireRemoteObject (model, relation, broker, ...args) {
3636
})
3737
}
3838

39-
const broker = new EventEmitter()
39+
const broker = new EventEmitter({ captureRejections: true })
40+
broker.on('error', err => console.log(err))
4041

4142
const model = {
4243
getName: () => 'model1',

src/adapters/datasources/datasource-mongodb.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,14 @@ export class DataSourceMongoDb extends DataSourceMemory {
6060
}
6161

6262
async collection () {
63-
try {
64-
return (await this.connection()).db(this.name).collection(this.name)
65-
} catch (error) {
66-
console.error({ fn: this.collection.name, error })
67-
throw error
68-
}
63+
return (await this.connection()).db(this.name).collection(this.name)
6964
}
7065

7166
/**
7267
* @override
7368
* @param {{
7469
* hydrate:function(Map<string,import("../../domain").Model>),
75-
* serializer:import("../../lib/serializer").Serializer
70+
* seriali zer:import("../../lib/serializer").Serializer
7671
* }} options
7772
*/
7873
load ({ hydrate, serializer }) {

src/domain/bind-adapters.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const debug = /true/i.test(process.env.DEBUG)
4+
35
function DefaultInboundAdapter (port) {
46
return async function ({ model, args }) {
57
return port.apply(model, args)
@@ -8,28 +10,35 @@ function DefaultInboundAdapter (port) {
810

911
/**
1012
* In a hex arch, ports and adapters control I/O between
11-
* the application core (domain) and the outside world.
12-
* Either a port invokes an outbound adapter or it is invoked
13-
* by an inbound one. This function calls adapter factory
14-
* functions to inject each adapter with its port or service
15-
* dependency.
16-
*
17-
* It returns an object containing the set of port functions
18-
* defined for the domain model in the model spec. These functions
19-
* are invoked by a universal function that handles error recovery,
13+
* the application core and the outside world. Inbound
14+
* adapters invoke ports and ports invoke outbound adapters.
15+
* Optionally, outbound adapters invoke services.
16+
*
17+
* To set the above each adapter's factory function
18+
* to inject its port or service dependency--I.e. to bind
19+
* it to a port or service.
20+
*
21+
* It returns an object containing the set of port functions
22+
* defined in the model spec for the domain model. These functions
23+
* are invoked by a port function, which handles error recovery,
2024
* instrumentation, authorization, flow control and other port features.
2125
*
22-
* @param {import('.').ports} ports - domain interfaces
23-
* @param {{[x:string]:function(*):function(*):any}} adapters - service adapters
24-
* @param {*} [services] - (micro-)services
26+
* @param {{
27+
* portSpec:import('.').ports
28+
* ports:{[x:string]:function()}
29+
* adapters:{[x:string]:function()}
30+
* services:{[x:string]:function()}
31+
* }}
32+
*
2533
*/
2634
export default function bindAdapters ({
2735
ports,
2836
adapters,
2937
services,
3038
portSpec
3139
} = {}) {
32-
if (!portSpec || !adapters) {
40+
if (!portSpec || !adapters || !ports) {
41+
debug && console.debug('missing params')
3342
return
3443
}
3544

@@ -44,26 +53,18 @@ export default function bindAdapters ({
4453

4554
return Object.keys(portSpec)
4655
.map(portName => {
47-
try {
48-
const spec = portSpec[portName]
49-
const type = spec.type
50-
const port = ports[portName]
51-
const adapter = adapters[portName]
52-
const service = services[spec.service]
53-
54-
if (!adapter && type === 'outbound') {
55-
console.warn('no adapter for port', portName, spec)
56-
return
57-
}
56+
const spec = portSpec[portName]
57+
const type = spec?.type ? spec.type : null
58+
const port = ports[portName]
59+
const adapter = adapters[portName]
60+
const service = services && spec.service ? services[spec.service] : null
5861

59-
return bindings[type](portName, port, adapter, service)
60-
} catch (error) {
61-
console.warn({
62-
fn: bindAdapters.name,
63-
error,
64-
spec: portSpec[portName]
65-
})
62+
if (!spec || !type || !port || !adapter) {
63+
debug && console.debug('bad port configuration', portName, spec)
64+
return
6665
}
66+
67+
return bindings[type](portName, port, adapter, service)
6768
})
6869
.reduce((p, c) => ({ ...p, ...c }))
6970
}

src/domain/make-ports.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function setPortTimeout (options) {
6161
// Retry the port on timeout
6262
const timerId = setTimeout(async () => {
6363
// Notify interested parties
64-
await model.emit(portTimeout(model.getName(), portName), timerArgs)
64+
model.emit(portTimeout(model.getName(), portName), timerArgs)
6565
// Invoke optional custom handler
6666
if (handler) handler(options)
6767
// Count retries by passing `timerArgs` to ourselves on the stack
@@ -230,7 +230,7 @@ export default function makePorts (ports, adapters, broker) {
230230

231231
// Signal the next port to run.
232232
if (rememberPort) {
233-
await model.emit(portConf.producesEvent, portName)
233+
model.emit(portConf.producesEvent, portName)
234234
}
235235

236236
// the result can be something other than a model

src/domain/mixins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export const withDeserializers = (...funcs) => o => {
129129
export const withbroker = broker => o => {
130130
return {
131131
...o,
132-
async emit (eventName, eventData) {
132+
emit (eventName, eventData) {
133133
broker.notify(eventName, eventData)
134134
},
135135
subscribe (eventName, callback) {

src/domain/model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @property {function()} toJSON - de/serialization logic
1515
* @property {function(eventName,function(eventName,Model):void)} addListener listen
1616
* for domain events
17-
* @property {function(eventName,Model):Promise<void>} emit emit a domain event
17+
* @property {function(eventName,Model):void} emit emit a domain event
1818
* @property {function()} [mixinMethod] - when the user
1919
* specifies a mixin, it is applied to the model on creation - adding methods is
2020
* a common result.
@@ -239,7 +239,7 @@ const Model = (() => {
239239
* @param {boolean} [forward] - forward event to service mesh,
240240
* defaults to `false`
241241
*/
242-
async emit (eventName, eventData, options) {
242+
emit (eventName, eventData, options) {
243243
broker.notify(
244244
eventName,
245245
{

src/domain/orchestrator.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function runWorkflow ({ wfName }) {
3535
DataSourceFactory.getSharedDataSource(wfName),
3636
wfName
3737
)
38-
await model.emit(wfName)
38+
model.emit(wfName)
3939
console.info(wfName, 'workflow started')
4040
}
4141

@@ -45,23 +45,21 @@ export async function runWorkflow ({ wfName }) {
4545
*
4646
* @param {Array<import(".").Model>} list
4747
*/
48-
export async function resumeWorkflow (list) {
48+
export function resumeWorkflow (list) {
4949
if (list?.length > 0) {
50-
await Promise.all(
51-
list.map(async function (model) {
52-
const history = model.getPortFlow()
53-
const ports = model.getSpec().ports
50+
list.forEach(function (model) {
51+
const history = model.getPortFlow()
52+
const ports = model.getSpec().ports
5453

55-
if (history?.length > 0 && !model.compensate) {
56-
const lastPort = history.length - 1
57-
const nextPort = ports[history[lastPort]].producesEvent
54+
if (history?.length > 0 && !model.compensate) {
55+
const lastPort = history.length - 1
56+
const nextPort = ports[history[lastPort]].producesEvent
5857

59-
if (nextPort && history[lastPort] !== 'workflowComplete') {
60-
await async(model.emit(nextPort, resumeWorkflow.name))
61-
}
58+
if (nextPort && history[lastPort] !== 'workflowComplete') {
59+
model.emit(nextPort, resumeWorkflow.name)
6260
}
63-
})
64-
).catch(error => console.error(error))
61+
}
62+
})
6563
}
6664
}
6765

0 commit comments

Comments
 (0)