| const { app, dialog } = require( 'electron' ); |
| const assets = require( '../../lib/assets' ); |
| const log = require( '../../lib/logger' )( 'desktop:failed-to-load' ); |
| const settings = require( '../../lib/settings' ); |
|
|
| |
| |
| |
| const FAIL_TO_LOAD_FILE = 'failed-to-start.html'; |
| const FAILED_FILE = 'file://' + assets.getPath( FAIL_TO_LOAD_FILE ); |
| const NETWORK_FAILED_FILE = 'file://' + assets.getPath( 'network-failed.html' ); |
|
|
| |
| |
| const ERRORS_TO_IGNORE = [ |
| -3, |
| -27, |
| -30, |
| -102, |
| -109, |
| -502, |
| -501, |
| ]; |
|
|
| let finalTry = false; |
|
|
| function isErrorPage( sender ) { |
| if ( sender && typeof sender.getURL !== 'undefined' ) { |
| let url = sender.getURL(); |
|
|
| if ( url.indexOf( '#-' ) !== -1 ) { |
| url = url.substring( 0, url.indexOf( '#-' ) ); |
| } |
|
|
| if ( url === FAILED_FILE ) { |
| return true; |
| } |
| } |
|
|
| return false; |
| } |
|
|
| function failedToLoadError( view ) { |
| |
| if ( finalTry === false ) { |
| view.webContents.loadURL( `file://${ assets.getPath( 'failed-to-start.html' ) }#-666` ); |
| finalTry = true; |
| } else { |
| |
| settings.saveSetting( 'proxy-type', '' ); |
|
|
| dialog.showMessageBox( |
| { |
| buttons: [ 'OK' ], |
| title: 'Aww shucks', |
| message: |
| 'Something went wrong starting up WordPress.com. If you are using a proxy then please make sure it is running', |
| }, |
| function () { |
| app.quit(); |
| } |
| ); |
| } |
| } |
|
|
| |
| |
| module.exports = function ( { view } ) { |
| |
| view.webContents.on( |
| 'did-fail-load', |
| async function ( event, errorCode, errorDescription, validatedURL ) { |
| log.error( `Failed to load URL '${ validatedURL }'` ); |
|
|
| if ( ERRORS_TO_IGNORE.indexOf( errorCode ) === -1 ) { |
| if ( isErrorPage( event.sender ) ) { |
| failedToLoadError( view ); |
| } else { |
| log.error( |
| 'Failed to load URL, showing fallback page: code=' + errorCode + ' ' + errorDescription |
| ); |
|
|
| await view.webContents.session.setProxy( { proxyRules: 'direct://' } ); |
| const file = errorCode === -106 ? NETWORK_FAILED_FILE : FAILED_FILE; |
| view.webContents.loadURL( file + '#' + errorCode ); |
| } |
| } |
| } |
| ); |
| }; |
|
|