Skip to content

Commit 1e59e6b

Browse files
Improve error handling (#77)
* Improve error handling by checking for invalid file formats * Create changelog and bump version
1 parent f857f37 commit 1e59e6b

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
<!-- @formatter:off -->
12
<!-- Keep a Changelog guide -> https://keepachangelog.com -->
23

34
# Changelog
45

56
## [Unreleased]
67

8+
### Changed
9+
- Improve error handling in case of an invalid openapi spec
10+
711
## [2.0.0-alpha.2] - 2023-07-21
812

913
### Changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pluginGroup=com.schwarzit.spectral-intellij-plugin
33
pluginName=Spectral
44
pluginRepositoryUrl=https://github.com/SchwarzIT/spectral-intellij-plugin
55
# SemVer format -> https://semver.org
6-
pluginVersion=2.0.0-alpha.2
6+
pluginVersion=2.0.0-alpha.3
77
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
88
pluginSinceBuild=222
99
pluginUntilBuild=231.*

src/main/kotlin/com/schwarzit/spectralIntellijPlugin/SpectralExternalAnnotator.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.schwarzit.spectralIntellijPlugin
33
import com.intellij.json.psi.JsonFile
44
import com.intellij.lang.annotation.AnnotationHolder
55
import com.intellij.lang.annotation.ExternalAnnotator
6+
import com.intellij.lang.annotation.HighlightSeverity
67
import com.intellij.openapi.components.service
78
import com.intellij.openapi.editor.Document
89
import com.intellij.openapi.editor.Editor
@@ -78,13 +79,31 @@ class SpectralExternalAnnotator : ExternalAnnotator<Editor, List<SpectralIssue>>
7879
val documentManager = PsiDocumentManager.getInstance(file.project)
7980
val document = documentManager.getDocument(file) ?: return
8081

81-
issues?.forEach { issue ->
82+
if (issues == null) return
83+
84+
if (issues.any { issue -> issue.code == "unrecognized-format" }) {
85+
logger.warn("Linted openapi spec is not valid: Skipping linting")
86+
holder.newAnnotation(HighlightSeverity.WARNING, "File is not formatted correctly. Linting was skipped.")
87+
.fileLevel()
88+
.create()
89+
return
90+
}
91+
92+
for (issue in issues) {
93+
// It happens that spectral produces invalid text ranges, those will just be ignored
94+
var textRange: TextRange
95+
try {
96+
textRange = calculateIssueTextRange(document, issue.range)
97+
} catch (e: Throwable) {
98+
continue
99+
}
100+
82101
holder
83102
.newAnnotation(
84103
mapSeverity(issue.severity),
85104
issue.code + ": " + issue.message
86105
)
87-
.range(calculateIssueTextRange(document, issue.range))
106+
.range(textRange)
88107
.create()
89108
}
90109
}

0 commit comments

Comments
 (0)