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