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

Expose YAMLException from js-yaml #169

Open
andreww2012 opened this issue Jan 12, 2024 · 0 comments
Open

Expose YAMLException from js-yaml #169

andreww2012 opened this issue Jan 12, 2024 · 0 comments

Comments

@andreww2012
Copy link

andreww2012 commented Jan 12, 2024

I find YAMLExceptions too big when the parsed front matter is big because they include the whole source text. I decided to reformat an error a bit, for that I had to catch the parsing error using the obvious approach:

import parseMdWithFrontMatter from 'gray-matter';
import {YAMLException} from 'js-yaml';

try {
  const parsedContents = parseMdWithFrontMatter(contents);
  // ...
} catch (error) {
  if (error instanceof YAMLException) {
    // Format the error ...
  }
  throw error;
}

However, I was surprised when the error wasn't caught. It turned out I have my own js-yaml of a different major version (4.1.0), so technically imported YAMLException was a different class.

I think for cases like this, importing (at least) YAMLException from underlying gray-matter would be beneficial. Although, feel free to correct me if I'm doing something wrong. And thank you for the lib! :)

P.S. My workaround:
import grayMatter from 'gray-matter';
import {YAMLException} from 'js-yaml';

export const parseMdWithFrontMatter = (...args: Parameters<typeof grayMatter>) => {
  try {
    return grayMatter(...args);
  } catch (error) {
    if (error && typeof error === 'object' && error.constructor?.name === 'YAMLException') {
      Object.setPrototypeOf(error, YAMLException.prototype);
    }
    throw error;
  }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant