Coverage for nlprp/errors.py: 68%

25 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-08-27 10:34 -0500

1r""" 

2crate_anon/nlprp/errors.py 

3 

4=============================================================================== 

5 

6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry. 

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

8 

9 This file is part of CRATE. 

10 

11 CRATE is free software: you can redistribute it and/or modify 

12 it under the terms of the GNU General Public License as published by 

13 the Free Software Foundation, either version 3 of the License, or 

14 (at your option) any later version. 

15 

16 CRATE is distributed in the hope that it will be useful, 

17 but WITHOUT ANY WARRANTY; without even the implied warranty of 

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

19 GNU General Public License for more details. 

20 

21 You should have received a copy of the GNU General Public License 

22 along with CRATE. If not, see <https://www.gnu.org/licenses/>. 

23 

24=============================================================================== 

25 

26Errors used by NLPRP servers. 

27 

28""" 

29 

30from cardinal_pythonlib.httpconst import HttpStatus 

31from cardinal_pythonlib.reprfunc import auto_repr 

32 

33 

34# ============================================================================= 

35# NlprpError 

36# ============================================================================= 

37 

38 

39class NlprpError(Exception): 

40 """ 

41 Represents an HTTP (and NLPRP) error. Is also an Exception, so can 

42 be raised. 

43 """ 

44 

45 def __init__( 

46 self, http_status: int, code: int, message: str, description: str = "" 

47 ) -> None: 

48 self.http_status = http_status 

49 self.code = code 

50 self.message = message 

51 self.description = description 

52 

53 def __str__(self) -> str: 

54 return ( 

55 f"NLPRP error: HTTP status {self.http_status}, " 

56 f"code {self.code}; " 

57 f"message {self.message!r}; " 

58 f"description {self.description!r}" 

59 ) 

60 

61 def __repr__(self) -> str: 

62 return auto_repr(self) 

63 

64 

65# ============================================================================= 

66# Common base errors 

67# ============================================================================= 

68 

69BAD_REQUEST = NlprpError( 

70 HttpStatus.BAD_REQUEST, 

71 HttpStatus.BAD_REQUEST, 

72 "Bad request", 

73 "Request was malformed", 

74) 

75UNAUTHORIZED = NlprpError( 

76 HttpStatus.UNAUTHORIZED, 

77 HttpStatus.UNAUTHORIZED, 

78 "Unauthorized", 

79 "The username/password combination given is incorrect", 

80) 

81NOT_FOUND = NlprpError( 

82 HttpStatus.NOT_FOUND, 

83 HttpStatus.NOT_FOUND, 

84 "Not Found", 

85 "The information requested was not found", 

86) 

87INTERNAL_SERVER_ERROR = NlprpError( 

88 HttpStatus.INTERNAL_SERVER_ERROR, 

89 HttpStatus.INTERNAL_SERVER_ERROR, 

90 "Internal Server Error", 

91 "An internal server error has occured", 

92) 

93 

94 

95# ============================================================================= 

96# Helper functions 

97# ============================================================================= 

98 

99 

100def mkerror(base_error: NlprpError, description: str = None) -> NlprpError: 

101 """ 

102 Makes a derived error by copying an existing one and amending its 

103 description. 

104 """ 

105 return NlprpError( 

106 http_status=base_error.http_status, 

107 code=base_error.code, 

108 message=base_error.message, 

109 description=description or base_error.description, 

110 ) 

111 

112 

113def key_missing_error(key: str = "", is_args: bool = False) -> NlprpError: 

114 """ 

115 Returns a '400: Bad Request' error response stating that a key is 

116 missing from 'args' in the request, or the key 'args' itself is missing 

117 """ 

118 if is_args: 

119 description = "Request did not contain top-level key 'args'" 

120 else: 

121 description = f"Args did not contain key '{key}'" 

122 return mkerror(BAD_REQUEST, description) 

123 

124 

125def no_such_proc_error(name: str, version: str = None) -> NlprpError: 

126 """ 

127 "No such processor" error. 

128 

129 Args: 

130 name: requested processor name 

131 version: requested processor version 

132 """ 

133 return mkerror( 

134 BAD_REQUEST, f"Processor {name!r}, version {version!r} does not exist" 

135 )