Skip to content

Commit

Permalink
fix: make render function standalone
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
* the render method in the Xception instance has been removed
* use renderError instead

EXAMPLE MIGRATION

before:
```ts
const error = new Xception(...)

const rendered = error.render();
```

after:
```
import { renderError } from 'xception/render';

const error = new Xception(...)

const rendered = renderError(error.render);
```

NOTE:
separating the render function from the default
export enables the use of other features
in non-Node environments
  • Loading branch information
alvis committed Nov 30, 2024
1 parent e4bc4c6 commit 188f13b
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 120 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"./render": {
"types": "./lib/render.d.ts",
"default": "./lib/render.js"
},
"./package.json": "./package.json"
},
"scripts": {
Expand Down
12 changes: 0 additions & 12 deletions source/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
*/

import { jsonify } from '#jsonify';
import { renderError } from '#render';
import { $cause, $meta, $namespace, $tags } from '#symbols';

import type { JsonObject } from 'type-fest';

import type { RenderOptions } from '#render';

export interface XceptionOptions {
/** upstream error */
cause?: unknown;
Expand Down Expand Up @@ -98,15 +95,6 @@ export class Xception extends Error {
return this[$tags];
}

/**
* render the error to a string
* @param options optional parameters
* @returns a rendered string to print
*/
public render(options?: RenderOptions): string {
return renderError(this, options);
}

/**
* convert the error to a jsonifiable object
* @returns a jsonifiable object
Expand Down
16 changes: 0 additions & 16 deletions source/import.ts

This file was deleted.

1 change: 0 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@
export * from './base';
export { Xception as default } from './base';

export * from './render';
export { default as xception } from './xception';
5 changes: 2 additions & 3 deletions source/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
* -------------------------------------------------------------------------
*/

import { existsSync, readFileSync } from 'node:fs';

import chalk from 'chalk';
import highlight from 'highlight-es';
import yamlify from 'yamlify-object';

import { importFs } from '#import';
import { jsonify } from '#jsonify';
import { disassembleStack } from '#stack';

Expand All @@ -41,8 +42,6 @@ export interface RenderOptions {
filter?: (path: string) => boolean;
}

const { existsSync, readFileSync } = await importFs();

const PADDING = ' ';

/** default number of lines from the targeted source line to be displayed */
Expand Down
11 changes: 0 additions & 11 deletions spec/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import { describe, expect, it } from 'vitest';
import { Xception } from '#base';
import { $cause, $meta, $namespace, $tags } from '#symbols';

import { ansi } from './ansi';

class NewError extends Xception {
constructor(options?: { cause?: unknown }) {
super('new error', { ...options, tags: ['new'] });
Expand Down Expand Up @@ -112,15 +110,6 @@ describe('cl:Xception', () => {
});
});

describe('render', () => {
it('should render the error', () => {
const rendered = extendedError.render().replace(ansi, '');

expect(rendered).toContain('[Xception] extended');
expect(rendered).toContain('at');
});
});

describe('toJSON', () => {
it('should return a jsonifiable object', () => {
expect(new Xception('message').toJSON()).toEqual({
Expand Down
45 changes: 0 additions & 45 deletions spec/import.spec.ts

This file was deleted.

58 changes: 26 additions & 32 deletions spec/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,34 @@

import { describe, expect, it, vi } from 'vitest';

import { Xception } from '#base';
import { renderError } from '#render';
import { ansi } from './ansi';

vi.mock('#import', () => ({
importFs: async () => ({
existsSync(path: string) {
switch (path) {
case 'src1':
case 'src2':
return true;
default:
return false;
}
},
readFileSync(path: string) {
console.log('reading: ', path);

switch (path) {
case 'src1':
case 'src2':
return new Array(20)
.fill(undefined)
.map((_, index) => `line ${index + 1}`)
.join('\n');
default:
throw new Error(`unrecognized path: ${path}`);
}
},
}),
vi.mock('node:fs', () => ({
existsSync(path: string) {
switch (path) {
case 'src1':
case 'src2':
return true;
default:
return false;
}
},
readFileSync(path: string) {
console.log('reading: ', path);

switch (path) {
case 'src1':
case 'src2':
return new Array(20)
.fill(undefined)
.map((_, index) => `line ${index + 1}`)
.join('\n');
default:
throw new Error(`unrecognized path: ${path}`);
}
},
}));

class MockedError extends Error {
Expand All @@ -55,12 +55,6 @@ class MockedError extends Error {
}
}

import { Xception } from '#base';
import { renderError } from '#render';

// const { Xception } = await import('#base');
// const { renderError } = await import('#render');

describe('fn:renderError', () => {
it('should render an error stack with its own format', () => {
const rendered = renderError(
Expand Down

0 comments on commit 188f13b

Please sign in to comment.