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

feat: improve errors #61

Merged
merged 10 commits into from
Apr 14, 2020
Merged

feat: improve errors #61

merged 10 commits into from
Apr 14, 2020

Conversation

fmvilas
Copy link
Member

@fmvilas fmvilas commented Apr 10, 2020

Description

Improves the information provided by errors. It has several improvements:

  1. Uses the Problem Details spec: https://tools.ietf.org/html/rfc7807. It makes it easier to differentiate error types.
  2. Uses JSON Pointers to refer indicate where's the wrong field.
  3. Adds the information about the line, the column, and the offset where the error happens. It's aware of the document format (YAML/JSON) and gives you this information correctly processed for the document format.
  4. It makes it easier to differentiate between JSON and YAML documents.
  5. Adds more tests.

Examples

Validation error
{
  type: 'https://github.com/asyncapi/parser-js/validation-errors',
  title: 'There were errors validating the AsyncAPI document.',
  validationErrors: [{
    jsonPointer: '/components/schemas/testSchema/properties/test/$ref',
    startLine: 30,
    startColumn: 11,
    startOffset: 615,
    endLine: 30,
    endColumn: 34,
    endOffset: 638,
  }]
}
The document is not valid JSON nor YAML (YAML in this case)
{
  type: 'https://github.com/asyncapi/parser-js/invalid-yaml,
  title: 'The provided YAML is not valid.',
  detail: 'duplicated mapping key at line 2, column -4:\n    bad:\n    ^',
  location: {
    jsonPointer: '/components/schemas/testSchema/properties/test/$ref',
    startLine: 2,
    startColumn: -4,
    startOffset: 5
  }
}
A $ref couldn't be resolved
{
  type: 'https://github.com/asyncapi/parser-js/dereference-error',
  title: 'Error opening file "/Users/fmvilas/refs/refed.yaml" \nENOENT: no such file or directory, open '/Users/fmvilas/refs/refed.yaml'}',
  refs: [{
    jsonPointer: '/components/schemas/testSchema/properties/test/$ref',
    startLine: 30,
    startColumn: 11,
    startOffset: 615,
    endLine: 30,
    endColumn: 34,
    endOffset: 638,
  }]
}

How to migrate

This PR introduces a breaking change. The error classes PaserErrorNoJS and ParserErrorUnsupportedVersion don't exist anymore. Instead, we only have a single ParserError class and you can guess the type of the error by looking at the type field of the it.

ParserErrorNoJS

Before:

ParserErrorNoJS('Could not convert AsyncAPI to JSON.')

Now:

ParserError({
  type: 'impossible-to-convert-to-json',
  title: 'Could not convert AsyncAPI to JSON.',
  detail: 'Most probably the AsyncAPI document contains invalid YAML or YAML features not supported in JSON.'
})

ParserErrorUnsupportedVersion

Before:

ParserErrorUnsupportedVersion('AsyncAPI version is missing or unsupported: x.x.x.')
ParserErrorUnsupportedVersion.parsedJSON = {...}

Now:

ParserError({
  type: 'missing-asyncapi-field',
  title: 'The `asyncapi` field is missing.',
  parsedJSON: {...},
}

and

ParserError({
  type: 'unsupported-version',
  title: `Version x.x.x is not supported.`,
  detail: 'Please update to the latest version.',
  parsedJSON: {...},
  validationErrors: [{...}],
})

ParserError

Before:

ParserError('many different error messages')
ParserError.parsedJSON = {...}
ParserError.errors = [{...}]

Now:

Different ParserError errors with different type fields to make it easier to identify the kind of message. For more information, see README.md.

@fmvilas fmvilas requested review from jonaslagoni and derberg April 10, 2020 20:39
@fmvilas fmvilas added this to the Make JS parser stable milestone Apr 10, 2020
lib/utils.js Show resolved Hide resolved
API.md Show resolved Hide resolved
Copy link
Member

@derberg derberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome stuff, just few remarks

PR description should clearly indicate that new version will no longer support types like ParserErrorNoJS and ParserErrorUnsupportedVersion and what use in exchange. This is a breaking change and it would be explained what is replacement so when people go to this PR from release notes, they do not have to read the code.

lib/parser.js Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
derberg
derberg previously approved these changes Apr 14, 2020
Copy link
Member

@derberg derberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it a lot ❤️
approved

@fmvilas fmvilas requested a review from derberg April 14, 2020 14:45
@fmvilas
Copy link
Member Author

fmvilas commented Apr 14, 2020

Thanks! 🚀

@fmvilas fmvilas merged commit 2af2417 into asyncapi:master Apr 14, 2020
@fmvilas fmvilas deleted the improve-errors branch April 14, 2020 14:50
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 0.17.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants