From 3bc2eb725c0e1aa43302c00f96e55cb450d15d5e Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sat, 23 Nov 2024 08:30:36 +0530 Subject: [PATCH] feat: add toJSON method --- src/types.ts | 12 ++++++++++++ src/youch.ts | 10 +++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index af8a2fe..63e4db0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 +} diff --git a/src/youch.ts b/src/youch.ts index 128efa2..d33e71d 100644 --- a/src/youch.ts +++ b/src/youch.ts @@ -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 @@ -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 */