This repository has been archived on 2023-05-12. You can view files and clone it, but cannot push or open issues or pull requests.
aiosched/aiosched/errors.py

105 lines
2.4 KiB
Python

"""
aiosched: Yet another Python async scheduler
Copyright (C) 2022 nocturn9x
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https:www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import traceback
from aiosched.task import Task
class SchedulerError(Exception):
"""
A generic scheduler error
"""
class InternalError(SchedulerError):
"""
Internal exception
"""
class ResourceBusy(SchedulerError):
"""
Exception that is raised when a resource is
accessed by more than one task at a time
"""
class ResourceClosed(SchedulerError):
"""
Raised when I/O is attempted on a closed
resource
"""
class ResourceBroken(SchedulerError):
"""
Raised when I/O is attempted on a broken
resource
"""
class TimedOutError(SchedulerError):
"""
This is raised if a timeout expires
"""
task: Task
class Cancelled(BaseException):
"""
A cancellation exception.
Inherits from BaseException as
it is not meant to be caught
"""
task: Task
class ErrorStack(SchedulerError):
"""
This exception wraps multiple exceptions and
shows each individual traceback of them when
printed. This is to ensure that no exception is
lost even if 2 or more tasks raise at the
same time or during cancellation of other
tasks
"""
def __init__(self, errors: list[BaseException]):
"""
Object constructor
"""
super().__init__()
self.errors = errors
def __str__(self):
"""
Returns str(self)
"""
tracebacks = ""
for i, err in enumerate(self.errors):
if i not in (1, len(self.errors)):
tracebacks += (
f"\n{''.join(traceback.format_exception(type(err), err, err.__traceback__))}\n{'-' * 32}\n"
)
else:
tracebacks += f"\n{''.join(traceback.format_exception(type(err), err, err.__traceback__))}"
return f"Multiple errors occurred:\n{tracebacks}"