__init__.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import re
  17. from twisted.internet.defer import CancelledError
  18. from twisted.python import failure
  19. from synapse.api.errors import SynapseError
  20. class RequestTimedOutError(SynapseError):
  21. """Exception representing timeout of an outbound request"""
  22. def __init__(self):
  23. super(RequestTimedOutError, self).__init__(504, "Timed out")
  24. def cancelled_to_request_timed_out_error(value, timeout):
  25. """Turns CancelledErrors into RequestTimedOutErrors.
  26. For use with async.add_timeout_to_deferred
  27. """
  28. if isinstance(value, failure.Failure):
  29. value.trap(CancelledError)
  30. raise RequestTimedOutError()
  31. return value
  32. ACCESS_TOKEN_RE = re.compile(r'(\?.*access(_|%5[Ff])token=)[^&]*(.*)$')
  33. def redact_uri(uri):
  34. """Strips access tokens from the uri replaces with <redacted>"""
  35. return ACCESS_TOKEN_RE.sub(
  36. r'\1<redacted>\3',
  37. uri
  38. )