Skip to content

Commit

Permalink
feat(watchers): add exceptions and log watchers
Browse files Browse the repository at this point in the history
  • Loading branch information
Melchyore committed May 25, 2019
1 parent e3e8b9c commit 1475241
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/EntryType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
*/

enum EntryType {
REQUEST = 'request',
COMMAND = 'command',
MODEL = 'model',
MAIL = 'mail',
LOG = 'log',
QUERY = 'query',
EVENT = 'event',
SCHEDULE = 'schedule'
// More's coming!
DEFAULT = 'watcher',
REQUEST = 'request',
COMMAND = 'command',
MODEL = 'model',
MAIL = 'mail',
LOG = 'log',
QUERY = 'query',
EVENT = 'event',
SCHEDULE = 'schedule',
EXCEPTION = 'exception',
VIEW = 'view'
// TODO: More types.
}

export default EntryType
113 changes: 113 additions & 0 deletions src/Watchers/ExceptionWatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* File: ExceptionWatcher.ts
* Project: adoscope
* Author: Paradox
*
* Copyright (c) 2019 Paradox.
*/

import { promises } from 'fs'
import * as path from 'path'
import * as _ from 'lodash'

import { RequestContract as Request } from '../Contracts/Request'

import BaseException from '../Exceptions/BaseException'
import EntryType from '../EntryType'
import Adoscope from '../Adoscope'
import Utils from '../lib/Utils'
import Watcher from './Watcher'

export default class ExceptionWatcher extends Watcher {

constructor (
private _app: Adoscope,
private _stdWrite = process.stderr.write,
private _exceptions: Array<BaseException> = []
) {
super(_app.config)

this._setupWriter()
}

/**
* Overrides **stderr** `write` function with custom writer.
*
* @private
*
* @method _setupWriter
*
* @returns {*}
*
* @memberof ExceptionWatcher
*/
private _setupWriter (): any {
// @ts-ignore
process.stderr.write = (string: string, encoding: string, fd: any) => {
let args = _.toArray(arguments)
args[0] = this._interceptConsoleExceptions(string)

this._stdWrite.apply(process.stderr, args)
}
}

private _interceptConsoleExceptions (string: string): string {
this.add(string)

return string
}

private async _adoscopeExceptions (): Promise<Array<string>> {
const exceptionsPath = path.join(__dirname, '../', 'Exceptions')
const exceptions = await promises.readdir(exceptionsPath)

return _.pull(_.map(exceptions, (file: string) => file.replace('.ts', '')), 'BaseException')
}

private _reset (): void {
process.stderr.write = this._stdWrite
}

private async _approveException (exception: BaseException): Promise<boolean> {
if (exception) {
const adoscopeExceptions = await this._adoscopeExceptions()

if (_.includes([...this._watcherConfig.options.ignore, ...adoscopeExceptions], exception.name)) {
return false
}

return true
}

return false
}

public get type (): EntryType {
return EntryType.EXCEPTION
}

public async add (exception: BaseException | string, request?: Request): Promise<void> {
// TODO: attach exceptions to requests.
try {
const exceptionObject = typeof exception === 'string' ? Utils.parseStringException(exception) : exception

if (await this._approveException(exceptionObject)) {
// TODO: remove this shit later.
this._exceptions.push(exceptionObject)

if (await this._app.isRecording()) {
await this._store(this.type, _.pick(exceptionObject, ['name', 'code', 'status', 'message', 'stack']))
}
}
} catch (e) {
console.error(e)
}
}

public get exceptions (): BaseException[] {
return this._exceptions
}

public record (): void {}

}
55 changes: 55 additions & 0 deletions src/Watchers/LogWatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* File: LogWatcher.ts
* Project: adoscope
* Author: Paradox
*
* Copyright (c) 2019 Paradox.
*/

import * as _ from 'lodash'

import EntryType from '../EntryType'
import Adoscope from '../Adoscope'
import Utils from '../lib/Utils'
import Watcher from './Watcher'

const intercept = require('intercept-stdout')

export default class LogWatcher extends Watcher {

constructor (
private _app: Adoscope,
private _stdWrite = process.stdout.write,
private _logs: Array<string> = [],
) {
super(_app.config)

intercept(async (text: string) => {
if (await this._app.isRecording()) {
if (!text.trim().startsWith('\u001b[32m✓')) {
const log = Utils.stripANSIColor(text)
let data = {}

if (typeof log === 'string') {
data = {
level: 'log',
text: log
}
} else {
data = log
}

this._store(this.type, data)
}
}
})
}

public get type (): EntryType {
return EntryType.LOG
}


public record (): void {}

}

0 comments on commit 1475241

Please sign in to comment.