Scoop API

scoop:scoop.compression:0.1.0

src/errors.scoop

Selected production source · src/errors.scoop

1package scoop.compression
3import scoop.platform.system.*
5/**
6 * The codec-level reason for a failed compression or decompression operation.
7 */
8public 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 Backend
39/**
40 * A codec failure retaining format, operation, reason, backend code, and diagnostic detail.
41 */
42public 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: String
63) : Exception(Some(operation + ": " + detail))
65internal 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.InvalidData
72 case 2 -> CompressionFailureKind.Truncated
73 case 3 -> CompressionFailureKind.DictionaryRequired
74 case 4 -> CompressionFailureKind.DictionaryMismatch
75 case 5 -> CompressionFailureKind.OutputLimit
76 case 6 -> CompressionFailureKind.WindowLimit
77 else -> CompressionFailureKind.Backend
78 }, code, detail)
81internal fun compressionTrailing(format: CompressionFormat): Nothing =
82 throw CompressionException("decompress", format, CompressionFailureKind.InvalidData, 0, "trailing compressed data")
84internal fun <T> compressionPresent(value: T?): Boolean = when (value) { case Some(_) -> true; case None -> false }