exceptions.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. class SearxException(Exception):
  15. pass
  16. class SearxParameterException(SearxException):
  17. def __init__(self, name, value):
  18. if value == '' or value is None:
  19. message = 'Empty ' + name + ' parameter'
  20. else:
  21. message = 'Invalid value "' + value + '" for parameter ' + name
  22. super().__init__(message)
  23. self.message = message
  24. self.parameter_name = name
  25. self.parameter_value = value
  26. class SearxSettingsException(SearxException):
  27. """Error while loading the settings"""
  28. def __init__(self, message, filename):
  29. super().__init__(message)
  30. self.message = message
  31. self.filename = filename
  32. class SearxEngineException(SearxException):
  33. """Error inside an engine"""
  34. class SearxXPathSyntaxException(SearxEngineException):
  35. """Syntax error in a XPATH"""
  36. def __init__(self, xpath_spec, message):
  37. super().__init__(str(xpath_spec) + " " + message)
  38. self.message = message
  39. # str(xpath_spec) to deal with str and XPath instance
  40. self.xpath_str = str(xpath_spec)
  41. class SearxEngineResponseException(SearxEngineException):
  42. """Impossible to parse the result of an engine"""
  43. class SearxEngineAPIException(SearxEngineResponseException):
  44. """The website has returned an application error"""
  45. class SearxEngineAccessDeniedException(SearxEngineResponseException):
  46. """The website is blocking the access"""
  47. def __init__(self, suspended_time=24 * 3600, message='Access denied'):
  48. super().__init__(message + ', suspended_time=' + str(suspended_time))
  49. self.suspended_time = suspended_time
  50. self.message = message
  51. class SearxEngineCaptchaException(SearxEngineAccessDeniedException):
  52. """The website has returned a CAPTCHA
  53. By default, searx stops sending requests to this engine for 1 day.
  54. """
  55. def __init__(self, suspended_time=24 * 3600, message='CAPTCHA'):
  56. super().__init__(message=message, suspended_time=suspended_time)
  57. class SearxEngineTooManyRequestsException(SearxEngineAccessDeniedException):
  58. """The website has returned a Too Many Request status code
  59. By default, searx stops sending requests to this engine for 1 hour.
  60. """
  61. def __init__(self, suspended_time=3600, message='Too many request'):
  62. super().__init__(message=message, suspended_time=suspended_time)
  63. class SearxEngineXPathException(SearxEngineResponseException):
  64. """Error while getting the result of an XPath expression"""
  65. def __init__(self, xpath_spec, message):
  66. super().__init__(str(xpath_spec) + " " + message)
  67. self.message = message
  68. # str(xpath_spec) to deal with str and XPath instance
  69. self.xpath_str = str(xpath_spec)