Scoop API

scoop:scoop.url:0.1.0

src/errors.scoop

Selected production source · src/errors.scoop

1package scoop.url
3/**
4 * The component responsible for a URL parsing or construction failure.
5 */
6public enum UrlComponent {
7 /**
8 * The failure concerns the complete reference or its overall structure.
9 */
10 Reference,
11 /**
12 * The failure concerns the scheme prefix.
13 */
14 Scheme,
15 /**
16 * The failure concerns the complete authority.
17 */
18 Authority,
19 /**
20 * The failure concerns the authority's user information.
21 */
22 UserInfo,
23 /**
24 * The failure concerns the DNS or IP host.
25 */
26 Host,
27 /**
28 * The failure concerns the explicit network port.
29 */
30 Port,
31 /**
32 * The failure concerns the path component.
33 */
34 Path,
35 /**
36 * The failure concerns the query component.
37 */
38 Query,
39 /**
40 * The failure concerns the fragment component.
41 */
42 Fragment,
43 /**
44 * The failure concerns a malformed percent escape or decoded component.
45 */
46 PercentEncoding }
48/**
49 * A format failure with its component, optional original UTF-8 byte offset, detail, and optional
50 * IDNA cause.
51 */
52public class UrlParseException public constructor(
53 /**
54 * The component whose syntax or value is invalid.
55 */
56 public val component: UrlComponent,
57 /**
58 * The zero-based original UTF-8 byte position when available; None when the failure has no
59 * original input position.
60 */
61 public val offset: Long?,
62 /**
63 * A human-readable explanation of the invalid component.
64 */
65 public val detail: String,
66 /**
67 * An underlying validation failure, such as an IDNA error, when available.
68 */
69 public val cause: Throwable? = None
70) : Exception(Some(detail))
72/**
73 * An allocation or parser-backend failure, separate from an ordinary invalid URL.
74 */
75public class UrlBackendException internal constructor(
76 /**
77 * The backend error code associated with the failed operation.
78 */
79 public val code: Int, detail: String) : Exception(Some(detail))
81internal fun urlError(component: UrlComponent, detail: String, offset: Long? = None): Nothing =
82 throw UrlParseException(component, offset, detail)
84internal fun checkUriCode(code: Int, component: UrlComponent, offset: Long? = None) {
85 if (code == 0) return
86 if (code == 1 || code == 5 || code == 9 || (code >= 12 && code <= 15)) {
87 urlError(component, if (code == 5) "relative resolution requires an absolute base" else "invalid URI component", offset)
88 }
89 throw UrlBackendException(code, "uriparser failed with code " + code.toString())