scoop:scoop.charset:0.1.0
src/errors.scoop
Selected production source · src/errors.scoop
1
package scoop.charset3
/**4
* The text, option, or backend category of a failed conversion.5
*/6
public enum CharsetFailureKind {7
/**8
* The source contains invalid byte sequences for its encoding.9
*/10
MalformedInput,11
/**12
* Final input ends partway through an encoded sequence.13
*/14
IncompleteInput,15
/**16
* The destination encoding cannot represent a source character.17
*/18
UnmappableCharacter,19
/**20
* The selected decoding policy requires an initial BOM that is absent.21
*/22
MissingBom,23
/**24
* An initial BOM conflicts with the selected encoding or byte order.25
*/26
ConflictingBom,27
/**28
* The selected encoding and conversion options are incompatible.29
*/30
InvalidOptions,31
/**32
* The native converter could not complete the requested operation.33
*/34
Backend35
}37
/**38
* A requested name or alias is not supported by the conversion backend.39
*/40
public class UnsupportedCharsetException public constructor(41
/**42
* The unsupported name supplied by the caller.43
*/44
public val name: String) :45
Exception(Some("unsupported charset: " + name))47
/**48
* A conversion failure carrying the operation, source and destination encodings, failure kind, and49
* available byte-offset/backend details.50
*/51
public class CharsetException public constructor(52
/**53
* The conversion operation that failed.54
*/55
public val operation: String,56
/**57
* The source encoding name used for the operation.58
*/59
public val source: String,60
/**61
* The destination encoding name used for the operation.62
*/63
public val destination: String,64
/**65
* The structured category of the conversion failure.66
*/67
public val kind: CharsetFailureKind,68
/**69
* The input byte offset associated with the failure, when available.70
*/71
public val offset: Long?,72
/**73
* The native backend error code, when available.74
*/75
public val backendCode: Int?,76
/**77
* A human-readable explanation of the conversion failure.78
*/79
public val detail: String80
) : Exception(Some(operation + ": " + detail))82
internal fun checkCharset(code: Int, backend: Int, operation: String, source: String, destination: String, offset: Long? = None) {83
if (code == 0) return84
val kind = when (code) {85
case 3 -> CharsetFailureKind.MalformedInput86
case 4 -> CharsetFailureKind.UnmappableCharacter87
case 5 -> CharsetFailureKind.IncompleteInput88
case 6 -> CharsetFailureKind.MissingBom89
case 7 -> CharsetFailureKind.ConflictingBom90
case 8 -> CharsetFailureKind.InvalidOptions91
else -> CharsetFailureKind.Backend92
}93
val detail = when (kind) {94
case MalformedInput -> "invalid byte sequence"95
case IncompleteInput -> "incomplete byte sequence at end of input"96
case UnmappableCharacter -> "character cannot be represented in the destination charset"97
case MissingBom -> "a byte order mark or an explicit default byte order is required"98
case ConflictingBom -> "byte order mark conflicts with the selected byte order"99
case InvalidOptions -> "BOM or byte order option does not apply to the selected charset"100
case Backend -> "Text backend could not complete the conversion"101
}102
throw CharsetException(operation, source, destination, kind, offset,103
if (backend == 0) None else Some(backend), detail)104
}