-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(watchers): add exceptions and log watchers
- Loading branch information
Showing
3 changed files
with
180 additions
and
9 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
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,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 {} | ||
|
||
} |
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,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 {} | ||
|
||
} |