UNPKG

@n8n/n8n-nodes-langchain

Version:
61 lines • 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapDatabricksErrorFetch = wrapDatabricksErrorFetch; exports.makeDatabricksFailedAttemptHandler = makeDatabricksFailedAttemptHandler; const is_record_1 = require("@n8n/utils/is-record"); const n8n_workflow_1 = require("n8n-workflow"); const oauth2_token_provider_1 = require("../oauth2-token-provider"); function wrapDatabricksErrorFetch(baseFetch) { return async (input, init) => { const response = await baseFetch(input, init); if (response.ok) return response; const text = await response.text(); let body = text; try { const parsed = JSON.parse(text); if ((0, is_record_1.isRecord)(parsed) && !('error' in parsed) && typeof parsed.message === 'string') { body = JSON.stringify({ error: { message: parsed.message, code: parsed.error_code } }); } } catch { } const headers = new Headers(response.headers); headers.delete('content-length'); return new Response(body, { status: response.status, statusText: response.statusText, headers, }); }; } function indicatesInvalidToken(text) { return /invalid\s+(access\s+)?token|token\s+(is\s+)?(invalid|expired)|expired\s+token/i.test(text); } function isExpiredTokenResponse(error, expiredStatus) { if (typeof error !== 'object' || error === null) return false; if (!('status' in error) || error.status !== expiredStatus) return false; return ('message' in error && typeof error.message === 'string' && indicatesInvalidToken(error.message)); } const RATE_LIMIT_HINT = "Databricks is throttling requests to this model service. Wait and retry, reduce concurrent requests, or ask your workspace admin to raise the endpoint's rate limit."; function makeDatabricksFailedAttemptHandler(expiredStatus, modelName) { return (error) => { const sessionExpired = (0, oauth2_token_provider_1.findSessionExpiredError)(error); if (sessionExpired) throw sessionExpired; if (expiredStatus !== undefined && isExpiredTokenResponse(error, expiredStatus)) { throw new n8n_workflow_1.OperationalError('Databricks rejected the access token and it could not be refreshed. The sign-in session has likely expired - open the credential and select Connect to sign in again.', { cause: error }); } if ((0, is_record_1.isRecord)(error) && error.status === 429) { const detail = (0, is_record_1.isRecord)(error.error) && typeof error.error.message === 'string' ? `: ${error.error.message}` : ''; Object.assign(error, { message: `Databricks rate limit reached for ${modelName}${detail}`, description: RATE_LIMIT_HINT, }); } }; } //# sourceMappingURL=error-handling.js.map