1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73import { Injectable } from '@nestjs/common';
import * as boxen from 'boxen';
import { type ErrorLogger } from './error-logger.interface';
import { type HealthIndicatorResult } from '../../health-indicator';
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)}`;
}
}