Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add codeContext property to types #43

Merged
merged 2 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
declare module "youch" {

interface YouchOptionsContract {
/**
* Number of lines to be displayed above the error
* in the stack trace.
*/
preLines?: number;

/**
* Number of lines to be displayed below the error
* in the stack trace.
*/
postLines?: number;
}

class Youch<Error, Request> {
constructor(error: Error, request: Request);
constructor(error: Error, request: Request, options?: YouchOptionsContract);

/**
* Stores the link `callback` which
Expand All @@ -18,21 +33,21 @@ declare module "youch" {
name: string;
status: number;
frames: {
file: string,
filePath: string,
line: number,
column: number,
callee: string,
calleeShort: string,
file: string;
filePath: string;
line: number;
column: number;
callee: string;
calleeShort: string;
context: {
start: number,
pre: string,
line: string,
post: string,
},
isModule: boolean,
isNative: boolean,
isApp: boolean
start: number;
pre: string;
line: string;
post: string;
};
isModule: boolean;
isNative: boolean;
isApp: boolean;
}[];
};
}>;
Expand Down
11 changes: 7 additions & 4 deletions src/Youch.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ const VIEW_PATH = './error.compiled.mustache'
const viewTemplate = fs.readFileSync(path.join(__dirname, VIEW_PATH), 'utf-8')

class Youch {
constructor (error, request) {
this.codeContext = 5
constructor (error, request, options = {}) {
this.options = options
this.options.postLines = options.postLines || 5
this.options.preLines = options.preLines || 5

this._filterHeaders = ['cookie', 'connection']
this.error = error
this.request = request
Expand Down Expand Up @@ -64,11 +67,11 @@ class Youch {

resolve({
pre: lines.slice(
Math.max(0, lineNumber - (this.codeContext + 1)),
Math.max(0, lineNumber - (this.options.preLines + 1)),
lineNumber - 1
),
line: lines[lineNumber - 1],
post: lines.slice(lineNumber, lineNumber + this.codeContext)
post: lines.slice(lineNumber, lineNumber + this.options.postLines)
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/error.compiled.mustache

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions test/youch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,43 @@ test.group('Youch', () => {
done()
})
})

test('preLines/postLines should be affect number of frame context lines', (assert, done) => {
const error = new Error('this is bar')

const youch = new Youch(error, {}, {
preLines: 2,
postLines: 4
})

youch
._parseError()
.then((stack) => {
const frame = stack[0]
const context = youch._getContext(frame)

assert.equal(context.pre.split('\n').length, 2)
assert.equal(context.post.split('\n').length, 4)
done()
})
.catch(done)
})

test('default preLines/postLines should be 5', (assert, done) => {
const error = new Error('this is bar')

const youch = new Youch(error, {})

youch
._parseError()
.then((stack) => {
const frame = stack[0]
const context = youch._getContext(frame)

assert.equal(context.pre.split('\n').length, 5)
assert.equal(context.post.split('\n').length, 5)
done()
})
.catch(done)
})
})
39 changes: 39 additions & 0 deletions test/youch.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,43 @@ test.group('Youch | ESM', () => {
done()
})
})

test('preLines/postLines should be affect number of frame context lines', (assert, done) => {
const error = new Error('this is bar')

const youch = new Youch(error, {}, {
preLines: 2,
postLines: 4
})

youch
._parseError()
.then((stack) => {
const frame = stack[0]
const context = youch._getContext(frame)

assert.equal(context.pre.split('\n').length, 2)
assert.equal(context.post.split('\n').length, 4)
done()
})
.catch(done)
})

test('default preLines/postLines should be 5', (assert, done) => {
const error = new Error('this is bar')

const youch = new Youch(error, {})

youch
._parseError()
.then((stack) => {
const frame = stack[0]
const context = youch._getContext(frame)

assert.equal(context.pre.split('\n').length, 5)
assert.equal(context.post.split('\n').length, 5)
done()
})
.catch(done)
})
})