exceptions.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. '''
  2. searx is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU Affero General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. searx is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Affero General Public License for more details.
  10. You should have received a copy of the GNU Affero General Public License
  11. along with searx. If not, see < http://www.gnu.org/licenses/ >.
  12. (C) 2017- by Alexandre Flament, <alex@al-f.net>
  13. '''
  14. from typing import Optional, Union
  15. class SearxException(Exception):
  16. pass
  17. class SearxParameterException(SearxException):
  18. def __init__(self, name, value):
  19. if value == '' or value is None:
  20. message = 'Empty ' + name + ' parameter'
  21. else:
  22. message = 'Invalid value "' + value + '" for parameter ' + name
  23. super().__init__(message)
  24. self.message = message
  25. self.parameter_name = name
  26. self.parameter_value = value
  27. class SearxSettingsException(SearxException):
  28. """Error while loading the settings"""
  29. def __init__(self, message: Union[str, Exception], filename: Optional[str]):
  30. super().__init__(message)
  31. self.message = message
  32. self.filename = filename
  33. class SearxEngineException(SearxException):
  34. """Error inside an engine"""
  35. class SearxXPathSyntaxException(SearxEngineException):
  36. """Syntax error in a XPATH"""
  37. def __init__(self, xpath_spec, message):
  38. super().__init__(str(xpath_spec) + " " + message)
  39. self.message = message
  40. # str(xpath_spec) to deal with str and XPath instance
  41. self.xpath_str = str(xpath_spec)
  42. class SearxEngineResponseException(SearxEngineException):
  43. """Impossible to parse the result of an engine"""
  44. class SearxEngineAPIException(SearxEngineResponseException):
  45. """The website has returned an application error"""
  46. class SearxEngineAccessDeniedException(SearxEngineResponseException):
  47. """The website is blocking the access"""
  48. SUSPEND_TIME_SETTING = "search.suspended_times.SearxEngineAccessDenied"
  49. """This settings contains the default suspended time"""
  50. def __init__(self, suspended_time: int = None, message: str = 'Access denied'):
  51. """Generic exception to raise when an engine denies access to the results
  52. Args:
  53. suspended_time (int, optional): How long the engine is going to be suspended in second. Defaults to None.
  54. message (str, optional): Internal message. Defaults to 'Access denied'.
  55. """
  56. suspended_time = suspended_time or self._get_default_suspended_time()
  57. super().__init__(message + ', suspended_time=' + str(suspended_time))
  58. self.suspended_time = suspended_time
  59. self.message = message
  60. def _get_default_suspended_time(self):
  61. from searx import get_setting
  62. return get_setting(self.SUSPEND_TIME_SETTING)
  63. class SearxEngineCaptchaException(SearxEngineAccessDeniedException):
  64. """The website has returned a CAPTCHA
  65. By default, searx stops sending requests to this engine for 1 day.
  66. """
  67. SUSPEND_TIME_SETTING = "search.suspended_times.SearxEngineCaptcha"
  68. def __init__(self, suspended_time=None, message='CAPTCHA'):
  69. super().__init__(message=message, suspended_time=suspended_time)
  70. class SearxEngineTooManyRequestsException(SearxEngineAccessDeniedException):
  71. """The website has returned a Too Many Request status code
  72. By default, searx stops sending requests to this engine for 1 hour.
  73. """
  74. SUSPEND_TIME_SETTING = "search.suspended_times.SearxEngineTooManyRequests"
  75. def __init__(self, suspended_time=None, message='Too many request'):
  76. super().__init__(message=message, suspended_time=suspended_time)
  77. class SearxEngineXPathException(SearxEngineResponseException):
  78. """Error while getting the result of an XPath expression"""
  79. def __init__(self, xpath_spec, message):
  80. super().__init__(str(xpath_spec) + " " + message)
  81. self.message = message
  82. # str(xpath_spec) to deal with str and XPath instance
  83. self.xpath_str = str(xpath_spec)