Skip to content

Commit

Permalink
feat: add tag support
Browse files Browse the repository at this point in the history
  • Loading branch information
alvis committed Dec 19, 2020
1 parent 5857138 commit f93e942
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
10 changes: 9 additions & 1 deletion source/prototype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,27 @@ export class Xception extends Error {
/** running context */
public meta: Record<string, unknown>;

/** additional associations */
public tags: string[];

/**
* @param message error message
* @param options additional options for the error
* @param options.cause upstream error
* @param options.namespace namespace of the error
* @param options.meta context where the error occur
* @param options.tags additional associations for the error
*/
constructor(
message: string,
options?: {
cause?: unknown;
namespace?: string;
meta?: Record<string, unknown>;
tags?: string[];
},
) {
const { cause, namespace, meta = {} } = { ...options };
const { cause, namespace, meta = {}, tags = [] } = { ...options };

super(message);
this.cause = cause;
Expand All @@ -66,5 +71,8 @@ export class Xception extends Error {
cause.stack,
].join('\n')
: this.stack;

// attach tags to the error
this.tags = cause instanceof Xception ? [...cause.tags, ...tags] : tags;
}
}
4 changes: 4 additions & 0 deletions source/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ function renderAssociations(error: unknown): string | null {
blocks.push(chalk.blue.underline(error.namespace));
}

if (error instanceof Xception && error.tags.length) {
blocks.push(...error.tags.map((tag) => chalk.cyan.bold(tag)));
}

return blocks.length ? '\n ' + blocks.join(' ') : null;
}

Expand Down
10 changes: 8 additions & 2 deletions spec/prototype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Xception } from '#prototype';

class NewError extends Xception {
constructor(options?: { cause?: unknown }) {
super('new error', { ...options });
super('new error', { ...options, tags: ['new'] });
}
}

Expand All @@ -29,6 +29,7 @@ function getExtendedError(): Xception {
cause,
namespace: 'test:xception',
meta: { name: 'xception' },
tags: ['extended'],
});
}
}
Expand Down Expand Up @@ -62,6 +63,10 @@ describe('cl:Xception', () => {
expect(extendedError.meta).toEqual({ name: 'xception' });
});

it('passes tags to the inherited class', () => {
expect(newError.tags).toEqual(['extended', 'new']);
});

it('keeps its own stack if the attached error has no stack', () => {
const error = new Xception('message', {
cause: { name: 'GenericError', message: 'error' },
Expand All @@ -71,9 +76,10 @@ describe('cl:Xception', () => {
});

it('takes a normal error if no origin is attached', () => {
const error = new Xception('message');
const error = new Xception('message', { tags: ['tag'] });

expect(error.stack).toContain('Xception');
expect(error.tags).toEqual(['tag']);
});

it('bears the right error type', () => {
Expand Down
3 changes: 2 additions & 1 deletion spec/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ describe('fn:renderStack', () => {
const rendered = renderStack(
new Xception('message', {
namespace: 'xception',
tags: ['tag1', 'tag2'],
meta: { name: 'xception' },
}),
);
Expand All @@ -166,7 +167,7 @@ describe('fn:renderStack', () => {
expect(plain).toContain(
'[Xception] message\n' +
'\n' +
' xception\n' +
' xception tag1 tag2\n' +
'\n' +
' name: xception\n' +
'\n' +
Expand Down

0 comments on commit f93e942

Please sign in to comment.