-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
680 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { HealthIndicatorResult } from '../../health-indicator'; | ||
|
||
export interface ErrorLogger { | ||
getErrorMessage(message: string, causes: HealthIndicatorResult): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Provider } from '@nestjs/common'; | ||
import { ErrorLogStyle } from '../../terminus-options.interface'; | ||
import { ErrorLogger } from './error-logger.interface'; | ||
import { JsonErrorLogger } from './json-error-logger.service'; | ||
import { PrettyErrorLogger } from './pretty-error-logger.service'; | ||
|
||
export const ERROR_LOGGER = 'TERMINUS_ERROR_LOGGER'; | ||
|
||
export function getErrorLoggerProvider( | ||
errorLogStyle?: ErrorLogStyle, | ||
): Provider<ErrorLogger> { | ||
switch (errorLogStyle) { | ||
case 'pretty': | ||
return { | ||
provide: ERROR_LOGGER, | ||
useClass: PrettyErrorLogger, | ||
}; | ||
default: | ||
return { | ||
provide: ERROR_LOGGER, | ||
useClass: JsonErrorLogger, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { ErrorLogger } from './error-logger.interface'; | ||
import { JsonErrorLogger } from './json-error-logger.service'; | ||
import { PrettyErrorLogger } from './pretty-error-logger.service'; | ||
|
||
export const ERROR_LOGGERS: Type<ErrorLogger>[] = [ | ||
JsonErrorLogger, | ||
PrettyErrorLogger, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ErrorLogger } from './error-logger.interface'; | ||
|
||
@Injectable() | ||
export class JsonErrorLogger implements ErrorLogger { | ||
getErrorMessage(message: string, causes: any) { | ||
return `${message} ${JSON.stringify(causes)}`; | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
lib/health-check/error-logger/pretty-error-logger.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { PrettyErrorLogger } from './pretty-error-logger.service'; | ||
|
||
const GREEN = '\x1b[0m\x1b[32m'; | ||
const RED = '\x1b[0m\x1b[31m'; | ||
const STOP_COLOR = '\x1b[0m'; | ||
|
||
describe('PrettyErrorLogger', () => { | ||
let prettyErrorLogger: PrettyErrorLogger; | ||
|
||
beforeEach(async () => { | ||
const module = Test.createTestingModule({ | ||
providers: [PrettyErrorLogger], | ||
}); | ||
const context = await module.compile(); | ||
|
||
prettyErrorLogger = context.get(PrettyErrorLogger); | ||
}); | ||
|
||
it('should print one "up" results', () => { | ||
const message = prettyErrorLogger.getErrorMessage('message', { | ||
dog: { | ||
status: 'up', | ||
}, | ||
}); | ||
|
||
expect(message).toBe(`message | ||
${GREEN}┌ ✅ dog ────────┐ | ||
│ │ | ||
│ status: up │ | ||
│ │ | ||
└────────────────┘${STOP_COLOR} | ||
`); | ||
}); | ||
|
||
it('should print one "down" results', () => { | ||
const message = prettyErrorLogger.getErrorMessage('message', { | ||
dog: { | ||
status: 'down', | ||
}, | ||
}); | ||
|
||
expect(message).toBe(`message | ||
${RED}┌ ❌ dog ──────────┐ | ||
│ │ | ||
│ status: down │ | ||
│ │ | ||
└──────────────────┘${STOP_COLOR} | ||
`); | ||
}); | ||
|
||
it('should print "up" and "down" results', () => { | ||
const message = prettyErrorLogger.getErrorMessage('message', { | ||
dog: { | ||
status: 'up', | ||
}, | ||
pug: { | ||
status: 'down', | ||
}, | ||
}); | ||
|
||
expect(message).toBe(`message | ||
${GREEN}┌ ✅ dog ────────┐ | ||
│ │ | ||
│ status: up │ | ||
│ │ | ||
└────────────────┘${STOP_COLOR} | ||
${RED}┌ ❌ pug ──────────┐ | ||
│ │ | ||
│ status: down │ | ||
│ │ | ||
└──────────────────┘${STOP_COLOR} | ||
`); | ||
}); | ||
|
||
it('should print object details', () => { | ||
const message = prettyErrorLogger.getErrorMessage('message', { | ||
dog: { | ||
status: 'up', | ||
foo: 'bar', | ||
}, | ||
}); | ||
|
||
expect(message).toBe(`message | ||
${GREEN}┌ ✅ dog ────────┐ | ||
│ │ | ||
│ status: up │ | ||
│ foo: bar │ | ||
│ │ | ||
└────────────────┘${STOP_COLOR} | ||
`); | ||
}); | ||
|
||
it('should print nested object details', () => { | ||
const message = prettyErrorLogger.getErrorMessage('message', { | ||
dog: { | ||
status: 'up', | ||
foo: { | ||
bar: 'baz', | ||
}, | ||
}, | ||
}); | ||
|
||
expect(message).toBe(`message | ||
${GREEN}┌ ✅ dog ──────────┐ | ||
│ │ | ||
│ status: up │ | ||
│ foo: │ | ||
│ - bar: baz │ | ||
│ │ | ||
└──────────────────┘${STOP_COLOR} | ||
`); | ||
}); | ||
|
||
it('should print array details', () => { | ||
const message = prettyErrorLogger.getErrorMessage('message', { | ||
dog: { | ||
status: 'up', | ||
foo: ['bar', 'baz'], | ||
}, | ||
}); | ||
|
||
expect(message).toBe(`message | ||
${GREEN}┌ ✅ dog ────────┐ | ||
│ │ | ||
│ status: up │ | ||
│ foo: │ | ||
│ - 0: bar │ | ||
│ - 1: baz │ | ||
│ │ | ||
└────────────────┘${STOP_COLOR} | ||
`); | ||
}); | ||
|
||
it('should print symbol details', () => { | ||
const message = prettyErrorLogger.getErrorMessage('message', { | ||
dog: { | ||
status: 'up', | ||
foo: Symbol('TEST'), | ||
}, | ||
}); | ||
|
||
expect(message).toBe(`message | ||
${GREEN}┌ ✅ dog ───────────────┐ | ||
│ │ | ||
│ status: up │ | ||
│ foo: Symbol(TEST) │ | ||
│ │ | ||
└───────────────────────┘${STOP_COLOR} | ||
`); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
lib/health-check/error-logger/pretty-error-logger.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { HealthIndicatorResult } from '../../health-indicator'; | ||
import { ErrorLogger } from './error-logger.interface'; | ||
import * as boxen from 'boxen'; | ||
|
||
const GREEN = '\x1b[0m\x1b[32m'; | ||
const RED = '\x1b[0m\x1b[31m'; | ||
const STOP_COLOR = '\x1b[0m'; | ||
|
||
@Injectable() | ||
export class PrettyErrorLogger implements ErrorLogger { | ||
private printIndent(level: number) { | ||
if (level === 0) { | ||
return ''; | ||
} | ||
return `${' '.repeat(level * 2)}- `; | ||
} | ||
|
||
private printIndicatorSummary(result: any, level = 0) { | ||
const messages: string[] = []; | ||
for (const [key, value] of Object.entries(result)) { | ||
if (typeof value === 'object' && value !== null) { | ||
messages.push( | ||
`${this.printIndent(level)}${key}:\n${this.printIndicatorSummary( | ||
value, | ||
level + 1, | ||
)}`, | ||
); | ||
} else { | ||
const val = (value as any)?.toString | ||
? (value as any).toString() | ||
: value; | ||
messages.push(`${this.printIndent(level)}${key}: ${val}`); | ||
} | ||
} | ||
return messages.join('\n'); | ||
} | ||
|
||
private printSummary(result: HealthIndicatorResult) { | ||
let message = ''; | ||
|
||
for (const [key, value] of Object.entries(result)) { | ||
const summary = this.printIndicatorSummary(value); | ||
|
||
if (value.status === 'up') { | ||
message += | ||
GREEN + | ||
(boxen as any)(summary, { | ||
padding: 1, | ||
title: `✅ ${key}`, | ||
}) + | ||
STOP_COLOR + | ||
'\n'; | ||
} | ||
if (value.status === 'down') { | ||
message += | ||
RED + | ||
(boxen as any)(summary, { | ||
padding: 1, | ||
title: `❌ ${key}`, | ||
}) + | ||
STOP_COLOR + | ||
'\n'; | ||
} | ||
} | ||
return message; | ||
} | ||
|
||
getErrorMessage(message: string, causes: HealthIndicatorResult) { | ||
return `${message}\n\n${this.printSummary(causes)}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type ErrorLogStyle = 'pretty' | 'json'; | ||
|
||
export interface TerminusModuleOptions { | ||
errorLogStyle: ErrorLogStyle; | ||
} |
Oops, something went wrong.