@unavatar/core 3.9.4 → 3.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@unavatar/core",
3
3
  "description": "Get unified user avatar from social networks, including Instagram, SoundCloud, Telegram, Twitter, YouTube & more.",
4
4
  "homepage": "https://unavatar.io",
5
- "version": "3.9.4",
5
+ "version": "3.9.5",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": "./src/index.js",
@@ -10,13 +10,20 @@ const ExtendableError = require('./error')
10
10
 
11
11
  const NOT_FOUND = Symbol('NOT_FOUND')
12
12
 
13
+ const EMPTY_PROVIDER_VALUE_CODE = {
14
+ MISSING_STATUS_CODE: 'missing_status_code',
15
+ EMPTY_GETTER_RESULT: 'empty_getter_result'
16
+ }
17
+
13
18
  const isStatusCodeMissing = statusCode =>
14
19
  statusCode === undefined || statusCode === null || statusCode === ''
15
20
 
16
- const createEmptyProviderValueError = ({ provider, statusCode }) =>
21
+ const createProviderError = ({ provider, statusCode, cause, code }) =>
17
22
  new ExtendableError({
18
23
  provider,
19
24
  statusCode,
25
+ cause,
26
+ code,
20
27
  message: 'Empty value returned by the provider.'
21
28
  })
22
29
 
@@ -67,10 +74,22 @@ module.exports = ({ PROXY_TIMEOUT, getHTML, onFetchHTML }) => {
67
74
  typeof $ === 'function' && typeof $.html === 'function'
68
75
  ? $.html()
69
76
  : undefined
77
+ attempt.lastHeaders = responseHeaders
78
+ attempt.lastStatusCode = statusCode
70
79
 
71
80
  if (isStatusCodeMissing(statusCode)) {
72
- log.error({ statusCode, status: 'missing_status_code' })
73
- throw createEmptyProviderValueError({ provider: name, statusCode })
81
+ const code = EMPTY_PROVIDER_VALUE_CODE.MISSING_STATUS_CODE
82
+ log.error({ statusCode, status: code })
83
+ throw createProviderError({
84
+ provider: name,
85
+ statusCode,
86
+ cause: {
87
+ html: attempt.lastHtml,
88
+ headers: responseHeaders,
89
+ statusCode
90
+ },
91
+ code
92
+ })
74
93
  }
75
94
 
76
95
  if (statusCode === httpStatus.NOT_FOUND) {
@@ -80,11 +99,16 @@ module.exports = ({ PROXY_TIMEOUT, getHTML, onFetchHTML }) => {
80
99
 
81
100
  const result = getter($)
82
101
  if (typeof result !== 'string' || result === '') {
83
- const error = createEmptyProviderValueError({
102
+ const error = createProviderError({
84
103
  provider: name,
85
- statusCode
104
+ statusCode,
105
+ code: EMPTY_PROVIDER_VALUE_CODE.EMPTY_GETTER_RESULT,
106
+ cause: {
107
+ html: attempt.lastHtml,
108
+ headers: responseHeaders,
109
+ statusCode
110
+ }
86
111
  })
87
- error.html = attempt.lastHtml
88
112
 
89
113
  const { detected: antibotDetected, provider: antibotProvider } =
90
114
  isAntibot({