Coverage for cc_modules/cc_exception.py: 82%

11 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-08 23:14 +0000

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/cc_modules/cc_exception.py 

5 

6=============================================================================== 

7 

8 Copyright (C) 2012, University of Cambridge, Department of Psychiatry. 

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

10 

11 This file is part of CamCOPS. 

12 

13 CamCOPS is free software: you can redistribute it and/or modify 

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

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

16 (at your option) any later version. 

17 

18 CamCOPS is distributed in the hope that it will be useful, 

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

20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

21 GNU General Public License for more details. 

22 

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

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

25 

26=============================================================================== 

27 

28**Exception-handling functions.** 

29 

30""" 

31 

32import logging 

33from typing import NoReturn 

34 

35from cardinal_pythonlib.logs import BraceStyleAdapter 

36 

37log = BraceStyleAdapter(logging.getLogger(__name__)) 

38 

39 

40# ============================================================================= 

41# Exception constants 

42# ============================================================================= 

43 

44STR_FORMAT_EXCEPTIONS = ( 

45 # Exceptions that can be raised by str.format() 

46 IndexError, # missing positional parameter: "{}, {}".format(1) 

47 KeyError, # missing named parameter: "{x}".format(y=2) 

48 ValueError, # e.g. unmatched brace: "{x".format(x=1) 

49) 

50 

51 

52# ============================================================================= 

53# Exceptions 

54# ============================================================================= 

55 

56 

57class FhirExportException(Exception): 

58 pass 

59 

60 

61# ============================================================================= 

62# Exception functions 

63# ============================================================================= 

64 

65 

66def raise_runtime_error(msg: str) -> NoReturn: 

67 """ 

68 Reports an error message to the Python log and raises a 

69 :exc:`RuntimeError`. 

70 """ 

71 log.critical(msg) 

72 raise RuntimeError(msg)