scoop:scoop.compression:0.1.0
src/errors.scoop
Selected production source · src/errors.scoop
1
package scoop.compression3
import scoop.platform.system.*5
/**6
* The codec-level reason for a failed compression or decompression operation.7
*/8
public enum CompressionFailureKind {9
/**10
* The compressed bytes or framing are invalid.11
*/12
InvalidData,13
/**14
* Input ended before the required frame or stream boundary.15
*/16
Truncated,17
/**18
* The stream requires a dictionary that was not supplied.19
*/20
DictionaryRequired,21
/**22
* The supplied dictionary does not match the stream.23
*/24
DictionaryMismatch,25
/**26
* Decoded output would exceed the configured byte limit.27
*/28
OutputLimit,29
/**30
* The stream requires a window larger than the configured limit.31
*/32
WindowLimit,33
/**34
* A native codec operation failed.35
*/36
Backend37
}39
/**40
* A codec failure retaining format, operation, reason, backend code, and diagnostic detail.41
*/42
public class CompressionException public constructor(43
/**44
* The operation that failed.45
*/46
public val operation: String,47
/**48
* The selected wire format.49
*/50
public val format: CompressionFormat,51
/**52
* The portable failure category.53
*/54
public val kind: CompressionFailureKind,55
/**56
* The native backend error code.57
*/58
public val backendCode: Int,59
/**60
* The diagnostic explanation from the codec.61
*/62
public val detail: String63
) : Exception(Some(operation + ": " + detail))65
internal fun compressionError(kind: Int, code: Int, detail: String, format: CompressionFormat): Throwable {66
if (kind == 8) return IllegalArgumentException(Some(detail))67
if (kind == 9) return PlatformException("allocate compression memory", PlatformErrorKind.NoMemory,68
if (code == 0) None else Some(NativeError(NativeErrorDomain.PosixErrno, code)))69
if (kind == 10) return ArithmeticException()70
return CompressionException("compression codec", format, when (kind) {71
case 1 -> CompressionFailureKind.InvalidData72
case 2 -> CompressionFailureKind.Truncated73
case 3 -> CompressionFailureKind.DictionaryRequired74
case 4 -> CompressionFailureKind.DictionaryMismatch75
case 5 -> CompressionFailureKind.OutputLimit76
case 6 -> CompressionFailureKind.WindowLimit77
else -> CompressionFailureKind.Backend78
}, code, detail)79
}81
internal fun compressionTrailing(format: CompressionFormat): Nothing =82
throw CompressionException("decompress", format, CompressionFailureKind.InvalidData, 0, "trailing compressed data")84
internal fun <T> compressionPresent(value: T?): Boolean = when (value) { case Some(_) -> true; case None -> false }