.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
exceptions.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Exception types raised by SearXNG modules.
3"""
4from __future__ import annotations
5
6from typing import Optional, Union
7
8
9class SearxException(Exception):
10 """Base SearXNG exception."""
11
12
14 """Raised when query miss a required parameter"""
15
16 def __init__(self, name, value):
17 if value == '' or value is None:
18 message = 'Empty ' + name + ' parameter'
19 else:
20 message = 'Invalid value "' + value + '" for parameter ' + name
21 super().__init__(message)
22 self.message = message
23 self.parameter_name = name
24 self.parameter_value = value
25
26
27class SearxSettingsException(SearxException):
28 """Error while loading the settings"""
29
30 def __init__(self, message: Union[str, Exception], filename: Optional[str]):
31 super().__init__(message)
32 self.message = message
33 self.filename = filename
34
35
36class SearxEngineException(SearxException):
37 """Error inside an engine"""
38
39
40class SearxXPathSyntaxException(SearxEngineException):
41 """Syntax error in a XPATH"""
42
43 def __init__(self, xpath_spec, message):
44 super().__init__(str(xpath_spec) + " " + message)
45 self.message = message
46 # str(xpath_spec) to deal with str and XPath instance
47 self.xpath_str = str(xpath_spec)
48
49
50class SearxEngineResponseException(SearxEngineException):
51 """Impossible to parse the result of an engine"""
52
53
54class SearxEngineAPIException(SearxEngineResponseException):
55 """The website has returned an application error"""
56
57
58class SearxEngineAccessDeniedException(SearxEngineResponseException):
59 """The website is blocking the access"""
60
61 SUSPEND_TIME_SETTING = "search.suspended_times.SearxEngineAccessDenied"
62 """This settings contains the default suspended time (default 86400 sec / 1
63 day)."""
64
65 def __init__(self, suspended_time: int | None = None, message: str = 'Access denied'):
66 """Generic exception to raise when an engine denies access to the results.
67
68 :param suspended_time: How long the engine is going to be suspended in
69 second. Defaults to None.
70 :type suspended_time: int, None
71 :param message: Internal message. Defaults to ``Access denied``
72 :type message: str
73 """
74 if suspended_time is None:
75 suspended_time = self._get_default_suspended_time()
76 super().__init__(message + ', suspended_time=' + str(suspended_time))
77 self.suspended_time = suspended_time
78 self.message = message
79
80 def _get_default_suspended_time(self) -> int:
81 from searx import get_setting # pylint: disable=C0415
82
83 return get_setting(self.SUSPEND_TIME_SETTING)
84
85
86class SearxEngineCaptchaException(SearxEngineAccessDeniedException):
87 """The website has returned a CAPTCHA."""
88
89 SUSPEND_TIME_SETTING = "search.suspended_times.SearxEngineCaptcha"
90 """This settings contains the default suspended time (default 86400 sec / 1
91 day)."""
92
93 def __init__(self, suspended_time: int | None = None, message='CAPTCHA'):
94 super().__init__(message=message, suspended_time=suspended_time)
95
96
97class SearxEngineTooManyRequestsException(SearxEngineAccessDeniedException):
98 """The website has returned a Too Many Request status code
99
100 By default, searx stops sending requests to this engine for 1 hour.
101 """
102
103 SUSPEND_TIME_SETTING = "search.suspended_times.SearxEngineTooManyRequests"
104 """This settings contains the default suspended time (default 3660 sec / 1
105 hour)."""
106
107 def __init__(self, suspended_time: int | None = None, message='Too many request'):
108 super().__init__(message=message, suspended_time=suspended_time)
109
110
111class SearxEngineXPathException(SearxEngineResponseException):
112 """Error while getting the result of an XPath expression"""
113
114 def __init__(self, xpath_spec, message):
115 super().__init__(str(xpath_spec) + " " + message)
116 self.message = message
117 # str(xpath_spec) to deal with str and XPath instance
118 self.xpath_str = str(xpath_spec)