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
Copy file name to clipboardExpand all lines: docs/guide/toolkit/results.md
+16-6Lines changed: 16 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -125,12 +125,12 @@ Alternatively, you can provide the `failed()` method with an error object or a l
125
125
126
126
The error object provided by this package has the following properties:
127
127
128
+
-`code`: an enum that identifies the error and can be used to programmatically detect and handle specific errors. It
129
+
is intentionally an enum, because to programmatically detect specific errors, the codes need to be a defined list of
130
+
values.
128
131
-`message`: a string message that describes the error.
129
-
-`code`: a backed enum that identifies error and can be used to programmatically detect and handle specific errors. It
130
-
is intentionally an enum, because if you need to detect specific errors, the codes need to be a defined list of
131
-
values - which is an enum.
132
-
-`key`: optionally, a key for the error. This can be used to group errors by keys, for example to group errors by
133
-
properties that exist on a command message.
132
+
-`key`: optionally, a key for the error. Use this to group errors by keys, for example to group errors by
133
+
properties that exist on a command message. The key can be a string or enum.
134
134
135
135
Error objects _must_ be instantiated with either a code or a message. They are immutable, so you cannot change their
136
136
properties after they have been created.
@@ -181,7 +181,7 @@ The error list class is iterable, countable and has `isEmpty()` and `isNotEmpty(
181
181
### Keyed Error Sets
182
182
183
183
If you are using the `key` property on error objects, we provide a keyed set of errors class that groups errors by their
184
-
key.
184
+
key. Keys can be strings or enums.
185
185
186
186
To create one, provide error objects to its constructor:
187
187
@@ -243,6 +243,16 @@ if ($errors->contains(CancelAttendeeTicketError::AlreadyCancelled)) {
243
243
}
244
244
```
245
245
246
+
If you only want to handle one error code at once, use the `code()` method on the errors list. This will return the first error code, or `null` if there are no error codes.
247
+
248
+
```php
249
+
$explanation = match ($errors->code()) {
250
+
CancelAttendeeTicketError::AlreadyCancelled => 'The ticket has already been cancelled.',
251
+
CancelAttendeeTicketError::NotFound => 'The ticket was not found.',
252
+
default => null,
253
+
};
254
+
```
255
+
246
256
## Exception
247
257
248
258
We provide a `FailedResultException` that you can use to throw a result object:
0 commit comments