errors.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. 'use strict'
  2. class UndiciError extends Error {
  3. constructor (message) {
  4. super(message)
  5. this.name = 'UndiciError'
  6. this.code = 'UND_ERR'
  7. }
  8. }
  9. class ConnectTimeoutError extends UndiciError {
  10. constructor (message) {
  11. super(message)
  12. Error.captureStackTrace(this, ConnectTimeoutError)
  13. this.name = 'ConnectTimeoutError'
  14. this.message = message || 'Connect Timeout Error'
  15. this.code = 'UND_ERR_CONNECT_TIMEOUT'
  16. }
  17. }
  18. class HeadersTimeoutError extends UndiciError {
  19. constructor (message) {
  20. super(message)
  21. Error.captureStackTrace(this, HeadersTimeoutError)
  22. this.name = 'HeadersTimeoutError'
  23. this.message = message || 'Headers Timeout Error'
  24. this.code = 'UND_ERR_HEADERS_TIMEOUT'
  25. }
  26. }
  27. class HeadersOverflowError extends UndiciError {
  28. constructor (message) {
  29. super(message)
  30. Error.captureStackTrace(this, HeadersOverflowError)
  31. this.name = 'HeadersOverflowError'
  32. this.message = message || 'Headers Overflow Error'
  33. this.code = 'UND_ERR_HEADERS_OVERFLOW'
  34. }
  35. }
  36. class BodyTimeoutError extends UndiciError {
  37. constructor (message) {
  38. super(message)
  39. Error.captureStackTrace(this, BodyTimeoutError)
  40. this.name = 'BodyTimeoutError'
  41. this.message = message || 'Body Timeout Error'
  42. this.code = 'UND_ERR_BODY_TIMEOUT'
  43. }
  44. }
  45. class ResponseStatusCodeError extends UndiciError {
  46. constructor (message, statusCode, headers, body) {
  47. super(message)
  48. Error.captureStackTrace(this, ResponseStatusCodeError)
  49. this.name = 'ResponseStatusCodeError'
  50. this.message = message || 'Response Status Code Error'
  51. this.code = 'UND_ERR_RESPONSE_STATUS_CODE'
  52. this.body = body
  53. this.status = statusCode
  54. this.statusCode = statusCode
  55. this.headers = headers
  56. }
  57. }
  58. class InvalidArgumentError extends UndiciError {
  59. constructor (message) {
  60. super(message)
  61. Error.captureStackTrace(this, InvalidArgumentError)
  62. this.name = 'InvalidArgumentError'
  63. this.message = message || 'Invalid Argument Error'
  64. this.code = 'UND_ERR_INVALID_ARG'
  65. }
  66. }
  67. class InvalidReturnValueError extends UndiciError {
  68. constructor (message) {
  69. super(message)
  70. Error.captureStackTrace(this, InvalidReturnValueError)
  71. this.name = 'InvalidReturnValueError'
  72. this.message = message || 'Invalid Return Value Error'
  73. this.code = 'UND_ERR_INVALID_RETURN_VALUE'
  74. }
  75. }
  76. class RequestAbortedError extends UndiciError {
  77. constructor (message) {
  78. super(message)
  79. Error.captureStackTrace(this, RequestAbortedError)
  80. this.name = 'AbortError'
  81. this.message = message || 'Request aborted'
  82. this.code = 'UND_ERR_ABORTED'
  83. }
  84. }
  85. class InformationalError extends UndiciError {
  86. constructor (message) {
  87. super(message)
  88. Error.captureStackTrace(this, InformationalError)
  89. this.name = 'InformationalError'
  90. this.message = message || 'Request information'
  91. this.code = 'UND_ERR_INFO'
  92. }
  93. }
  94. class RequestContentLengthMismatchError extends UndiciError {
  95. constructor (message) {
  96. super(message)
  97. Error.captureStackTrace(this, RequestContentLengthMismatchError)
  98. this.name = 'RequestContentLengthMismatchError'
  99. this.message = message || 'Request body length does not match content-length header'
  100. this.code = 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
  101. }
  102. }
  103. class ResponseContentLengthMismatchError extends UndiciError {
  104. constructor (message) {
  105. super(message)
  106. Error.captureStackTrace(this, ResponseContentLengthMismatchError)
  107. this.name = 'ResponseContentLengthMismatchError'
  108. this.message = message || 'Response body length does not match content-length header'
  109. this.code = 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH'
  110. }
  111. }
  112. class ClientDestroyedError extends UndiciError {
  113. constructor (message) {
  114. super(message)
  115. Error.captureStackTrace(this, ClientDestroyedError)
  116. this.name = 'ClientDestroyedError'
  117. this.message = message || 'The client is destroyed'
  118. this.code = 'UND_ERR_DESTROYED'
  119. }
  120. }
  121. class ClientClosedError extends UndiciError {
  122. constructor (message) {
  123. super(message)
  124. Error.captureStackTrace(this, ClientClosedError)
  125. this.name = 'ClientClosedError'
  126. this.message = message || 'The client is closed'
  127. this.code = 'UND_ERR_CLOSED'
  128. }
  129. }
  130. class SocketError extends UndiciError {
  131. constructor (message, socket) {
  132. super(message)
  133. Error.captureStackTrace(this, SocketError)
  134. this.name = 'SocketError'
  135. this.message = message || 'Socket error'
  136. this.code = 'UND_ERR_SOCKET'
  137. this.socket = socket
  138. }
  139. }
  140. class NotSupportedError extends UndiciError {
  141. constructor (message) {
  142. super(message)
  143. Error.captureStackTrace(this, NotSupportedError)
  144. this.name = 'NotSupportedError'
  145. this.message = message || 'Not supported error'
  146. this.code = 'UND_ERR_NOT_SUPPORTED'
  147. }
  148. }
  149. class BalancedPoolMissingUpstreamError extends UndiciError {
  150. constructor (message) {
  151. super(message)
  152. Error.captureStackTrace(this, NotSupportedError)
  153. this.name = 'MissingUpstreamError'
  154. this.message = message || 'No upstream has been added to the BalancedPool'
  155. this.code = 'UND_ERR_BPL_MISSING_UPSTREAM'
  156. }
  157. }
  158. class HTTPParserError extends Error {
  159. constructor (message, code, data) {
  160. super(message)
  161. Error.captureStackTrace(this, HTTPParserError)
  162. this.name = 'HTTPParserError'
  163. this.code = code ? `HPE_${code}` : undefined
  164. this.data = data ? data.toString() : undefined
  165. }
  166. }
  167. class ResponseExceededMaxSizeError extends UndiciError {
  168. constructor (message) {
  169. super(message)
  170. Error.captureStackTrace(this, ResponseExceededMaxSizeError)
  171. this.name = 'ResponseExceededMaxSizeError'
  172. this.message = message || 'Response content exceeded max size'
  173. this.code = 'UND_ERR_RES_EXCEEDED_MAX_SIZE'
  174. }
  175. }
  176. module.exports = {
  177. HTTPParserError,
  178. UndiciError,
  179. HeadersTimeoutError,
  180. HeadersOverflowError,
  181. BodyTimeoutError,
  182. RequestContentLengthMismatchError,
  183. ConnectTimeoutError,
  184. ResponseStatusCodeError,
  185. InvalidArgumentError,
  186. InvalidReturnValueError,
  187. RequestAbortedError,
  188. ClientDestroyedError,
  189. ClientClosedError,
  190. InformationalError,
  191. SocketError,
  192. NotSupportedError,
  193. ResponseContentLengthMismatchError,
  194. BalancedPoolMissingUpstreamError,
  195. ResponseExceededMaxSizeError
  196. }