link-in - רשומות קוד

חפש את הקוד שלך >

try & catch

Try & catch

        try {
            $this->itemPrice->runExecute();
        } catch (Exception $e){
            $this->logger->critical('Priority Import prices failed.', ['exception' => $e]);
            $this->messageManager->addErrorMessage($e->getMessage());
        }

דוגמה לביטול הצגת שגיאות של מגנטו : 

@ssh2_sftp($connection)

דוגמה לאיחוד מספר פונקציות לטובת רישום יחיד : 

                try {
                    if (empty($host) || empty($username) || empty($password)) {
                        throw new \Exception('some SFTP Details are missing');
                    }

                    if (!function_exists("ssh2_connect")) {
                        throw new \Exception('Function ssh2_connect not found, you cannot use ssh2 here');
                    }
                    if (!$connection = @ssh2_connect($host, $port)) {
                        throw new \Exception('Unable to connect');
                    }
                    if (!@ssh2_auth_password($connection, $username, $password)) {
                        throw new \Exception('Unable to authenticate.');
                    }
                    if (!$sftp = @ssh2_sftp($connection)) {
                        throw new \Exception('Unable to create a stream.');
                    }
                    $file = fopen('ssh2.sftp://' . intval($sftp) . '/' . $remoteDir, 'r');
                    if (file_put_contents($localFilePath, $file) === false) {
                        throw \Exception('Unable to save file');
                    }
                } catch (\Exception $e){
                    $this->output->writeln($e->getMessage());
                    \Magento\Framework\App\ObjectManager::getInstance()
                        ->get(\Psr\Log\LoggerInterface::class)->error('PriceImport: '.$e->getMessage());
                    $this->addEmailError('PriceImport : Failed to connect to SFTP or download file');
                    $this->sendEmailWithErrors();
                    return false;
                }