Errors Module | spectrumx.errors
Classes
spectrumx.errors.Result
Result(
*,
value: T | Unset = _unset,
exception: Exception | Unset = _unset,
error_info: dict[str, Any] | None = None,
)
Bases: Generic[T]
flowchart TD
spectrumx.errors.Result[Result]
click spectrumx.errors.Result href "" "spectrumx.errors.Result"
Either packs a value (success) or an exception (failure).
Useful when running several operations that might fail, but you'd like to handle failures later.
Notice the constructor will raise if any are true
- Both
valueandexceptionare provided. - Neither
valuenorexceptionare provided. valueis an instance ofException.exceptionis not an instance ofException.error_infois provided without anexception. The opposite is allowed.
Also, None is a valid value and will be considered a success.
READING a Result
There are several ways to use it. Assuming result is an instance of this class:
- By calling:
result()either returns the value or re-raises the exception. You can also callresult.unwrap()for the same effect. - By checking truthfulness:
bool(result) is Truemeans success. This can be used to filter them:failed_jobs = [result for result in all_jobs if not result]. - To access the value:
my_val = result.value_or(default_value). - Or the exception:
my_exc = result.exception_or(default_exception). - When failed, see
result.error_infoto get more information if provided.
CREATING a Result
def dangerous_fn(asset_id: str) -> int:
if random.random() < 0.1:
raise KnownError("Something went wrong")
return hash(asset_id)
def wrap_dangerous_fn(asset_id) -> Result[int]:
try:
return Result(value=dangerous_fn(asset_id))
except KnownError as e:
error_info = {
"reason": "It happens ...",
"asset_id": asset_id,
}
return Result(exception=e, error_info=error_info)
These examples will raise
Result() # empty result
Result(value=123, exception=RuntimeError()) # both value and exception
Result(value=123, error_info={}) # error_info without exception
Result(exception=123) # exception is not an Exception
Result(value=RuntimeError()) # value is an Exception
These are valid
Result(value=123) # success
Result(value=None) # success with None
Result(exception=RuntimeError()) # simple failure
Result(exception=RuntimeError(), error_info={}) # failure with extra info
Result(exception=RuntimeError(), error_info=True)
# allowed but not recommended (`error_info` is not a dict)
May your results be successful!
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T | Unset
|
The value to pack. |
_unset
|
exception
|
Exception | Unset
|
The exception to pack. |
_unset
|
error_info
|
dict[str, Any] | None
|
Additional information about the error. |
None
|
-
API Reference
SDS Client | spectrumx.client.Client
Client Functions
-
API Reference
Errors Module | spectrumx.errors
Functions
process_upload_results
Methods:
| Name | Description |
|---|---|
exception_or |
Returns the wrapped exception or a default one when result is a value. |
unwrap |
Alias for |
value_or |
Returns the wrapped value or a default value when result is an exception. |
Source code in spectrumx/errors.py
Functions
exception_or
Returns the wrapped exception or a default one when result is a value.
unwrap
value_or
Functions
spectrumx.errors.process_upload_results
process_upload_results(
upload_results: Sequence[Result],
*,
verbose: bool,
raise_on_error: bool,
) -> bool
Process the results of a batch upload, raising if any has failed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
upload_results
|
Sequence[Result]
|
A sequence of Result objects. |
required |
verbose
|
bool
|
Whether to log errors. |
required |
raise_on_error
|
bool
|
Whether to raise an exception on failure. |
required |
Raises: The first SDSError wrapped as upload_results if any upload has failed and raise_on_error is True. Returns: bool: True if all uploads were successful, False otherwise.
Source code in spectrumx/errors.py
Exceptions
spectrumx.errors.SDSError
Bases: Exception
flowchart TD
spectrumx.errors.SDSError[SDSError]
click spectrumx.errors.SDSError href "" "spectrumx.errors.SDSError"
Base class for all SDS errors.
spectrumx.errors.AuthError
Bases: SDSError
flowchart TD
spectrumx.errors.AuthError[AuthError]
spectrumx.errors.SDSError[SDSError]
spectrumx.errors.SDSError --> spectrumx.errors.AuthError
click spectrumx.errors.AuthError href "" "spectrumx.errors.AuthError"
click spectrumx.errors.SDSError href "" "spectrumx.errors.SDSError"
Issue with user authentication against SDS.
spectrumx.errors.NetworkError
Bases: SDSError
flowchart TD
spectrumx.errors.NetworkError[NetworkError]
spectrumx.errors.SDSError[SDSError]
spectrumx.errors.SDSError --> spectrumx.errors.NetworkError
click spectrumx.errors.NetworkError href "" "spectrumx.errors.NetworkError"
click spectrumx.errors.SDSError href "" "spectrumx.errors.SDSError"
Issue with the client connection to the SDS service.
spectrumx.errors.ServiceError
Bases: SDSError
flowchart TD
spectrumx.errors.ServiceError[ServiceError]
spectrumx.errors.SDSError[SDSError]
spectrumx.errors.SDSError --> spectrumx.errors.ServiceError
click spectrumx.errors.ServiceError href "" "spectrumx.errors.ServiceError"
click spectrumx.errors.SDSError href "" "spectrumx.errors.SDSError"
Issue with the SDS service.
spectrumx.errors.FileError
Bases: SDSError
flowchart TD
spectrumx.errors.FileError[FileError]
spectrumx.errors.SDSError[SDSError]
spectrumx.errors.SDSError --> spectrumx.errors.FileError
click spectrumx.errors.FileError href "" "spectrumx.errors.FileError"
click spectrumx.errors.SDSError href "" "spectrumx.errors.SDSError"
Issue interacting with a file in SDS.
spectrumx.errors.CaptureError
Bases: SDSError
flowchart TD
spectrumx.errors.CaptureError[CaptureError]
spectrumx.errors.SDSError[SDSError]
spectrumx.errors.SDSError --> spectrumx.errors.CaptureError
click spectrumx.errors.CaptureError href "" "spectrumx.errors.CaptureError"
click spectrumx.errors.SDSError href "" "spectrumx.errors.SDSError"
Issue interacting with a capture in SDS.
Methods:
| Name | Description |
|---|---|
extract_existing_capture_uuid |
Extract existing capture UUID from error message |
Functions
extract_existing_capture_uuid
Extract existing capture UUID from error message if this is a duplicate capture error.
Returns:
| Type | Description |
|---|---|
str | None
|
The UUID of the existing capture if found, None otherwise. |
Source code in spectrumx/errors.py
spectrumx.errors.DatasetError
Bases: SDSError
flowchart TD
spectrumx.errors.DatasetError[DatasetError]
spectrumx.errors.SDSError[SDSError]
spectrumx.errors.SDSError --> spectrumx.errors.DatasetError
click spectrumx.errors.DatasetError href "" "spectrumx.errors.DatasetError"
click spectrumx.errors.SDSError href "" "spectrumx.errors.SDSError"
Issue interacting with a dataset in SDS.
spectrumx.errors.ExperimentError
Bases: SDSError
flowchart TD
spectrumx.errors.ExperimentError[ExperimentError]
spectrumx.errors.SDSError[SDSError]
spectrumx.errors.SDSError --> spectrumx.errors.ExperimentError
click spectrumx.errors.ExperimentError href "" "spectrumx.errors.ExperimentError"
click spectrumx.errors.SDSError href "" "spectrumx.errors.SDSError"
Issue interacting with an experiment in SDS.