Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
yandexexception.php
1<?php
2namespace Bitrix\Seo\Engine;
3
7
8Loc::loadMessages(__DIR__.'/../../seo_search.php');
9
11 extends \Exception
12{
13 protected $code;
14 protected $message;
15
16 protected $result;
17 protected $status;
18
19 public function __construct($queryResult, \Exception $previous = NULL)
20 {
21// exception use two classes - new and old. Define them
22 if ($queryResult)
23 {
24 if ($queryResult instanceof \CHTTP)
25 {
26 $this->result = $queryResult->result;
27 $this->status = $queryResult->status;
28 }
29 elseif ($queryResult instanceof Web\HttpClient)
30 {
31 $this->result = $queryResult->getResult();
32 $this->status = $queryResult->getStatus();
33 }
34 }
35
36 if (!$queryResult)
37 {
38 parent::__construct('no result', 0, $previous);
39 }
40 elseif ($this->parseError())
41 {
42 $this->formatMessage(); //format and try translate message
43 parent::__construct($this->message, $this->status, $previous);
44 }
45 else
46 {
47 parent::__construct($this->result, $this->status, $previous);
48 }
49 }
50
51 public function getStatus()
52 {
53 return $this->status;
54 }
55
56 protected function parseError()
57 {
58 $matches = array();
59// old style dbg: maybe delete? In new webmaster API this format not using already
60 if (preg_match("/<error code=\"([^\"]+)\"><message>([^<]+)<\/message><\/error>/", $this->result, $matches))
61 {
62 $this->code = $matches[1];
63 $this->message = $matches[2];
64
65// Try translate error. If unknown error - write as is
66 $codeTranslated = Loc::getMessage('YANDEX_ERROR__'.str_replace(' ','_',ToUpper($this->code)));
67 $messageTranslated = Loc::getMessage('YANDEX_ERROR__'.str_replace(' ','_',ToUpper($this->message)));
68 $this->code = ($codeTranslated <> '') ? $codeTranslated : $this->code;
69 $this->message = ($messageTranslated <> '') ? $messageTranslated : $this->message;
70
71 return true;
72 }
73
74// new style
75 if ($resultArray = Json::decode($this->result))
76 {
77 if (array_key_exists('error_code', $resultArray))
78 $this->code = $resultArray["error_code"];
79 if (array_key_exists('error_message', $resultArray))
80 $this->message = $resultArray["error_message"];
81
82 return true;
83 }
84
85 return false;
86 }
87
88 private function formatMessage()
89 {
90 $translateString = Loc::getMessage('SEO_ERROR_'.$this->code);
91 if($translateString <> '')
92 {
93 $this->message = $translateString.' ('.Loc::getMessage('SEO_ERROR_CODE').': '.$this->code.').';
94 }
95 else
96 {
97 $this->message = $this->code . ': ' . $this->message;
98 }
99 }
100}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
__construct($queryResult, \Exception $previous=NULL)