Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | 1x 1x 1x 12x 12x 15x 11x 9x 12x 37x 12x 12x 25x 37x 36x 1x 8x 8x 8x 8x 8x 5x 5x 3x 5x 8x 5x 5x 5x | import { format } from 'winston'
import { TransformableInfo } from 'logform'
import { LEVEL, MESSAGE, SPLAT } from 'triple-beam'
/**
* Interface to implement an FormatExceptionOptions object
*/
export interface FormatExceptionOptions {
metaKey?: string
}
/**
* Interface to implement an FormatException object
*/
export interface FormatException {
message: string
trace: string[]
}
export interface ParserExceptionInterface {
/**
* Try to generate a FormatException from TransformableInfo
* @param {TransformableInfo | any} obj
* @returns {[FormatException, Error] | null}
*/
getFormatException(
obj: TransformableInfo | any
): [FormatException, Error] | null
/**
* Extract Error object from TransformableInfo message
* @param {TransformableInfo | any} obj
* @returns {Error | null}
*/
extractMessageError(obj: TransformableInfo | any): Error | null
/**
* Extract first Error object from TransformableInfo splat
* @param {TransformableInfo} obj
* @returns {Error | null}
*/
extractSplatError(obj: TransformableInfo): Error | null
/**
* Parse Error object.
* @param {Error} error
* @returns {FormatException}
*/
parseError(error: Error): FormatException
/**
* Parse stack string.
* @param {string} stack
* @returns {FormatException}
*/
parseStack(stack: string): FormatException
}
export class ParserError implements ParserExceptionInterface {
/**
* {@inheritdoc}
*/
public getFormatException(
obj: TransformableInfo | any
): [FormatException, Error] | null {
const err = this.extractMessageError(obj) || this.extractSplatError(obj)
return err ? [this.parseError(err), err] : null
}
/**
* {@inheritdoc}
*/
public extractMessageError(obj: TransformableInfo | any): Error | null {
return this.isErr(obj)
? obj
: obj.message && this.isErr(obj.message)
? obj.message
: null
}
/**
* {@inheritdoc}
*/
public extractSplatError(obj: TransformableInfo | any): Error | null {
return (obj[SPLAT] || []).filter(this.isErr).shift()
}
/**
* {@inheritdoc}
*/
public parseError(error: Error): FormatException {
return this.parseStack(error.stack)
}
/**
* {@inheritdoc}
*/
public parseStack(stack: string): FormatException {
return stack.split('\n').reduce(
(acc, cur, inx) => {
switch (inx) {
case 0:
acc.message = cur
break
default:
acc['trace'].push(cur.trim().replace(/^at /gim, ''))
}
return acc
},
{ message: '', trace: [] }
)
}
/**
* Validate if the object is an Error
* @param obj
* @returns {boolean}
*/
private isErr(obj: any): boolean {
return obj instanceof Error
}
}
/**
* Transform logger info when detected an exception.
*
* @param {TransformableInfo} info
* @param {FormatExceptionOptions} opts
*
* @returns {Format}
*/
export const exception = format(
(info, opts: FormatExceptionOptions = {}): TransformableInfo => {
const { metaKey = 'metadata' } = opts
const parser = new ParserError()
const [exception, error] = parser.getFormatException(info) || []
const metadata = info[metaKey] || {}
const getMessage = (data: any, exc: FormatException) => {
let msg = exc.message
if (typeof data === 'string') {
msg = data
.replace(new RegExp(msg.substr(msg.indexOf(':') + 1)), '')
.trim()
}
return msg
}
if (!error) return info
delete info.stack
delete metadata.stack
return Object.assign({}, info, {
level: info.level || 'error',
message: getMessage(info.message, exception),
[LEVEL]: error[LEVEL] || info.level,
[MESSAGE]: error[MESSAGE] || exception.message,
[metaKey]: { ...metadata, exception },
})
}
)
|