Skip to content

Commit 5788792

Browse files
committed
feat: Nicer console message reporting error in ready or onCrash
1 parent dad37d6 commit 5788792

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

src/CompositeService.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { formatWithOptions } from 'util'
22
import mergeStream from 'merge-stream'
33
import { Service } from './Service'
4-
import {
5-
NormalizedCompositeServiceConfig,
6-
validateAndNormalizeConfig,
7-
} from './validateAndNormalizeConfig'
8-
import { CompositeServiceConfig } from './interfaces/CompositeServiceConfig'
4+
import { NormalizedCompositeServiceConfig } from './validateAndNormalizeConfig'
95
import { Logger } from './Logger'
106
import { mapStreamLines } from './util/stream'
117

@@ -16,8 +12,8 @@ export class CompositeService {
1612
private stopping = false
1713
private logger: Logger
1814

19-
constructor(config: CompositeServiceConfig) {
20-
this.config = validateAndNormalizeConfig(config)
15+
constructor(config: NormalizedCompositeServiceConfig) {
16+
this.config = config
2117

2218
if (this.config.windowsCtrlCShutdown) {
2319
require('generate-ctrl-c-event') // make sure this module loads before we even start

src/Service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class Service {
6161
this.ready = promiseTry(() => this.config.ready(ctx))
6262
.finally(() => this.outputClone.destroy())
6363
.catch(error => {
64-
const prefix = `In \`ready\` function for service '${this.id}'`
64+
const prefix = `In \`service.${this.id}.ready\``
6565
this.handleFatalError(`${prefix}: ${maybeErrorText(error)}`)
6666
return never()
6767
})
@@ -103,7 +103,7 @@ export class Service {
103103
try {
104104
await this.config.onCrash(ctx)
105105
} catch (error) {
106-
const prefix = `In \`onCrash\` function for service ${this.id}`
106+
const prefix = `In \`service.${this.id}.onCrash\``
107107
this.handleFatalError(`${prefix}: ${maybeErrorText(error)}`)
108108
await never()
109109
}

src/startCompositeService.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CompositeServiceConfig } from './interfaces/CompositeServiceConfig'
2+
import { validateAndNormalizeConfig } from './validateAndNormalizeConfig'
23
import { CompositeService } from './CompositeService'
34

45
let started = false
@@ -10,6 +11,7 @@ export function startCompositeService(config: CompositeServiceConfig) {
1011
if (started) {
1112
throw new Error('Already started a composite service in this process')
1213
}
13-
new CompositeService(config)
14+
const normalizedConfig = validateAndNormalizeConfig(config)
15+
new CompositeService(normalizedConfig)
1416
started = true
1517
}

test/integration/crashing.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe('crashing', () => {
105105
"first | Started 🚀",
106106
" (debug) Started service 'first'",
107107
" (debug) Starting service 'second'...",
108-
" (error) Fatal error: In \`ready\` function for service 'second': TypeError: Cannot read property 'bar' of undefined",
108+
" (error) Fatal error: In \`service.second.ready\`: TypeError: Cannot read property 'bar' of undefined",
109109
"<stack trace>",
110110
" (debug) Stopping composite service...",
111111
" (debug) Stopping service 'second'...",
@@ -140,7 +140,7 @@ describe('crashing', () => {
140140
"second | Crashing",
141141
" (info) Service 'second' crashed",
142142
"isServiceReady: false",
143-
" (error) Fatal error: In \`onCrash\` function for service second: Error: Crash",
143+
" (error) Fatal error: In \`service.second.onCrash\`: Error: Crash",
144144
"<stack trace>",
145145
" (debug) Stopping composite service...",
146146
" (debug) Stopping service 'first'...",
@@ -168,7 +168,7 @@ describe('crashing', () => {
168168
"second | Crashing",
169169
" (info) Service 'second' crashed",
170170
"isServiceReady: true",
171-
" (error) Fatal error: In \`onCrash\` function for service second: Error: Crash",
171+
" (error) Fatal error: In \`service.second.onCrash\`: Error: Crash",
172172
"<stack trace>",
173173
" (debug) Stopping composite service...",
174174
" (debug) Stopping service 'third'...",
@@ -207,7 +207,7 @@ describe('crashing', () => {
207207
" (info) Restarting service 'second'",
208208
"second | Crashing",
209209
" (info) Service 'second' crashed",
210-
" (error) Fatal error: In \`onCrash\` function for service second: Error: Crashed three times",
210+
" (error) Fatal error: In \`service.second.onCrash\`: Error: Crashed three times",
211211
"<stack trace>",
212212
" (debug) Stopping composite service...",
213213
" (debug) Stopping service 'first'...",

0 commit comments

Comments
 (0)