77 ProxyConfiguration as CoreProxyConfiguration ,
88} from '@crawlee/core' ;
99import { gotScraping } from '@crawlee/utils' ;
10+ import type { UserProxy } from 'apify-client' ;
1011import ow from 'ow' ;
1112
1213import { Actor } from './actor' ;
@@ -213,11 +214,13 @@ export class ProxyConfiguration extends CoreProxyConfiguration {
213214 * You should use the {@apilink createProxyConfiguration} function to create a pre-initialized
214215 * `ProxyConfiguration` instance instead of calling this manually.
215216 */
216- async initialize ( ) : Promise < void > {
217+ async initialize ( ) : Promise < boolean > {
217218 if ( this . usesApifyProxy ) {
218219 await this . _setPasswordIfToken ( ) ;
219- await this . _checkAccess ( ) ;
220+ return this . _checkAccess ( ) ;
220221 }
222+
223+ return true ;
221224 }
222225
223226 /**
@@ -315,7 +318,20 @@ export class ProxyConfiguration extends CoreProxyConfiguration {
315318 const token = this . config . get ( 'token' ) ;
316319
317320 if ( token ) {
318- const { proxy } = await Actor . apifyClient . user ( ) . get ( ) ;
321+ let proxy : UserProxy ;
322+
323+ try {
324+ const user = await Actor . apifyClient . user ( ) . get ( ) ;
325+ proxy = user . proxy ! ;
326+ } catch ( error ) {
327+ if ( Actor . isAtHome ( ) ) {
328+ throw error ;
329+ } else {
330+ this . log . warning ( `Failed to fetch user data based on token, disabling proxy.` , { error } ) ;
331+ return ;
332+ }
333+ }
334+
319335 const { password } = proxy ! ;
320336
321337 if ( this . password ) {
@@ -329,8 +345,14 @@ export class ProxyConfiguration extends CoreProxyConfiguration {
329345 }
330346
331347 if ( ! this . password ) {
332- throw new Error ( `Apify Proxy password must be provided using options.password or the "${ APIFY_ENV_VARS . PROXY_PASSWORD } " environment variable. `
333- + `If you add the "${ APIFY_ENV_VARS . TOKEN } " environment variable, the password will be automatically inferred.` ) ;
348+ if ( Actor . isAtHome ( ) ) {
349+ throw new Error ( `Apify Proxy password must be provided using options.password or the "${ APIFY_ENV_VARS . PROXY_PASSWORD } " environment variable. `
350+ + `If you add the "${ APIFY_ENV_VARS . TOKEN } " environment variable, the password will be automatically inferred.` ) ;
351+ } else {
352+ this . log . warning ( `No proxy password or token detected, running without proxy. To use Apify Proxy locally, `
353+ + `provide options.password or "${ APIFY_ENV_VARS . PROXY_PASSWORD } " environment variable. `
354+ + `If you add the "${ APIFY_ENV_VARS . TOKEN } " environment variable, the password will be automatically inferred.` ) ;
355+ }
334356 }
335357 }
336358
@@ -339,17 +361,31 @@ export class ProxyConfiguration extends CoreProxyConfiguration {
339361 * If the check can not be made, it only prints a warning and allows the program to continue. This is to
340362 * prevent program crashes caused by short downtimes of Proxy.
341363 */
342- protected async _checkAccess ( ) : Promise < void > {
364+ protected async _checkAccess ( ) : Promise < boolean > {
343365 const status = await this . _fetchStatus ( ) ;
344- if ( status ) {
345- const { connected, connectionError, isManInTheMiddle } = status ;
346- this . isManInTheMiddle = isManInTheMiddle ;
347366
348- if ( ! connected ) this . _throwApifyProxyConnectionError ( connectionError ) ;
349- } else {
367+ if ( ! status ) {
350368 this . log . warning ( 'Apify Proxy access check timed out. Watch out for errors with status code 407. '
351369 + 'If you see some, it most likely means you don\'t have access to either all or some of the proxies you\'re trying to use.' ) ;
370+ return true ;
352371 }
372+
373+ const { connected, connectionError, isManInTheMiddle } = status ;
374+ this . isManInTheMiddle = isManInTheMiddle ;
375+
376+ if ( connected ) {
377+ return true ;
378+ }
379+
380+ // Throw only on the platform, locally we just print a warning and run requests without the proxy.
381+ // This is because the user might not have set up things correctly yet.
382+ // It still fails on the platform, where we don't want to allow this behavior.
383+ if ( Actor . isAtHome ( ) ) {
384+ throw new Error ( connectionError ) ;
385+ }
386+
387+ this . log . warning ( connectionError ) ;
388+ return false ;
353389 }
354390
355391 /**
@@ -376,14 +412,6 @@ export class ProxyConfiguration extends CoreProxyConfiguration {
376412 return undefined ;
377413 }
378414
379- /**
380- * Throws Apify Proxy is not connected
381- * @internal
382- */
383- protected _throwApifyProxyConnectionError ( errorMessage : string ) {
384- throw new Error ( errorMessage ) ;
385- }
386-
387415 /**
388416 * Throws cannot combine custom proxies with Apify Proxy
389417 * @internal
0 commit comments