Skip to content

Errors

API

This module contains the error classes used in the framework.

ErrorFormatter

Bases: Exception

Source code in src/gapper/core/errors.py
class ErrorFormatter(Exception):
    def extract_user_traceback(self, grader_path: str | None = None) -> List[str]:
        """Extract the user traceback from the exception.

        :param grader_path: The path to the grader file.
        """
        tbs: List[traceback.FrameSummary] = traceback.extract_tb(self.__traceback__)
        if grader_path is None:
            filtered_tbs = filter(lambda tb: "gapper" not in tb.filename, tbs)
        else:
            filtered_tbs = filter(lambda tb: grader_path not in tb.filename, tbs)
        return traceback.format_list(list(filtered_tbs))

    def extract_user_traceback_str(
        self, grader_path: str | None = None, indent_num: int = 0
    ) -> str:
        """Extract the user traceback from the exception as a string."""
        return indent(
            "\n".join(self.extract_user_traceback(grader_path)),
            " " * indent_num,
        )

    def extract_traceback_str(self, indent_num: int = 0) -> str:
        """Extract the traceback from the exception as a string."""
        return indent(
            "\n".join(traceback.format_tb(self.__traceback__)), " " * indent_num
        )

    def _get_last_tb(self, tb: TracebackType) -> TracebackType:
        while tb.tb_next is not None:
            tb = tb.tb_next
        return tb

    def format_args(self, indent_num: int = 0) -> str:
        """Format the arguments of the error message.

        :param indent_num: The number of spaces to indent the message.
        """
        return indent(
            ",\n".join(str(arg).rstrip("\n") for arg in self.args),
            " " * indent_num,
        )

    def format(self) -> str:
        """Format the error message."""
        raise NotImplementedError

extract_traceback_str

extract_traceback_str(indent_num: int = 0) -> str

Extract the traceback from the exception as a string.

Source code in src/gapper/core/errors.py
def extract_traceback_str(self, indent_num: int = 0) -> str:
    """Extract the traceback from the exception as a string."""
    return indent(
        "\n".join(traceback.format_tb(self.__traceback__)), " " * indent_num
    )

extract_user_traceback

extract_user_traceback(grader_path: str | None = None) -> List[str]

Extract the user traceback from the exception.

Parameters:

Name Type Description Default
grader_path str | None

The path to the grader file.

None
Source code in src/gapper/core/errors.py
def extract_user_traceback(self, grader_path: str | None = None) -> List[str]:
    """Extract the user traceback from the exception.

    :param grader_path: The path to the grader file.
    """
    tbs: List[traceback.FrameSummary] = traceback.extract_tb(self.__traceback__)
    if grader_path is None:
        filtered_tbs = filter(lambda tb: "gapper" not in tb.filename, tbs)
    else:
        filtered_tbs = filter(lambda tb: grader_path not in tb.filename, tbs)
    return traceback.format_list(list(filtered_tbs))

extract_user_traceback_str

extract_user_traceback_str(grader_path: str | None = None, indent_num: int = 0) -> str

Extract the user traceback from the exception as a string.

Source code in src/gapper/core/errors.py
def extract_user_traceback_str(
    self, grader_path: str | None = None, indent_num: int = 0
) -> str:
    """Extract the user traceback from the exception as a string."""
    return indent(
        "\n".join(self.extract_user_traceback(grader_path)),
        " " * indent_num,
    )

format

format() -> str

Format the error message.

Source code in src/gapper/core/errors.py
def format(self) -> str:
    """Format the error message."""
    raise NotImplementedError

format_args

format_args(indent_num: int = 0) -> str

Format the arguments of the error message.

Parameters:

Name Type Description Default
indent_num int

The number of spaces to indent the message.

0
Source code in src/gapper/core/errors.py
def format_args(self, indent_num: int = 0) -> str:
    """Format the arguments of the error message.

    :param indent_num: The number of spaces to indent the message.
    """
    return indent(
        ",\n".join(str(arg).rstrip("\n") for arg in self.args),
        " " * indent_num,
    )

InternalError

Bases: ErrorFormatter

Raised when an internal error occurs in the framework.

Source code in src/gapper/core/errors.py
class InternalError(ErrorFormatter):
    """Raised when an internal error occurs in the framework."""

    def format(self) -> str:
        tb_info = indent(
            tb_str if (tb_str := self.extract_traceback_str()) else "Not Provided\n",
            "  ",
        )
        return (
            f"Internal Error. Please report this to the developers. \n"
            f"The reason is following: \n{self.format_args(indent_num=2)}\n"
            f"Stack Trace: \n{tb_info}"
        )

NoSubmissionError

Bases: StudentError

Raised when no submission is loaded.

Source code in src/gapper/core/errors.py
class NoSubmissionError(StudentError):
    """Raised when no submission is loaded."""

    def __init__(self, expected_name: str):
        super().__init__(expected_name)

    @property
    def expected_name(self) -> str:
        return self.args[0]

    def format(self) -> str:
        return (
            f"No submission is found.\n"
            f"If you're submitting a script, please name your submission file as '{self.expected_name}'.\n"
            f"If you're writing a function or a class, "
            f"please name your submission fn/class as '{self.expected_name}'.\n"
            f"(without quotes)\n"
        )

SubmissionSyntaxError

Bases: StudentError

Raised when a submission has syntax errors.

Source code in src/gapper/core/errors.py
class SubmissionSyntaxError(StudentError):
    """Raised when a submission has syntax errors."""

    def format(self) -> str:
        return (
            f"The submission has syntax errors. "
            f"The reason is following: \n"
            f"{self.format_args(indent_num=2)}\n"
            f"{self.extract_user_traceback_str()}"
        )

TestFailedError

Bases: ErrorFormatter

Raised when a test fails.

Source code in src/gapper/core/errors.py
class TestFailedError(ErrorFormatter):
    """Raised when a test fails."""

    def format(self) -> str:
        return f"Test Assertion Failed. The reason is following: \n{self.format_args(indent_num=2)}\n"