-
Notifications
You must be signed in to change notification settings - Fork 592
/
NetException.scala
122 lines (113 loc) · 5.02 KB
/
NetException.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) 2013 Functional Streams for Scala
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package fs2
package io
package net
import com.comcast.ip4s
import com.comcast.ip4s.Host
import com.comcast.ip4s.SocketAddress
import fs2.io.internal.facade
import scala.annotation.nowarn
import scala.scalajs.js
import scala.util.control.NoStackTrace
import scala.util.matching.Regex
class ProtocolException(message: String = null, cause: Throwable = null)
extends IOException(message, cause)
class SocketException(message: String = null, cause: Throwable = null)
extends IOException(message, cause)
private class JavaScriptSocketException(message: String, cause: js.JavaScriptException)
extends SocketException(message, cause)
with NoStackTrace
object SocketException {
private[io] def unapply(cause: js.JavaScriptException): Option[SocketException] =
cause.exception match {
case error: js.Error if error.message.contains("ECONNRESET") =>
Some(new JavaScriptSocketException("Connection reset by peer", cause))
case _ => BindException.unapply(cause).orElse(ConnectException.unapply(cause))
}
}
class BindException(message: String = null, cause: Throwable = null)
extends SocketException(message, cause)
private class JavaScriptBindException(cause: js.JavaScriptException)
extends BindException("Address already in use", cause)
with NoStackTrace
object BindException {
private[net] def unapply(cause: js.JavaScriptException): Option[BindException] =
cause.exception match {
case error: js.Error if error.message.contains("EADDRINUSE") =>
Some(new JavaScriptBindException(cause))
case _ => None
}
}
class ConnectException(message: String = null, cause: Throwable = null)
extends SocketException(message, cause)
private class JavaScriptConnectException(cause: js.JavaScriptException)
extends ConnectException("Connection refused", cause)
with NoStackTrace
object ConnectException {
private[net] def unapply(cause: js.JavaScriptException): Option[ConnectException] =
cause.exception match {
case error: js.Error if error.message.contains("ECONNREFUSED") =>
Some(new JavaScriptConnectException(cause))
case _ => None
}
}
class SocketTimeoutException(message: String = null, cause: Throwable = null)
extends InterruptedIOException(message, cause)
private class JavaScriptSocketTimeoutException(cause: js.JavaScriptException)
extends SocketTimeoutException("Connection timed out", cause)
with NoStackTrace
object SocketTimeoutException {
private[io] def unapply(cause: js.JavaScriptException): Option[SocketTimeoutException] =
cause.exception match {
case error: js.Error if error.message.contains("ETIMEDOUT") =>
Some(new JavaScriptSocketTimeoutException(cause))
case _ => None
}
}
@deprecated("Use ip4s.UnknownHostException instead", "3.2.0")
class UnknownHostException(message: String = null, cause: Throwable = null)
extends ip4s.UnknownHostException(message, cause)
@nowarn("cat=deprecation")
private class JavaScriptUnknownHostException(host: String, cause: js.JavaScriptException)
extends UnknownHostException(s"$host: ${UnknownHostException.message}", cause)
with NoStackTrace
object UnknownHostException {
private[io] def unapply(cause: js.JavaScriptException): Option[ip4s.UnknownHostException] =
cause.exception match {
case error: js.Error =>
pattern.findFirstMatchIn(error.message).collect { case Regex.Groups(addr) =>
val host =
Option(addr)
.flatMap { addr =>
SocketAddress.fromString(addr).map(_.host).orElse(Host.fromString(addr))
}
.fold("<unknown>")(_.toString)
new JavaScriptUnknownHostException(host, cause)
}
case _ => None
}
private[this] val pattern = raw"(?:ENOTFOUND|EAI_AGAIN)(?: (\S+))?".r
private[net] val message = facade.os.`type`() match {
case "Darwin" => "nodename nor servname provided, or not known"
case _ => "Name or service not known"
}
}