exceptions.py 4.0 KB

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