exceptions.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. def __init__(self, suspended_time=24 * 3600, message='Access denied'):
  49. super().__init__(message + ', suspended_time=' + str(suspended_time))
  50. self.suspended_time = suspended_time
  51. self.message = message
  52. class SearxEngineCaptchaException(SearxEngineAccessDeniedException):
  53. """The website has returned a CAPTCHA
  54. By default, searx stops sending requests to this engine for 1 day.
  55. """
  56. def __init__(self, suspended_time=24 * 3600, message='CAPTCHA'):
  57. super().__init__(message=message, suspended_time=suspended_time)
  58. class SearxEngineTooManyRequestsException(SearxEngineAccessDeniedException):
  59. """The website has returned a Too Many Request status code
  60. By default, searx stops sending requests to this engine for 1 hour.
  61. """
  62. def __init__(self, suspended_time=3600, message='Too many request'):
  63. super().__init__(message=message, suspended_time=suspended_time)
  64. class SearxEngineXPathException(SearxEngineResponseException):
  65. """Error while getting the result of an XPath expression"""
  66. def __init__(self, xpath_spec, message):
  67. super().__init__(str(xpath_spec) + " " + message)
  68. self.message = message
  69. # str(xpath_spec) to deal with str and XPath instance
  70. self.xpath_str = str(xpath_spec)