Skip to content

Commit

Permalink
Merge pull request #298 from ZIMkaRU/bugfix/improve-bfx-api-error-han…
Browse files Browse the repository at this point in the history
…dling

Improve bfx api error handling
  • Loading branch information
prdn authored Jun 16, 2023
2 parents 6031c00 + 7be9cf8 commit 8a6a4b3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
37 changes: 22 additions & 15 deletions workers/loc.api/helpers/api-errors-testers.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,70 @@
'use strict'

const _getErrorString = (err) => {
const response = err?.response
? ` ${err.response}`
: ''
return `${err.toString()}${response}`
}

const isAuthError = (err) => {
return /(token: invalid)|(missing api key or secret)|(apikey: digest invalid)|(apikey: invalid)|(ERR_AUTH_UNAUTHORIZED)/.test(err.toString())
return /(token: invalid)|(missing api key or secret)|(apikey: digest invalid)|(apikey: invalid)|(ERR_AUTH_UNAUTHORIZED)/.test(_getErrorString(err))
}

const isRateLimitError = (err) => {
return /(ERR(_RATE)?_LIMIT)|(ratelimit)/.test(err.toString())
return /(ERR(_RATE)?_LIMIT)|(ratelimit)/.test(_getErrorString(err))
}

const isNonceSmallError = (err) => {
return /nonce: small/.test(err.toString())
return /nonce: small/.test(_getErrorString(err))
}

const isUserIsNotMerchantError = (err) => {
return /ERR_INVOICE_LIST: ERR_PAY_USER_NOT_MERCHANT/.test(err.toString())
return /ERR_INVOICE_LIST: ERR_PAY_USER_NOT_MERCHANT/.test(_getErrorString(err))
}

const isSymbolInvalidError = (err) => {
return /(symbol: invalid)|(currency: invalid)/.test(err.toString())
return /(symbol: invalid)|(currency: invalid)/.test(_getErrorString(err))
}

const isENetUnreachError = (err) => {
return /ENETUNREACH/.test(err.toString())
return /ENETUNREACH/.test(_getErrorString(err))
}

const isEConnResetError = (err) => {
return /ECONNRESET/.test(err.toString())
return /ECONNRESET/.test(_getErrorString(err))
}

const isETimedOutError = (err) => {
return /ETIMEDOUT/.test(err.toString())
return /ETIMEDOUT/.test(_getErrorString(err))
}

const isEAiAgainError = (err) => {
return /EAI_AGAIN/.test(err.toString())
return /EAI_AGAIN/.test(_getErrorString(err))
}

const isEConnRefusedError = (err) => {
return /ECONNREFUSED/.test(err.toString())
return /ECONNREFUSED/.test(_getErrorString(err))
}

const isENotFoundError = (err) => {
return /ENOTFOUND/.test(err.toString())
return /ENOTFOUND/.test(_getErrorString(err))
}

const isESocketTimeoutError = (err) => {
return /ESOCKETTIMEDOUT/.test(err.toString())
return /ESOCKETTIMEDOUT/.test(_getErrorString(err))
}

const isEHostUnreachError = (err) => {
return /EHOSTUNREACH/.test(err.toString())
return /EHOSTUNREACH/.test(_getErrorString(err))
}

const isEProtoError = (err) => {
return /EPROTO/.test(err.toString())
return /EPROTO/.test(_getErrorString(err))
}

const isTempUnavailableError = (err) => {
return /temporarily_unavailable/.test(err.toString())
return /temporarily_unavailable/.test(_getErrorString(err))
}

const isENetError = (err) => (
Expand Down
10 changes: 9 additions & 1 deletion workers/loc.api/responder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,19 @@ const _getErrorMetadata = (args, err) => {
data = null
} = errWithMetadata

const _message = err?.status
? `${message}
- BFX_API_STATUS: ${err.status}
- BFX_API_STATUS_TEXT: ${err.statustext ?? 'Status text is not abailable'}
- BFX_API_RAW_BODY_CODE: ${err.code ?? 'Code is not abailable'}
- BFX_API_RAW_BODY_RESPONSE: ${err.response ?? 'Response is not abailable'}`
: message

const error = Object.assign(
errWithMetadata,
{
statusCode: code,
statusMessage: message,
statusMessage: _message,
data
}
)
Expand Down

0 comments on commit 8a6a4b3

Please sign in to comment.