Coverage for cc_modules/cc_exception.py : 78%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
3"""
4camcops_server/cc_modules/cc_exception.py
6===============================================================================
8 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com).
10 This file is part of CamCOPS.
12 CamCOPS is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 CamCOPS is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.
25===============================================================================
27**Exception-handling functions.**
29"""
31import logging
32from typing import NoReturn
34from cardinal_pythonlib.logs import BraceStyleAdapter
36log = BraceStyleAdapter(logging.getLogger(__name__))
39# =============================================================================
40# Exception constants
41# =============================================================================
43STR_FORMAT_EXCEPTIONS = (
44 # Exceptions that can be raised by str.format()
45 IndexError, # missing positional parameter: "{}, {}".format(1)
46 KeyError, # missing named parameter: "{x}".format(y=2)
47 ValueError # e.g. unmatched brace: "{x".format(x=1)
48)
51# =============================================================================
52# Exception functions
53# =============================================================================
55def raise_runtime_error(msg: str) -> NoReturn:
56 """
57 Reports an error message to the Python log and raises a
58 :exc:`RuntimeError`.
59 """
60 log.critical(msg)
61 raise RuntimeError(msg)