mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 12:40:35 +00:00
Added fix and test (#1302)
This commit is contained in:
@@ -9,29 +9,6 @@ from gamestonk_terminal.rich_config import console
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def try_except(f):
|
||||
"""Adds a try except block if the user is not in development mode
|
||||
|
||||
Parameters
|
||||
----------
|
||||
f: function
|
||||
The function to be wrapped
|
||||
"""
|
||||
# pylint: disable=inconsistent-return-statements
|
||||
@functools.wraps(f)
|
||||
def inner(*args, **kwargs):
|
||||
if os.environ.get("DEBUG_MODE") == "true":
|
||||
return f(*args, **kwargs)
|
||||
try:
|
||||
return f(*args, **kwargs)
|
||||
except Exception as e:
|
||||
console.print(f"[red]Error: {e}[/red]")
|
||||
logger.exception("%s", type(e).__name__)
|
||||
return []
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
def log_start_end(func=None, log=None):
|
||||
"""Wrap function to add a log entry at execution start and end.
|
||||
|
||||
@@ -83,11 +60,16 @@ def log_start_end(func=None, log=None):
|
||||
else:
|
||||
log.info("START", extra={"func_name_override": func.__name__})
|
||||
|
||||
if os.environ.get("DEBUG_MODE") == "true":
|
||||
value = func(*args, **kwargs)
|
||||
log.info("END", extra={"func_name_override": func.__name__})
|
||||
return value
|
||||
try:
|
||||
value = func(*args, **kwargs)
|
||||
log.info("END", extra={"func_name_override": func.__name__})
|
||||
return value
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
console.print(f"[red]Error: {e}[/red]")
|
||||
log.exception("Exception", extra={"func_name_override": func.__name__})
|
||||
return None
|
||||
|
||||
|
||||
@@ -6,16 +6,19 @@ import os
|
||||
from typing import List
|
||||
import webbrowser
|
||||
from datetime import datetime
|
||||
import logging
|
||||
from ast import literal_eval
|
||||
from prompt_toolkit.completion import NestedCompleter
|
||||
from gamestonk_terminal.rich_config import console
|
||||
from gamestonk_terminal.decorators import try_except
|
||||
import papermill as pm
|
||||
|
||||
from gamestonk_terminal.rich_config import console
|
||||
from gamestonk_terminal.decorators import log_start_end
|
||||
from gamestonk_terminal.parent_classes import BaseController
|
||||
from gamestonk_terminal import feature_flags as gtff
|
||||
from gamestonk_terminal.menu import session
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ReportController(BaseController):
|
||||
"""Report Controller class."""
|
||||
@@ -91,7 +94,7 @@ Select one of the following reports:[/info][cmds]
|
||||
{self.reports_opts}[/cmds]"""
|
||||
console.print(text=help_text, menu="Jupyter - Reports")
|
||||
|
||||
@try_except
|
||||
@log_start_end(log=logger)
|
||||
def switch(self, an_input: str):
|
||||
"""Process and dispatch input.
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ from gamestonk_terminal.decorators import log_start_end
|
||||
|
||||
from gamestonk_terminal.menu import session
|
||||
from gamestonk_terminal import feature_flags as gtff
|
||||
from gamestonk_terminal.decorators import try_except
|
||||
from gamestonk_terminal.helper_funcs import (
|
||||
system_clear,
|
||||
get_flair,
|
||||
@@ -120,7 +119,6 @@ class BaseController(metaclass=ABCMeta):
|
||||
raise NotImplementedError("Must override print_help")
|
||||
|
||||
@log_start_end(log=logger)
|
||||
@try_except
|
||||
def switch(self, an_input: str) -> List[str]:
|
||||
"""Process and dispatch input
|
||||
|
||||
|
||||
24
tests/gamestonk_terminal/test_debug_mode.py
Normal file
24
tests/gamestonk_terminal/test_debug_mode.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import logging
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from gamestonk_terminal.decorators import log_start_end
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@log_start_end(log=logger)
|
||||
def function_that_fails():
|
||||
raise ValueError("Failure")
|
||||
|
||||
|
||||
def test_debug_false():
|
||||
os.environ["DEBUG_MODE"] = "false"
|
||||
function_that_fails()
|
||||
|
||||
|
||||
def test_debug_true():
|
||||
os.environ["DEBUG_MODE"] = "true"
|
||||
with pytest.raises(ValueError):
|
||||
function_that_fails()
|
||||
Reference in New Issue
Block a user