Skip to content

Commit

Permalink
Include path information in exceptions, and improve location informat…
Browse files Browse the repository at this point in the history
…ion shown when an invalid type is given for a polymorphic value.

This resolves #37.
  • Loading branch information
charleskorn committed Oct 3, 2020
1 parent c1d9633 commit 79aae57
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 180 deletions.
66 changes: 32 additions & 34 deletions src/main/kotlin/com/charleskorn/kaml/YamlException.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,83 +18,81 @@

package com.charleskorn.kaml

import org.snakeyaml.engine.v2.events.Event

public open class YamlException(
override val message: String,
public val line: Int,
public val column: Int,
public val path: YamlPath,
override val cause: Throwable? = null
) : RuntimeException(message, cause) {
public constructor(message: String, location: Location, cause: Throwable? = null) : this(message, location.line, location.column, cause)

public val location: Location = Location(line, column)
public val location: Location = path.endLocation
public val line: Int = location.line
public val column: Int = location.column

override fun toString(): String = "${this::class.qualifiedName} at line $line, column $column: $message"
override fun toString(): String = "${this::class.qualifiedName} at ${path.toHumanReadableString()} on line $line, column $column: $message"
}

public class DuplicateKeyException(
public val originalLocation: Location,
public val duplicateLocation: Location,
public val originalPath: YamlPath,
public val duplicatePath: YamlPath,
public val key: String
) :
YamlException("Duplicate key $key. It was previously given at line ${originalLocation.line}, column ${originalLocation.column}.", duplicateLocation)
YamlException("Duplicate key $key. It was previously given at line ${originalPath.endLocation.line}, column ${originalPath.endLocation.column}.", duplicatePath) {

public class EmptyYamlDocumentException(message: String, location: Location) : YamlException(message, location)
public val originalLocation: Location = originalPath.endLocation
public val duplicateLocation: Location = duplicatePath.endLocation
}

public class EmptyYamlDocumentException(message: String, path: YamlPath) : YamlException(message, path)

public class InvalidPropertyValueException(
public val propertyName: String,
public val reason: String,
location: Location,
path: YamlPath,
cause: Throwable? = null
) : YamlException("Value for '$propertyName' is invalid: $reason", location, cause)
) : YamlException("Value for '$propertyName' is invalid: $reason", path, cause)

public class MalformedYamlException(message: String, location: Location) : YamlException(message, location)
public class MalformedYamlException(message: String, path: YamlPath) : YamlException(message, path)

public class UnexpectedNullValueException(location: Location) : YamlException("Unexpected null or empty value for non-null field.", location)
public class UnexpectedNullValueException(path: YamlPath) : YamlException("Unexpected null or empty value for non-null field.", path)

public class MissingRequiredPropertyException(
public val propertyName: String,
location: Location,
path: YamlPath,
cause: Throwable? = null
) :
YamlException("Property '$propertyName' is required but it is missing.", location, cause)
YamlException("Property '$propertyName' is required but it is missing.", path, cause)

public class UnknownPropertyException(
public val propertyName: String,
public val validPropertyNames: Set<String>,
location: Location
path: YamlPath
) :
YamlException("Unknown property '$propertyName'. Known properties are: ${validPropertyNames.sorted().joinToString(", ")}", location)
YamlException("Unknown property '$propertyName'. Known properties are: ${validPropertyNames.sorted().joinToString(", ")}", path)

public class UnknownPolymorphicTypeException(
public val typeName: String,
public val validTypeNames: Set<String>,
location: Location,
path: YamlPath,
cause: Throwable? = null
) :
YamlException("Unknown type '$typeName'. Known types are: ${validTypeNames.sorted().joinToString(", ")}", location, cause)
YamlException("Unknown type '$typeName'. Known types are: ${validTypeNames.sorted().joinToString(", ")}", path, cause)

public class YamlScalarFormatException(
message: String,
location: Location,
path: YamlPath,
public val originalValue: String
) : YamlException(message, location)
) : YamlException(message, path)

public open class IncorrectTypeException(message: String, location: Location) : YamlException(message, location)
public open class IncorrectTypeException(message: String, path: YamlPath) : YamlException(message, path)

public class MissingTypeTagException(location: Location) :
IncorrectTypeException("Value is missing a type tag (eg. !<type>)", location)
public class MissingTypeTagException(path: YamlPath) :
IncorrectTypeException("Value is missing a type tag (eg. !<type>)", path)

public class UnknownAnchorException(public val anchorName: String, location: Location) :
YamlException("Unknown anchor '$anchorName'.", location)
public class UnknownAnchorException(public val anchorName: String, path: YamlPath) :
YamlException("Unknown anchor '$anchorName'.", path)

public class NoAnchorForExtensionException(
public val key: String,
public val extensionDefinitionPrefix: String,
location: Location
path: YamlPath
) :
YamlException("The key '$key' starts with the extension definition prefix '$extensionDefinitionPrefix' but does not define an anchor.", location)

internal val Event.location: Location
get() = Location(startMark.get().line + 1, startMark.get().column + 1)
YamlException("The key '$key' starts with the extension definition prefix '$extensionDefinitionPrefix' but does not define an anchor.", path)
Loading

0 comments on commit 79aae57

Please sign in to comment.