Skip to content

Commit

Permalink
Improve error handling (#77)
Browse files Browse the repository at this point in the history
* Improve error handling by checking for invalid file formats

* Create changelog and bump version
  • Loading branch information
markbrockhoff authored Jul 21, 2023
1 parent f857f37 commit 1e59e6b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<!-- @formatter:off -->
<!-- Keep a Changelog guide -> https://keepachangelog.com -->

# Changelog

## [Unreleased]

### Changed
- Improve error handling in case of an invalid openapi spec

## [2.0.0-alpha.2] - 2023-07-21

### Changed
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pluginGroup=com.schwarzit.spectral-intellij-plugin
pluginName=Spectral
pluginRepositoryUrl=https://github.com/SchwarzIT/spectral-intellij-plugin
# SemVer format -> https://semver.org
pluginVersion=2.0.0-alpha.2
pluginVersion=2.0.0-alpha.3
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild=222
pluginUntilBuild=231.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.schwarzit.spectralIntellijPlugin
import com.intellij.json.psi.JsonFile
import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.lang.annotation.ExternalAnnotator
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.Editor
Expand Down Expand Up @@ -78,13 +79,31 @@ class SpectralExternalAnnotator : ExternalAnnotator<Editor, List<SpectralIssue>>
val documentManager = PsiDocumentManager.getInstance(file.project)
val document = documentManager.getDocument(file) ?: return

issues?.forEach { issue ->
if (issues == null) return

if (issues.any { issue -> issue.code == "unrecognized-format" }) {
logger.warn("Linted openapi spec is not valid: Skipping linting")
holder.newAnnotation(HighlightSeverity.WARNING, "File is not formatted correctly. Linting was skipped.")
.fileLevel()
.create()
return
}

for (issue in issues) {
// It happens that spectral produces invalid text ranges, those will just be ignored
var textRange: TextRange
try {
textRange = calculateIssueTextRange(document, issue.range)
} catch (e: Throwable) {
continue
}

holder
.newAnnotation(
mapSeverity(issue.severity),
issue.code + ": " + issue.message
)
.range(calculateIssueTextRange(document, issue.range))
.range(textRange)
.create()
}
}
Expand Down

0 comments on commit 1e59e6b

Please sign in to comment.