def test_raise(self): with self.assertRaises(KeyError): with catch(IndexError): dict()["nothing"] with self.assertRaises(IndexError): with catch(KeyError, reraise=IndexError): dict()["nothing"] with self.assertRaises(ValueError): with catch((ZeroDivisionError, IndexError), reraise=ValueError): 1 / 0 with self.assertRaises(ValueError): with catch((ZeroDivisionError, IndexError), reraise=ValueError): list()[1] with self.assertRaises(ValueError): try: valerr = ValueError() with catch(ZeroDivisionError, reraise=valerr): 1 / 0 except ValueError as err: self.assertIs(err, valerr) raise
def test_callback(self): results = [] with catch((ZeroDivisionError, IndexError), callback=results.append): 1 / 0 self.assertIsInstance(results.pop(), ZeroDivisionError) with catch((ZeroDivisionError, IndexError), callback=results.append): list()[1] self.assertIsInstance(results.pop(), IndexError)
def test_catch_errors(self): with catch(): raise Exception() with catch(ZeroDivisionError): 1 / 0 with catch((KeyError, IndexError)): list()[1] dict()["nothing"]
def test_exec_info(self): with catch() as exec_info: self.assertTrue(exec_info["ok"]) self.assertIsNone(exec_info["exception"]) self.assertIsNone(exec_info["callback_returned"]) self.assertTrue(exec_info["ok"]) self.assertIsNone(exec_info["exception"]) self.assertIsNone(exec_info["callback_returned"]) callback = mock.MagicMock(return_value=1) with catch(NotImplementedError, callback=callback) as exec_info: raise NotImplementedError self.assertFalse(exec_info["ok"]) self.assertIsInstance(exec_info["exception"], NotImplementedError) self.assertEqual(exec_info["callback_returned"], callback.return_value)
def cd(path): """ Switch to a directory to excute. :param path: directory string """ current_dir = os.getcwd() os.chdir(path) try: yield finally: with contextutils.catch(): os.chdir(current_dir)
def fix_value(self): with catch(reraise=ModelFieldError): type = ITEM_TYPE[self.type] self.value = type.factory(self.value)
def __getattr__(self, name): with catch(KeyError, reraise=AttributeError): return self[name]
def foo(*args, **kwargs): with contextutils.catch( getattr(exceptions, "WindowsError", OSError), reraise=OSError, ): return func(*args, **kwargs)
def post(self, request): logger.info("New request for item collector") try: self.PostValidator.validate(request.data) except ValidationError as err: return Response({ "ok": False, "detail": err.message, }, status=status.HTTP_400_BAD_REQUEST) auth_key = request.data.get("auth") try: user = auth_utils.authenticate_from_key(auth_key) except auth_utils.exceptions.AuthenticationFailed as err: return Response({ "ok": False, "detail": err.detail, }, status=status.HTTP_401_UNAUTHORIZED) project_name = request.data.get("project") try: project = project_models.Project.objects.get( name=project_name, group__in=user.groups.all(), status=project_models.PROJECT_STATUS.enable, ) except project_models.Project.DoesNotExist as err: return Response({ "ok": False, "detail": "enabled project(%s) not found" % project_name, }, status=status.HTTP_404_NOT_FOUND) item_name = request.data.get("item") try: item = item_models.MonitorItem.objects.get( name=item_name, project=project, status=item_models.ITEM_STATUS.enable, ) except item_models.MonitorItem.DoesNotExist as err: return Response({ "ok": False, "detail": "enabled item(%s) not found" % item_name, }, status=status.HTTP_404_NOT_FOUND) try: with transaction.atomic(): self.update_item(item, request.data.get("value")) self.insert_item_history(item, user) except exceptions.ModelFieldError as err: return Response({ "ok": False, "detail": "value(%s) of item(%s) save failed" % ( request.data.get("value"), item_name, ), }, status=status.HTTP_406_NOT_ACCEPTABLE) logger.info("Item[%s] has updated", item_name) try: return Response({ "ok": True, "detail": { "project": project.id, "item": item.id, "value": item.value, }, }, status=status.HTTP_202_ACCEPTED) finally: with catch(): TasksPool.apply_async(self.check_triggers, [item])