You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
then rename the file `ResultContainer/__init__.py` to `ResultContainer/ResultContainer.py` and move `ResultContainer.py` to wherever you want to use it.
33
33
34
+
35
+
34
36
## Variants
35
37
36
38
```python
37
39
# Result is the main class and Ok and Err are constructors.
38
-
from ResultContainer import Result, Ok, Err
40
+
from ResultContainer import Result, Ok, Err, ResultErr
39
41
```
40
42
41
43
-`Ok(value)`
42
-
-`value` is wrapped within an `Ok`.
44
+
-`value` is any object to be wrapped within an `Ok`.
43
45
- Constructor: `Result.as_Ok(value)`
44
46
-`Result.Ok` attribute returns the wrapped `value`
45
47
- Can never wrap a `ResultErr` instance (it will just be converted to an `Err(value)`).
46
48
47
49
-`Err(e)`
48
-
-`e` is wrapped within an `Err`, and `type(e) is ResultErr`.
50
+
-`e` is any object to be wrapped within an `Err`.
51
+
-`e` is stored as `ResultErr` Exception object.
52
+
- If `not isinstance(e, ResultErr)`, then `e = ResultErr(e)`.
49
53
- Constructor: `Result.as_Err(error_msg)`
50
54
-`Result.Err` attribute returns the wrapped `e`.
51
55
52
56
### Properties of the `Result` Variants
53
57
54
58
#### `Err(e)`:
55
59
56
-
- Represents a failure (error-state) and contains `e` as a `ResultErr` object that stores error messages and traceback information.
60
+
- Represents a failure (error-state).
61
+
-`e` is a `ResultErr` object that stores error messages and traceback information.
57
62
58
-
- Can be initialized with `Err(error_msg)`
63
+
- If `e` is another type, it is converted to a `ResultErr`.
64
+
That is, given `Err(e)` and `not isinstance(e, ResultErr)` becomes `Err( ResultErr(e) )`.
65
+
66
+
- Can be initialized with `Err(error_msg)`, where `error_msg` can be any object (typically a str)
59
67
-`Err(e)` ➥ syntactic-sugar for ➥ `Result.as_Err(e)`
60
68
61
69
- If an `Ok(value)` operation fails, then it is converted to an `Err(e)`, where `e` stores the error message.
@@ -94,14 +102,79 @@ from ResultContainer import Result, Ok, Err
94
102
-`Err(e1) < Err(e2) ` ➣ `False`
95
103
-`Err(e1) <= Err(e2) ` ➣ `True`
96
104
97
-
## Initialization
105
+
106
+
107
+
## ResultErr Class
108
+
109
+
The `ResultErr` class is a custom exception class for error handling in the `Result` object. The `ResultErr` class captures error messages and optional traceback information. Its primary use is for identifying when a `Result` instance is an `Err` variant, which is handled automatically. It should not be necessary to use the ResultErr class directly, but select attributes and methods are presented here for background information.
110
+
111
+
### Initialization
112
+
113
+
```python
114
+
# All arguments are optional
115
+
from ResultContainer import ResultErr
116
+
117
+
# Main object signature:
118
+
e = ResultErr(msg="", add_traceback=True, max_messages=20)
119
+
# msg (Any, optional): Error message(s) to initialize with.
120
+
# `str(msg)` is the message that is stored.
121
+
# If msg is a Sequence, then each item in the Sequence is
122
+
# appended as str(item) to the error messages.
123
+
# Default is "", to disable error status.
124
+
# add_traceback (bool, optional): If True, then traceback information is added to the message.
125
+
# max_messages (int, optional): The maximum number of error messages to store.
126
+
# After this, all additional messages are ignored. Default is 20.
127
+
```
128
+
129
+
### Attributes and Methods
130
+
131
+
These are select attributes and methods built into `Result` object.
132
+
133
+
#### Attributes
134
+
135
+
```
136
+
size (int): Returns the number of error messages.
137
+
138
+
is_Ok (bool): Returns False if in error status (ie, size == 0).
139
+
is_Err (bool): Returns True if in error status (ie, size > 0).
140
+
141
+
Err_msg (list[str]): List of the error messages that have been added.
142
+
Err_traceback (list[list[str]]): List of lists that contains the traceback information for each error message.
143
+
```
144
+
145
+
#### Methods
146
+
147
+
```
148
+
append(msg, add_traceback=True):
149
+
Append an error message to the instance.
150
+
151
+
raises(add_traceback=False, error_msg=""):
152
+
Raise a ResultErr exception if `size > 0`.
153
+
`error_msg` is an optional note to append to the ResultErr.
0 commit comments