Coverage for portality/bll/exceptions.py: 96%

71 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-22 15:59 +0100

1import sys, traceback, json 

2 

3class AuthoriseException(Exception): 

4 """ 

5 Exception to raise if an action is not authorised 

6 ~~AuthNZ:Exception->AuthNZ:Feature~~ 

7 """ 

8 

9 # standardised reasons why an action might not be allowed 

10 NOT_OWNER = "not_owner" 

11 WRONG_ROLE = "wrong_role" 

12 WRONG_STATUS = "wrong_status" 

13 NOT_AUTHORISED = "not_authorised" 

14 

15 def __init__(self, message=None, reason=None): 

16 super(AuthoriseException, self).__init__(message) 

17 self.reason = reason 

18 

19class NoSuchFormContext(Exception): 

20 """ 

21 Exception to raise if a form context is requested that can't be found 

22 """ 

23 pass 

24 

25class ArgumentException(Exception): 

26 """ 

27 Exception to raise if an expected argument is not present 

28 """ 

29 pass 

30 

31class SaveException(Exception): 

32 """ 

33 Exception to raise if a save operation did not work as expected 

34 """ 

35 pass 

36 

37class NoSuchObjectException(Exception): 

38 """ 

39 Exception to raise if the object id given does not correspond to an actual object 

40 in the datastore 

41 """ 

42 pass 

43 

44class NoSuchPropertyException(Exception): 

45 """ 

46 Exception to raise if an object does not have a property that was required of it 

47 to complete the operation 

48 """ 

49 pass 

50 

51class ConfigurationException(Exception): 

52 """ 

53 Exception to raise when our own configuration is broken 

54 """ 

55 pass 

56 

57class DuplicateArticleException(Exception): 

58 """ 

59 Exception to raise when a duplicate article is detected, and this is not permitted 

60 """ 

61 pass 

62 

63class ArticleNotAcceptable(Exception): 

64 """ 

65 Exception to raise when an article does not have suitable data to be ingested into DOAJ 

66 """ 

67 def __init__(self, *args, **kwargs): 

68 self.message = kwargs.get("message", "") 

69 super(ArticleNotAcceptable, self).__init__(*args) 

70 

71 def __str__(self): 

72 super(ArticleNotAcceptable, self).__str__() 

73 return self.message 

74 

75class ArticleMergeConflict(Exception): 

76 """ 

77 Exception to raise when it's not clear which article to merge an update with 

78 """ 

79 pass 

80 

81class IllegalStatusException(Exception): 

82 """ 

83 Exception to raise when an application is in a state that is not allowed for the current action 

84 """ 

85 

86 def __init__(self, message=None): 

87 super(IllegalStatusException, self).__init__(message) 

88 

89class DuplicateUpdateRequest(Exception): 

90 """ 

91 Exception to raise when an attempt is made to create mulitple or duplicate update requests for a journal 

92 """ 

93 

94 def __init__(self, message=None): 

95 super(DuplicateUpdateRequest, self).__init__(message) 

96 

97 

98class IngestException(Exception): 

99 """ 

100 ~~ArticleIngest:Exception->Article:Service~~ 

101 """ 

102 def __init__(self, *args, **kwargs): 

103 self.stack = None 

104 self.message = kwargs.get("message") 

105 self.inner_message = kwargs.get("inner_message") 

106 self.inner = kwargs.get("inner") 

107 self.result = kwargs.get("result", {}) 

108 

109 tb = sys.exc_info()[2] 

110 if self.inner is not None: 

111 if self.inner_message is None and self.inner.args[0] is not None: 

112 self.inner_message = self.inner.args[0] 

113 

114 if tb is not None: 

115 self.stack = "".join(traceback.format_exception(self.inner.__class__, self.inner, tb)) 

116 else: 

117 self.stack = "".join(traceback.format_exception_only(self.inner.__class__, self.inner)) 

118 else: 

119 if tb is not None: 

120 self.stack = "".join(traceback.format_tb(tb)) 

121 else: 

122 self.stack = "".join(traceback.format_stack()) 

123 

124 def trace(self): 

125 return self.stack 

126 

127 def __str__(self): 

128 repr = "Ingest Exception: " 

129 if self.message: 

130 repr += self.message 

131 if self.inner_message: 

132 repr += " - " + self.inner_message 

133 if self.result: 

134 repr += " (" + json.dumps(self.result, cls=SetEncoder) + ")" 

135 return repr 

136 

137 

138class SetEncoder(json.JSONEncoder): 

139 def default(self, obj): 

140 if isinstance(obj, set): 

141 return list(obj) 

142 return json.JSONEncoder.default(self, obj)