Thanks to visit codestin.com
Credit goes to github.com

Skip to content

The core parser used by Youch and the Youch terminal package for pretty printing errors

License

Notifications You must be signed in to change notification settings

poppinss/youch-core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

youch-core

Error parser used by Youch to parse a JavaScript error into a collection of frames


gh-workflow-image npm-image license-image

Introduction

The youch-core package contains the Error parser used by youch to pretty-print errors on the Web and in the terminal.

It is a low-level package, and you will only use it if you want to create your own Error printer while reusing the core Error parsing logic.

Installation

Install the package from the npm registry as follows.

npm i youch-core
yarn add youch-core
pnpm add youch-core

Usage

You may parse an error using the ErrorParser.parse method. The parse method accepts the error object and returns a Promise with ParsedError.

import { ErrorParser } from 'youch-core'

/**
 * Error object to parse
 */
const error = new Error('Something went wrong')

/**
 * Create a parser instance and parse the error
 */
const parser = new ErrorParser()
const parsedError = await parser.parse(error)

The parsedError.frames property is an array of stack frames with the filename, line number, and the source code snippet for the given frame in the stack.

parsedError.frames.forEach((frame) => {
  console.log(`${frame.type}: ${frame.fileName}:${frame.lineNumber}`)
})

Using a custom source code loader

The ErrorParser reads the source code of files within the stack trace using the Node.js fs module. However, you can override this default and provide a custom source loader using the parser.defineSourceLoader method.

Note

The defineSourceLoader method is called for every frame within the stack traces. Therefore, you must perform the necessary checks before attempting to read the source code of a file. For example, you must not attempt to read the source code for fileNames pointing to native code.

const parser = new ErrorParser()

parser.defineSourceLoader(async (stackFrame) => {
  if (stackFrame.type !== 'native') {
    stackFrame.source = await someFunctionToGetFileSource(stackFrame.fileName)
  }
})

Contributing

One of the primary goals of Poppinss is to have a vibrant community of users and contributors who believe in the principles of the framework.

We encourage you to read the contribution guide before contributing to the framework.

Code of Conduct

To ensure that the Poppinss community is welcoming to all, please review and abide by the Code of Conduct.

License

The youch-core package is open-sourced software licensed under the MIT license.

About

The core parser used by Youch and the Youch terminal package for pretty printing errors

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 2

  •  
  •