errors.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. 'use strict'
  2. const kUndiciError = Symbol.for('undici.error.UND_ERR')
  3. class UndiciError extends Error {
  4. constructor (message, options) {
  5. super(message, options)
  6. this.name = 'UndiciError'
  7. this.code = 'UND_ERR'
  8. }
  9. static [Symbol.hasInstance] (instance) {
  10. return instance && instance[kUndiciError] === true
  11. }
  12. get [kUndiciError] () {
  13. return true
  14. }
  15. }
  16. const kConnectTimeoutError = Symbol.for('undici.error.UND_ERR_CONNECT_TIMEOUT')
  17. class ConnectTimeoutError extends UndiciError {
  18. constructor (message) {
  19. super(message)
  20. this.name = 'ConnectTimeoutError'
  21. this.message = message || 'Connect Timeout Error'
  22. this.code = 'UND_ERR_CONNECT_TIMEOUT'
  23. }
  24. static [Symbol.hasInstance] (instance) {
  25. return instance && instance[kConnectTimeoutError] === true
  26. }
  27. get [kConnectTimeoutError] () {
  28. return true
  29. }
  30. }
  31. const kHeadersTimeoutError = Symbol.for('undici.error.UND_ERR_HEADERS_TIMEOUT')
  32. class HeadersTimeoutError extends UndiciError {
  33. constructor (message) {
  34. super(message)
  35. this.name = 'HeadersTimeoutError'
  36. this.message = message || 'Headers Timeout Error'
  37. this.code = 'UND_ERR_HEADERS_TIMEOUT'
  38. }
  39. static [Symbol.hasInstance] (instance) {
  40. return instance && instance[kHeadersTimeoutError] === true
  41. }
  42. get [kHeadersTimeoutError] () {
  43. return true
  44. }
  45. }
  46. const kHeadersOverflowError = Symbol.for('undici.error.UND_ERR_HEADERS_OVERFLOW')
  47. class HeadersOverflowError extends UndiciError {
  48. constructor (message) {
  49. super(message)
  50. this.name = 'HeadersOverflowError'
  51. this.message = message || 'Headers Overflow Error'
  52. this.code = 'UND_ERR_HEADERS_OVERFLOW'
  53. }
  54. static [Symbol.hasInstance] (instance) {
  55. return instance && instance[kHeadersOverflowError] === true
  56. }
  57. get [kHeadersOverflowError] () {
  58. return true
  59. }
  60. }
  61. const kBodyTimeoutError = Symbol.for('undici.error.UND_ERR_BODY_TIMEOUT')
  62. class BodyTimeoutError extends UndiciError {
  63. constructor (message) {
  64. super(message)
  65. this.name = 'BodyTimeoutError'
  66. this.message = message || 'Body Timeout Error'
  67. this.code = 'UND_ERR_BODY_TIMEOUT'
  68. }
  69. static [Symbol.hasInstance] (instance) {
  70. return instance && instance[kBodyTimeoutError] === true
  71. }
  72. get [kBodyTimeoutError] () {
  73. return true
  74. }
  75. }
  76. const kInvalidArgumentError = Symbol.for('undici.error.UND_ERR_INVALID_ARG')
  77. class InvalidArgumentError extends UndiciError {
  78. constructor (message) {
  79. super(message)
  80. this.name = 'InvalidArgumentError'
  81. this.message = message || 'Invalid Argument Error'
  82. this.code = 'UND_ERR_INVALID_ARG'
  83. }
  84. static [Symbol.hasInstance] (instance) {
  85. return instance && instance[kInvalidArgumentError] === true
  86. }
  87. get [kInvalidArgumentError] () {
  88. return true
  89. }
  90. }
  91. const kInvalidReturnValueError = Symbol.for('undici.error.UND_ERR_INVALID_RETURN_VALUE')
  92. class InvalidReturnValueError extends UndiciError {
  93. constructor (message) {
  94. super(message)
  95. this.name = 'InvalidReturnValueError'
  96. this.message = message || 'Invalid Return Value Error'
  97. this.code = 'UND_ERR_INVALID_RETURN_VALUE'
  98. }
  99. static [Symbol.hasInstance] (instance) {
  100. return instance && instance[kInvalidReturnValueError] === true
  101. }
  102. get [kInvalidReturnValueError] () {
  103. return true
  104. }
  105. }
  106. const kAbortError = Symbol.for('undici.error.UND_ERR_ABORT')
  107. class AbortError extends UndiciError {
  108. constructor (message) {
  109. super(message)
  110. this.name = 'AbortError'
  111. this.message = message || 'The operation was aborted'
  112. this.code = 'UND_ERR_ABORT'
  113. }
  114. static [Symbol.hasInstance] (instance) {
  115. return instance && instance[kAbortError] === true
  116. }
  117. get [kAbortError] () {
  118. return true
  119. }
  120. }
  121. const kRequestAbortedError = Symbol.for('undici.error.UND_ERR_ABORTED')
  122. class RequestAbortedError extends AbortError {
  123. constructor (message) {
  124. super(message)
  125. this.name = 'AbortError'
  126. this.message = message || 'Request aborted'
  127. this.code = 'UND_ERR_ABORTED'
  128. }
  129. static [Symbol.hasInstance] (instance) {
  130. return instance && instance[kRequestAbortedError] === true
  131. }
  132. get [kRequestAbortedError] () {
  133. return true
  134. }
  135. }
  136. const kInformationalError = Symbol.for('undici.error.UND_ERR_INFO')
  137. class InformationalError extends UndiciError {
  138. constructor (message) {
  139. super(message)
  140. this.name = 'InformationalError'
  141. this.message = message || 'Request information'
  142. this.code = 'UND_ERR_INFO'
  143. }
  144. static [Symbol.hasInstance] (instance) {
  145. return instance && instance[kInformationalError] === true
  146. }
  147. get [kInformationalError] () {
  148. return true
  149. }
  150. }
  151. const kRequestContentLengthMismatchError = Symbol.for('undici.error.UND_ERR_REQ_CONTENT_LENGTH_MISMATCH')
  152. class RequestContentLengthMismatchError extends UndiciError {
  153. constructor (message) {
  154. super(message)
  155. this.name = 'RequestContentLengthMismatchError'
  156. this.message = message || 'Request body length does not match content-length header'
  157. this.code = 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
  158. }
  159. static [Symbol.hasInstance] (instance) {
  160. return instance && instance[kRequestContentLengthMismatchError] === true
  161. }
  162. get [kRequestContentLengthMismatchError] () {
  163. return true
  164. }
  165. }
  166. const kResponseContentLengthMismatchError = Symbol.for('undici.error.UND_ERR_RES_CONTENT_LENGTH_MISMATCH')
  167. class ResponseContentLengthMismatchError extends UndiciError {
  168. constructor (message) {
  169. super(message)
  170. this.name = 'ResponseContentLengthMismatchError'
  171. this.message = message || 'Response body length does not match content-length header'
  172. this.code = 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH'
  173. }
  174. static [Symbol.hasInstance] (instance) {
  175. return instance && instance[kResponseContentLengthMismatchError] === true
  176. }
  177. get [kResponseContentLengthMismatchError] () {
  178. return true
  179. }
  180. }
  181. const kClientDestroyedError = Symbol.for('undici.error.UND_ERR_DESTROYED')
  182. class ClientDestroyedError extends UndiciError {
  183. constructor (message) {
  184. super(message)
  185. this.name = 'ClientDestroyedError'
  186. this.message = message || 'The client is destroyed'
  187. this.code = 'UND_ERR_DESTROYED'
  188. }
  189. static [Symbol.hasInstance] (instance) {
  190. return instance && instance[kClientDestroyedError] === true
  191. }
  192. get [kClientDestroyedError] () {
  193. return true
  194. }
  195. }
  196. const kClientClosedError = Symbol.for('undici.error.UND_ERR_CLOSED')
  197. class ClientClosedError extends UndiciError {
  198. constructor (message) {
  199. super(message)
  200. this.name = 'ClientClosedError'
  201. this.message = message || 'The client is closed'
  202. this.code = 'UND_ERR_CLOSED'
  203. }
  204. static [Symbol.hasInstance] (instance) {
  205. return instance && instance[kClientClosedError] === true
  206. }
  207. get [kClientClosedError] () {
  208. return true
  209. }
  210. }
  211. const kSocketError = Symbol.for('undici.error.UND_ERR_SOCKET')
  212. class SocketError extends UndiciError {
  213. constructor (message, socket) {
  214. super(message)
  215. this.name = 'SocketError'
  216. this.message = message || 'Socket error'
  217. this.code = 'UND_ERR_SOCKET'
  218. this.socket = socket
  219. }
  220. static [Symbol.hasInstance] (instance) {
  221. return instance && instance[kSocketError] === true
  222. }
  223. get [kSocketError] () {
  224. return true
  225. }
  226. }
  227. const kNotSupportedError = Symbol.for('undici.error.UND_ERR_NOT_SUPPORTED')
  228. class NotSupportedError extends UndiciError {
  229. constructor (message) {
  230. super(message)
  231. this.name = 'NotSupportedError'
  232. this.message = message || 'Not supported error'
  233. this.code = 'UND_ERR_NOT_SUPPORTED'
  234. }
  235. static [Symbol.hasInstance] (instance) {
  236. return instance && instance[kNotSupportedError] === true
  237. }
  238. get [kNotSupportedError] () {
  239. return true
  240. }
  241. }
  242. const kBalancedPoolMissingUpstreamError = Symbol.for('undici.error.UND_ERR_BPL_MISSING_UPSTREAM')
  243. class BalancedPoolMissingUpstreamError extends UndiciError {
  244. constructor (message) {
  245. super(message)
  246. this.name = 'MissingUpstreamError'
  247. this.message = message || 'No upstream has been added to the BalancedPool'
  248. this.code = 'UND_ERR_BPL_MISSING_UPSTREAM'
  249. }
  250. static [Symbol.hasInstance] (instance) {
  251. return instance && instance[kBalancedPoolMissingUpstreamError] === true
  252. }
  253. get [kBalancedPoolMissingUpstreamError] () {
  254. return true
  255. }
  256. }
  257. const kHTTPParserError = Symbol.for('undici.error.UND_ERR_HTTP_PARSER')
  258. class HTTPParserError extends Error {
  259. constructor (message, code, data) {
  260. super(message)
  261. this.name = 'HTTPParserError'
  262. this.code = code ? `HPE_${code}` : undefined
  263. this.data = data ? data.toString() : undefined
  264. }
  265. static [Symbol.hasInstance] (instance) {
  266. return instance && instance[kHTTPParserError] === true
  267. }
  268. get [kHTTPParserError] () {
  269. return true
  270. }
  271. }
  272. const kResponseExceededMaxSizeError = Symbol.for('undici.error.UND_ERR_RES_EXCEEDED_MAX_SIZE')
  273. class ResponseExceededMaxSizeError extends UndiciError {
  274. constructor (message) {
  275. super(message)
  276. this.name = 'ResponseExceededMaxSizeError'
  277. this.message = message || 'Response content exceeded max size'
  278. this.code = 'UND_ERR_RES_EXCEEDED_MAX_SIZE'
  279. }
  280. static [Symbol.hasInstance] (instance) {
  281. return instance && instance[kResponseExceededMaxSizeError] === true
  282. }
  283. get [kResponseExceededMaxSizeError] () {
  284. return true
  285. }
  286. }
  287. const kRequestRetryError = Symbol.for('undici.error.UND_ERR_REQ_RETRY')
  288. class RequestRetryError extends UndiciError {
  289. constructor (message, code, { headers, data }) {
  290. super(message)
  291. this.name = 'RequestRetryError'
  292. this.message = message || 'Request retry error'
  293. this.code = 'UND_ERR_REQ_RETRY'
  294. this.statusCode = code
  295. this.data = data
  296. this.headers = headers
  297. }
  298. static [Symbol.hasInstance] (instance) {
  299. return instance && instance[kRequestRetryError] === true
  300. }
  301. get [kRequestRetryError] () {
  302. return true
  303. }
  304. }
  305. const kResponseError = Symbol.for('undici.error.UND_ERR_RESPONSE')
  306. class ResponseError extends UndiciError {
  307. constructor (message, code, { headers, body }) {
  308. super(message)
  309. this.name = 'ResponseError'
  310. this.message = message || 'Response error'
  311. this.code = 'UND_ERR_RESPONSE'
  312. this.statusCode = code
  313. this.body = body
  314. this.headers = headers
  315. }
  316. static [Symbol.hasInstance] (instance) {
  317. return instance && instance[kResponseError] === true
  318. }
  319. get [kResponseError] () {
  320. return true
  321. }
  322. }
  323. const kSecureProxyConnectionError = Symbol.for('undici.error.UND_ERR_PRX_TLS')
  324. class SecureProxyConnectionError extends UndiciError {
  325. constructor (cause, message, options = {}) {
  326. super(message, { cause, ...options })
  327. this.name = 'SecureProxyConnectionError'
  328. this.message = message || 'Secure Proxy Connection failed'
  329. this.code = 'UND_ERR_PRX_TLS'
  330. this.cause = cause
  331. }
  332. static [Symbol.hasInstance] (instance) {
  333. return instance && instance[kSecureProxyConnectionError] === true
  334. }
  335. get [kSecureProxyConnectionError] () {
  336. return true
  337. }
  338. }
  339. const kMaxOriginsReachedError = Symbol.for('undici.error.UND_ERR_MAX_ORIGINS_REACHED')
  340. class MaxOriginsReachedError extends UndiciError {
  341. constructor (message) {
  342. super(message)
  343. this.name = 'MaxOriginsReachedError'
  344. this.message = message || 'Maximum allowed origins reached'
  345. this.code = 'UND_ERR_MAX_ORIGINS_REACHED'
  346. }
  347. static [Symbol.hasInstance] (instance) {
  348. return instance && instance[kMaxOriginsReachedError] === true
  349. }
  350. get [kMaxOriginsReachedError] () {
  351. return true
  352. }
  353. }
  354. module.exports = {
  355. AbortError,
  356. HTTPParserError,
  357. UndiciError,
  358. HeadersTimeoutError,
  359. HeadersOverflowError,
  360. BodyTimeoutError,
  361. RequestContentLengthMismatchError,
  362. ConnectTimeoutError,
  363. InvalidArgumentError,
  364. InvalidReturnValueError,
  365. RequestAbortedError,
  366. ClientDestroyedError,
  367. ClientClosedError,
  368. InformationalError,
  369. SocketError,
  370. NotSupportedError,
  371. ResponseContentLengthMismatchError,
  372. BalancedPoolMissingUpstreamError,
  373. ResponseExceededMaxSizeError,
  374. RequestRetryError,
  375. ResponseError,
  376. SecureProxyConnectionError,
  377. MaxOriginsReachedError
  378. }