Files
Gymnasium/gym/error.py

248 lines
4.8 KiB
Python
Raw Normal View History

2016-04-27 08:00:58 -07:00
class Error(Exception):
pass
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
# Local errors
2021-07-29 02:26:34 +02:00
class Unregistered(Error):
"""Raised when the user requests an item from the registry that does
not actually exist.
"""
2021-07-29 02:26:34 +02:00
pass
2021-07-29 02:26:34 +02:00
class UnregisteredEnv(Unregistered):
"""Raised when the user requests an env from the registry that does
not actually exist.
"""
2021-07-29 02:26:34 +02:00
pass
2021-07-29 02:26:34 +02:00
Remove mandatory version in environment name (#2535) * refactor: required version in env name * refactor: rename env_id function * refactor: regex + function logic * test: some more edge cases + error * refactor: version max/comparison - Move version max/comparison in a method - Remove type ignore to improve type checking * fix: failing tests & error introduced in rebasing * refactor: simplify map and remove type ignore * refactor: remove _is_lesser_version() method * refactor: _versions() to return NamedTuples * chore: remove minor changes * Refactor registration EnvSpec and EnvSpecTree * test: move tests from #2513 here * fix: typing * test: try to fix unregistered env * refactor: change InitVar id name for typing Changed InitVar id name to fix the following typing error: error: Declaration "id" is obscured by a declaration of the same name * refactor: return only the first difflib match * test: improve tear down of added test env * refactor: dataclass fields * refactor: compile regex pattern only once * refactor: `_assert_version_exists()` `_assert_version_exists()` rewritten from @JesseFarebro: https://github.com/openai/gym/pull/2535#discussion_r777573839 * refactor: latest_spec -> latest_versioned_spec * fix: bug + typing + test * Add default/versioned tests * Fix Env field defaults * feat: improve versioned/unversioned env handling * Disallow versioned/unversioned registration/lookup * test: remove warning + check default suggestion Co-authored-by: Jesse Farebrother <jessefarebro@gmail.com>
2022-01-19 13:50:25 -05:00
class NamespaceNotFound(UnregisteredEnv):
"""Raised when the user requests an env from the registry where the
namespace doesn't exist.
"""
pass
class NameNotFound(UnregisteredEnv):
"""Raised when the user requests an env from the registry where the
name doesn't exist.
"""
pass
class VersionNotFound(UnregisteredEnv):
"""Raised when the user requests an env from the registry where the
version doesn't exist.
"""
pass
class UnregisteredBenchmark(Unregistered):
2016-04-27 08:00:58 -07:00
"""Raised when the user requests an env from the registry that does
not actually exist.
"""
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
pass
2021-07-29 02:26:34 +02:00
class DeprecatedEnv(Error):
[WIP] add support for seeding environments (#135) * Make environments seedable * Fix monitor bugs - Set monitor_id before setting the infix. This was a bug that would yield incorrect results with multiple monitors. - Remove extra pid from stats recorder filename. This should be purely cosmetic. * Start uploading seeds in episode_batch * Fix _bigint_from_bytes for python3 * Set seed explicitly in random_agent * Pass through seed argument * Also pass through random state to spaces * Pass random state into the observation/action spaces * Make all _seed methods return the list of used seeds * Switch over to np.random where possible * Start hashing seeds, and also seed doom engine * Fixup seeding determinism in many cases * Seed before loading the ROM * Make seeding more Python3 friendly * Make the MuJoCo skipping a bit more forgiving * Remove debugging PDB calls * Make setInt argument into raw bytes * Validate and upload seeds * Skip box2d * Make seeds smaller, and change representation of seeds in upload * Handle long seeds * Fix RandomAgent example to be deterministic * Handle integer types correctly in Python2 and Python3 * Try caching pip * Try adding swap * Add df and free calls * Bump swap * Bump swap size * Try setting overcommit * Try other sysctls * Try fixing overcommit * Try just setting overcommit_memory=1 * Add explanatory comment * Add what's new section to readme * BUG: Mark ElevatorAction-ram-v0 as non-deterministic for now * Document seed * Move nondetermistic check into spec
2016-05-29 09:07:09 -07:00
"""Raised when the user requests an env from the registry with an
older version number than the latest env with the same name.
"""
2021-07-29 02:26:34 +02:00
[WIP] add support for seeding environments (#135) * Make environments seedable * Fix monitor bugs - Set monitor_id before setting the infix. This was a bug that would yield incorrect results with multiple monitors. - Remove extra pid from stats recorder filename. This should be purely cosmetic. * Start uploading seeds in episode_batch * Fix _bigint_from_bytes for python3 * Set seed explicitly in random_agent * Pass through seed argument * Also pass through random state to spaces * Pass random state into the observation/action spaces * Make all _seed methods return the list of used seeds * Switch over to np.random where possible * Start hashing seeds, and also seed doom engine * Fixup seeding determinism in many cases * Seed before loading the ROM * Make seeding more Python3 friendly * Make the MuJoCo skipping a bit more forgiving * Remove debugging PDB calls * Make setInt argument into raw bytes * Validate and upload seeds * Skip box2d * Make seeds smaller, and change representation of seeds in upload * Handle long seeds * Fix RandomAgent example to be deterministic * Handle integer types correctly in Python2 and Python3 * Try caching pip * Try adding swap * Add df and free calls * Bump swap * Bump swap size * Try setting overcommit * Try other sysctls * Try fixing overcommit * Try just setting overcommit_memory=1 * Add explanatory comment * Add what's new section to readme * BUG: Mark ElevatorAction-ram-v0 as non-deterministic for now * Document seed * Move nondetermistic check into spec
2016-05-29 09:07:09 -07:00
pass
2021-07-29 02:26:34 +02:00
Remove mandatory version in environment name (#2535) * refactor: required version in env name * refactor: rename env_id function * refactor: regex + function logic * test: some more edge cases + error * refactor: version max/comparison - Move version max/comparison in a method - Remove type ignore to improve type checking * fix: failing tests & error introduced in rebasing * refactor: simplify map and remove type ignore * refactor: remove _is_lesser_version() method * refactor: _versions() to return NamedTuples * chore: remove minor changes * Refactor registration EnvSpec and EnvSpecTree * test: move tests from #2513 here * fix: typing * test: try to fix unregistered env * refactor: change InitVar id name for typing Changed InitVar id name to fix the following typing error: error: Declaration "id" is obscured by a declaration of the same name * refactor: return only the first difflib match * test: improve tear down of added test env * refactor: dataclass fields * refactor: compile regex pattern only once * refactor: `_assert_version_exists()` `_assert_version_exists()` rewritten from @JesseFarebro: https://github.com/openai/gym/pull/2535#discussion_r777573839 * refactor: latest_spec -> latest_versioned_spec * fix: bug + typing + test * Add default/versioned tests * Fix Env field defaults * feat: improve versioned/unversioned env handling * Disallow versioned/unversioned registration/lookup * test: remove warning + check default suggestion Co-authored-by: Jesse Farebrother <jessefarebro@gmail.com>
2022-01-19 13:50:25 -05:00
class RegistrationError(Error):
"""Raised when the user attempts to register an invalid env.
For example, an unversioned env when a versioned env exists.
"""
pass
[WIP] add support for seeding environments (#135) * Make environments seedable * Fix monitor bugs - Set monitor_id before setting the infix. This was a bug that would yield incorrect results with multiple monitors. - Remove extra pid from stats recorder filename. This should be purely cosmetic. * Start uploading seeds in episode_batch * Fix _bigint_from_bytes for python3 * Set seed explicitly in random_agent * Pass through seed argument * Also pass through random state to spaces * Pass random state into the observation/action spaces * Make all _seed methods return the list of used seeds * Switch over to np.random where possible * Start hashing seeds, and also seed doom engine * Fixup seeding determinism in many cases * Seed before loading the ROM * Make seeding more Python3 friendly * Make the MuJoCo skipping a bit more forgiving * Remove debugging PDB calls * Make setInt argument into raw bytes * Validate and upload seeds * Skip box2d * Make seeds smaller, and change representation of seeds in upload * Handle long seeds * Fix RandomAgent example to be deterministic * Handle integer types correctly in Python2 and Python3 * Try caching pip * Try adding swap * Add df and free calls * Bump swap * Bump swap size * Try setting overcommit * Try other sysctls * Try fixing overcommit * Try just setting overcommit_memory=1 * Add explanatory comment * Add what's new section to readme * BUG: Mark ElevatorAction-ram-v0 as non-deterministic for now * Document seed * Move nondetermistic check into spec
2016-05-29 09:07:09 -07:00
class UnseedableEnv(Error):
"""Raised when the user tries to seed an env that does not support
seeding.
"""
2021-07-29 02:26:34 +02:00
pass
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class DependencyNotInstalled(Error):
pass
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class UnsupportedMode(Exception):
"""Raised when the user requests a rendering mode not supported by the
environment.
"""
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
pass
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class ResetNeeded(Exception):
"""When the monitor is active, raised when the user tries to step an
environment that's already done.
"""
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
pass
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class ResetNotAllowed(Exception):
"""When the monitor is active, raised when the user tries to step an
environment that's not yet done.
"""
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
pass
2021-07-29 02:26:34 +02:00
class InvalidAction(Exception):
"""Raised when the user performs an action not contained within the
action space
"""
2021-07-29 02:26:34 +02:00
pass
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
# API errors
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class APIError(Error):
2021-07-29 02:26:34 +02:00
def __init__(
self,
message=None,
http_body=None,
http_status=None,
json_body=None,
headers=None,
):
super().__init__(message)
2016-04-27 08:00:58 -07:00
2021-07-29 02:26:34 +02:00
if http_body and hasattr(http_body, "decode"):
2016-04-27 08:00:58 -07:00
try:
2021-07-29 02:26:34 +02:00
http_body = http_body.decode("utf-8")
except Exception:
http_body = "<Could not decode body as utf-8.>"
2016-04-27 08:00:58 -07:00
self._message = message
self.http_body = http_body
self.http_status = http_status
self.json_body = json_body
self.headers = headers or {}
2021-07-29 02:26:34 +02:00
self.request_id = self.headers.get("request-id", None)
2016-04-27 08:00:58 -07:00
def __unicode__(self):
if self.request_id is not None:
msg = self._message or "<empty message>"
return f"Request {self.request_id}: {msg}"
2016-04-27 08:00:58 -07:00
else:
return self._message
def __str__(self):
2022-05-09 16:22:49 +01:00
return self.__unicode__()
2016-04-27 08:00:58 -07:00
class APIConnectionError(APIError):
pass
class InvalidRequestError(APIError):
2021-07-29 02:26:34 +02:00
def __init__(
self,
message,
param,
http_body=None,
http_status=None,
json_body=None,
headers=None,
):
super().__init__(message, http_body, http_status, json_body, headers)
2016-04-27 08:00:58 -07:00
self.param = param
class AuthenticationError(APIError):
pass
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class RateLimitError(APIError):
pass
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
# Video errors
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class VideoRecorderError(Error):
pass
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class InvalidFrame(Error):
pass
2021-07-29 02:26:34 +02:00
# Wrapper errors
2021-07-29 02:26:34 +02:00
class DoubleWrapperError(Error):
pass
class WrapAfterConfigureError(Error):
pass
2017-03-05 19:54:09 -08:00
class RetriesExceededError(Error):
pass
2021-07-29 02:26:34 +02:00
# Vectorized environments errors
2021-07-29 02:26:34 +02:00
class AlreadyPendingCallError(Exception):
"""
Raised when `reset`, or `step` is called asynchronously (e.g. with
`reset_async`, or `step_async` respectively), and `reset_async`, or
`step_async` (respectively) is called again (without a complete call to
`reset_wait`, or `step_wait` respectively).
"""
2021-07-29 02:26:34 +02:00
def __init__(self, message, name):
super().__init__(message)
self.name = name
2021-07-29 02:26:34 +02:00
class NoAsyncCallError(Exception):
"""
Raised when an asynchronous `reset`, or `step` is not running, but
`reset_wait`, or `step_wait` (respectively) is called.
"""
2021-07-29 02:26:34 +02:00
def __init__(self, message, name):
super().__init__(message)
self.name = name
2021-07-29 02:26:34 +02:00
class ClosedEnvironmentError(Exception):
"""
Trying to call `reset`, or `step`, while the environment is closed.
"""
2021-07-29 02:26:34 +02:00
pass
2021-07-29 02:26:34 +02:00
class CustomSpaceError(Exception):
"""
The space is a custom gym.Space instance, and is not supported by
`AsyncVectorEnv` with `shared_memory=True`.
"""
2021-07-29 02:26:34 +02:00
pass