Skip to content

Commit

Permalink
feat(ayamari): prettify stack to static method (#83)
Browse files Browse the repository at this point in the history
* feat(ayamari): prettify stack to static method

* improved cause by condition
  • Loading branch information
sviridoff authored Jan 4, 2024
1 parent d4410b0 commit 9107e51
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
24 changes: 17 additions & 7 deletions packages/ayamari/src/__tests__/ayamari_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,30 @@ describe('Ayamari', () => {
it('should print pretty stack', () => {
const { errFn } = new Ayamari({
injectStack: true,
color: false,
});
const nativeErr = new Error('native err');
const err = errFn.Fail('err', {
cause: nativeErr,
});
assert.match(err.prettyStack(), /Fail \[575\]: err/);
assert.match(Ayamari.prettifyStack(err, false), /Fail \[575\]: err/);
});

it('should print pretty stack without injectStack', () => {
const { errFn } = new Ayamari({
color: false,
describe('when injectStack is false', () => {
it('should print pretty stack', () => {
const { errFn } = new Ayamari();
const nativeErr = new Error('native err: example');
const err = errFn.Fail('err', {
cause: nativeErr,
});
assert.match(Ayamari.prettifyStack(err, false), /Fail \[575\]: err/);
});

describe('when cause is null', () => {
it('should print pretty stack', () => {
const { errFn } = new Ayamari();
const err = errFn.Fail('err');
assert.match(Ayamari.prettifyStack(err, false), /Fail \[575\]: err/);
});
});
const err = errFn.Fail('err');
assert.match(err.prettyStack(), /Fail \[575\]: err/);
});
});
19 changes: 6 additions & 13 deletions packages/ayamari/src/ayamari.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type Entries<T> = [keyof T, ValueOf<T>][];
export interface AyamariGlobalOpts<CustomErrCode> {
levelValue?: number;
injectStack?: boolean;
color?: boolean;
customErrCode?: CustomErrCode;
}

Expand All @@ -20,7 +19,6 @@ export interface AyamariOpts {
meta?: unknown;
injectStack?: boolean;
levelValue?: number;
color?: boolean;
}

export interface AyamariErr {
Expand All @@ -30,7 +28,6 @@ export interface AyamariErr {
stack: string;
cause: AyamariErr | Error | null | undefined;
levelValue: number;
prettyStack(): string;
createdAt: string;
meta: any;
}
Expand Down Expand Up @@ -85,13 +82,11 @@ export class Ayamari<CustomErrCode> {
>();
#injectStack: boolean;
#levelValue: number;
#color: boolean;

constructor(opts: AyamariGlobalOpts<CustomErrCode> = {}) {
this.#injectStack = opts.injectStack ?? false;
this.#levelValue =
opts.levelValue ?? Ayamari.level.info;
this.#color = opts.color ?? true;
if (opts.customErrCode) {
this.errCode = {
...this.errCode,
Expand Down Expand Up @@ -133,19 +128,13 @@ export class Ayamari<CustomErrCode> {
name,
message: msg,
code: errCode,
stack: opts.cause?.stack || `${name}: ${msg}`,
stack: `${name}: ${msg}`,
cause: opts.cause || null,
meta: opts.meta ?? null,
levelValue: opts.levelValue ?? this.#levelValue,
prettyStack: () => {
return PrettyStack.print(
err,
opts.color ?? this.#color,
);
},
createdAt: new Date().toISOString(),
} as AyamariErr;
if (opts.injectStack || this.#injectStack) {
if (opts.injectStack ?? this.#injectStack) {
Error.captureStackTrace(err, createErr);
}
return err;
Expand All @@ -164,4 +153,8 @@ export class Ayamari<CustomErrCode> {
propagateErrRes(msg: string, opts: AyamariOpts) {
return Result.failure(this.propagateErr(msg, opts));
}

static prettifyStack(err: AyamariErr, color = true) {
return PrettyStack.print(err, color);
}
}
8 changes: 4 additions & 4 deletions packages/ayamari/src/pretty_stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class PrettyStack {
/** Kindly borrowed from https://github.com/errwischt/stacktrace-parser/blob/master/src/stack-trace-parser.js */
static #lineRe =
/^\s*at (?:((?:\[object object\])?[^\\/]+(?: \[as \S+\])?) )?\(?(.*?):(\d+)(?::(\d+))?\)?\s*$/i;
static #errMsgRe = /^(.*)\:\s(.*)/;
static #errMsgRe = /^([^:]*):\s(.*)/;
static #filenameRe = /^.*[\\\/]/;
static #color = {
reset: '\x1b[0m',
Expand Down Expand Up @@ -37,17 +37,17 @@ export class PrettyStack {
}
let prettyStacks = '';
const lines: string[] = [];
let stacksLineIndex = 0;
let stackIndex = 0;
for (const stack of stacks) {
let stackLineIndex = 0;
let prettyStack = '';
stackIndex++;
for (const line of stack.split('\n')) {
stackLineIndex++;
stacksLineIndex++;
if (stackLineIndex === 1) {
const [, errName, errMsg] =
PrettyStack.#errMsgRe.exec(line) || [];
const causeBy = stacksLineIndex
const causeBy = stacks.length === stackIndex
? ''
: `${red}└──${reset} `;
prettyStack += ` ${causeBy}${bgRed}${errName}${reset}${gray}:${reset} ${errMsg}\n`;
Expand Down

0 comments on commit 9107e51

Please sign in to comment.