Skip to content

Commit

Permalink
feat: add toJSON method
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 23, 2024
1 parent 948a77a commit 3bc2eb7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,15 @@ export type YouchANSIOptions = {
*/
offset?: number
}

/**
* Set of options accepted by Youch when rendering error
* to JSON output
*/
export type YouchJSONOptions = {
/**
* Define the offset to skip certain stack frames from
* the top
*/
offset?: number
}
10 changes: 9 additions & 1 deletion src/youch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { Parser, SourceLoader, Transformer } from 'youch-core/types'

import { Metadata } from './metadata.js'
import { Templates } from './templates.js'
import { YouchANSIOptions, YouchHTMLOptions } from './types.js'
import { YouchANSIOptions, YouchHTMLOptions, YouchJSONOptions } from './types.js'

/**
* Youch exposes the API to render errors to HTML output
Expand Down Expand Up @@ -60,6 +60,14 @@ export class Youch {
return this
}

/**
* Parses error to JSON
*/
async toJSON(error: unknown, options?: YouchJSONOptions) {
options = { ...options }
return new ErrorParser({ offset: options.offset }).parse(error)
}

/**
* Render error to HTML
*/
Expand Down

0 comments on commit 3bc2eb7

Please sign in to comment.