Skip to content

MessageHandler

MessageHandler()

capture_exceptions(code)

Run code and capture any exceptions as error messages.

Exceptions with empty messages are not reported as error messages but still mark the handler as failed.

PARAMETER DESCRIPTION
code

The function to run.

TYPE: Callable[[], None]

RETURNS DESCRIPTION
bool

True if code ran without exceptions, False otherwise.

error(message, file=None, position=None, position_end=None)

Report an error and mark current script as failed.

PARAMETER DESCRIPTION
message

The error message.

TYPE: str

file

The file where the error occurred.

TYPE: str | None DEFAULT: None

position

The position in the file where the error occurred.

TYPE: Any DEFAULT: None

position_end

The end position in the file where the error occurred.

TYPE: Any DEFAULT: None

error_from_exception(exception, omit_empty=False)

Print message from exception.

If the exception is a FilePositionException, the file and position information will be used.

PARAMETER DESCRIPTION
exception

The exception to print the message from.

TYPE: Exception

omit_empty

Whether to omit messages with empty text. Handler will still be marked as failed.

TYPE: bool DEFAULT: False

fail()

Mark the message handler as failed.

has_failed()

Check if the message handler has failed (errors reported).

RETURNS DESCRIPTION
bool

True if the message handler has failed, False otherwise.

info(message, file=None, position=None, position_end=None)

Print informational message using current script.

PARAMETER DESCRIPTION
message

Informational message.

TYPE: str

file

The file where the message occurred.

TYPE: str | None DEFAULT: None

position

The position in the file where the message occurred.

TYPE: Any DEFAULT: None

position_end

The end position in the file where the message occurred.

TYPE: Any DEFAULT: None

is_ok()

Check if the message handler is ok (no errors reported).

RETURNS DESCRIPTION
bool

True if the message handler is ok, False otherwise.

message(message, prefix=None, file=None, position=None, position_end=None, fail=False)

Print message using current script.

This method should be overridden by subclasses.

PARAMETER DESCRIPTION
prefix

Prefix for the message.

TYPE: str | None DEFAULT: None

message

Message to print.

TYPE: str

file

The file where the message occurred.

TYPE: str | None DEFAULT: None

position

The position in the file where the message occurred.

TYPE: Any DEFAULT: None

position_end

The end position in the file where the message occurred.

TYPE: Any DEFAULT: None

fail

Whether the message indicates a failure.

TYPE: bool DEFAULT: False

reset()

Reset the message handler to ok.

warning(message, file=None, position=None, position_end=None)

Print warning using current script.

PARAMETER DESCRIPTION
message

Warning message.

TYPE: str

file

The file where the warning occurred.

TYPE: str | None DEFAULT: None

position

The position in the file where the warning occurred.

TYPE: Any DEFAULT: None

position_end

The end position in the file where the warning occurred.

TYPE: Any DEFAULT: None

current_message_handler(use_fallback=False)

Get the current message handler.

RETURNS DESCRIPTION
MessageHandler

The current message handler.

error(message, file=None, position=None, position_end=None)

Report an error and mark current message handler as failed.

PARAMETER DESCRIPTION
message

The error message.

TYPE: str

file

The file where the error occurred.

TYPE: str | None DEFAULT: None

position

The position in the file where the error occurred.

TYPE: Any DEFAULT: None

position_end

The end position in the file where the error occurred.

TYPE: Any DEFAULT: None

error_from_exception(exception, omit_empty=False)

Print message from exception using current message handler.

If the exception is a FilePositionException, the file and position information will be used.

PARAMETER DESCRIPTION
exception

The exception to print the message from.

TYPE: Exception

omit_empty

Whether to omit messages with empty text. Handler will still be marked as failed.

TYPE: bool DEFAULT: False

info(message, file=None, position=None, position_end=None)

Print informational message using current message handler.

PARAMETER DESCRIPTION
message

Informational message.

TYPE: str

file

The file where the message occurred.

TYPE: str | None DEFAULT: None

position

The position in the file where the message occurred.

TYPE: Any DEFAULT: None

position_end

The end position in the file where the message occurred.

TYPE: Any DEFAULT: None

message(message, prefix=None, file=None, position=None, position_end=None, fail=False)

Print message using current message handler.

If there is no current message handler, messages that mark a failure will raise a RuntimeError, others will be ignored.

PARAMETER DESCRIPTION
message

Message to print.

TYPE: str

prefix

Prefix for the message.

TYPE: str | None DEFAULT: None

file

The file where the message occurred.

TYPE: str | None DEFAULT: None

position

The position in the file where the message occurred.

TYPE: Any DEFAULT: None

position_end

The end position in the file where the message occurred.

TYPE: Any DEFAULT: None

fail

Whether the message indicates a failure.

TYPE: bool DEFAULT: False

pop_message_handler()

Pop the current message handler from the stack.

push_message_handler(handler)

Push a new message handler onto the stack.

PARAMETER DESCRIPTION
handler

The message handler to push.

TYPE: MessageHandler

remove_message_handler(handler)

Remove a message handler from the stack.

PARAMETER DESCRIPTION
handler

The message handler to remove.

TYPE: MessageHandler

warning(message, file=None, position=None, position_end=None)

Print warning using current message handler.

PARAMETER DESCRIPTION
message

Warning message.

TYPE: str

file

The file where the warning occurred.

TYPE: str | None DEFAULT: None

position

The position in the file where the warning occurred.

TYPE: Any DEFAULT: None

position_end

The end position in the file where the warning occurred.

TYPE: Any DEFAULT: None