exceptions.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ''' Put all exceptions here. '''
  2. class ResourceError(RuntimeError):
  3. '''
  4. Raised in an attempt to create a resource a URI that already exists and is
  5. not supposed to.
  6. This usually surfaces at the HTTP level as a 409.
  7. '''
  8. def __init__(self, uuid, msg=None):
  9. self.uuid = uuid
  10. self.msg = msg
  11. class ResourceExistsError(ResourceError):
  12. '''
  13. Raised in an attempt to create a resource a URI that already exists and is
  14. not supposed to.
  15. This usually surfaces at the HTTP level as a 409.
  16. '''
  17. def __repr__(self):
  18. return self.msg or 'Resource #{} already exists.'.format(self.uuid)
  19. class ResourceNotExistsError(ResourceError):
  20. '''
  21. Raised in an attempt to create a resource a URN that does not exist and is
  22. supposed to.
  23. This usually surfaces at the HTTP level as a 404.
  24. '''
  25. def __repr__(self):
  26. return self.msg or 'Resource #{} does not exist.'.format(self.uuid)
  27. class InvalidResourceError(ResourceError):
  28. '''
  29. Raised when an invalid resource is found.
  30. This usually surfaces at the HTTP level as a 409 or other error.
  31. '''
  32. def __repr__(self):
  33. return self.msg or 'Resource #{} is invalid.'.format(self.uuid)
  34. class ServerManagedTermError(RuntimeError):
  35. '''
  36. Raised in an attempt to change a triple containing a server-managed term.
  37. This usually surfaces at the HTTP level as a 409 or other error.
  38. '''
  39. def __init__(self, terms, term_type):
  40. if term_type == 's':
  41. term_name = 'subject'
  42. elif term_type == 'p':
  43. term_name = 'predicate'
  44. elif term_type == 't':
  45. term_name = 'RDF type'
  46. else:
  47. term_name = 'term'
  48. self.terms = terms
  49. self.term_name = term_name
  50. def __str__(self):
  51. return 'Some {}s are server managed and cannot be modified: {}'\
  52. .format(self.term_name, ' , '.join(self.terms))
  53. class SingleSubjectError(RuntimeError):
  54. '''
  55. Raised when a SPARQL-Update query or a RDF payload for a PUT contain
  56. subjects that do not correspond to the resource being operated on.
  57. '''
  58. def __init__(self, uri, subject):
  59. self.uri = uri
  60. self.subject = subject
  61. def __str__(self):
  62. return '{} is not in the topic of this RDF, which is {}'.format(
  63. self.uri, self.subject)