Skip to content

Commit eea7b36

Browse files
committed
Adding new Match option to Result object
1 parent b9fe50b commit eea7b36

File tree

5 files changed

+131
-372
lines changed

5 files changed

+131
-372
lines changed

.github/workflows/release-vscodetoolkit.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,6 @@ jobs:
4747
run: npx vsce package --out ResultR.VSCodeToolkit.${{ steps.version.outputs.VERSION }}.vsix
4848
working-directory: src/ResultR.VSCodeToolkit
4949

50-
- name: Publish to VS Code Marketplace
51-
run: npx vsce publish --pat ${{ secrets.VSCODE_MARKETPLACE_TOKEN }}
52-
working-directory: src/ResultR.VSCodeToolkit
53-
54-
- name: Publish to Open VSX Registry
55-
run: npx ovsx publish ResultR.VSCodeToolkit.${{ steps.version.outputs.VERSION }}.vsix --pat ${{ secrets.OPEN_VSX_TOKEN }}
56-
working-directory: src/ResultR.VSCodeToolkit
57-
continue-on-error: true
58-
5950
- name: Create GitHub Release
6051
uses: softprops/action-gh-release@v2
6152
with:

.github/workflows/release-vstoolkit.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ jobs:
5757
}
5858
id: rename
5959

60-
- name: Publish to Visual Studio Marketplace
61-
uses: CodingWithCalvin/GHA-VSMarketplacePublisher@v2.0.0
62-
with:
63-
marketplace-pat: ${{ secrets.VS_MARKETPLACE_TOKEN }}
64-
publish-manifest-path: src/ResultR.VSToolkit/Resources/publishManifest.json
65-
vsix-path: ${{ steps.rename.outputs.VSIX_PATH }}
66-
6760
- name: Create GitHub Release
6861
uses: softprops/action-gh-release@v2
6962
with:

docs/wiki/The-Result-Type.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,89 @@ public async Task<IActionResult> CreateUser(CreateUserRequest request)
167167
}
168168
```
169169

170+
### Pattern Matching with Match()
171+
172+
The `Match()` method provides a functional approach to handling results by executing one of two callbacks based on success or failure:
173+
174+
```csharp
175+
[HttpGet("{id}")]
176+
public async Task<IActionResult> GetUser(int id)
177+
{
178+
var result = await _dispatcher.Dispatch(new GetUserRequest(id));
179+
180+
return result.Match(
181+
user => Ok(user),
182+
error => NotFound(new { error })
183+
);
184+
}
185+
186+
[HttpPost]
187+
public async Task<IActionResult> CreateUser(CreateUserRequest request)
188+
{
189+
var result = await _dispatcher.Dispatch(request);
190+
191+
return result.Match(
192+
user => CreatedAtAction(nameof(GetUser), new { id = user.Id }, user),
193+
error => BadRequest(new { error })
194+
);
195+
}
196+
```
197+
198+
#### Accessing Exception Information
199+
200+
Use the overload that accepts both error message and exception when you need detailed error information:
201+
202+
```csharp
203+
[HttpGet("{id}")]
204+
public async Task<IActionResult> GetUser(int id)
205+
{
206+
var result = await _dispatcher.Dispatch(new GetUserRequest(id));
207+
208+
return result.Match(
209+
user => Ok(user),
210+
(error, exception) =>
211+
{
212+
if (exception is not null)
213+
{
214+
_logger.LogError(exception, "Failed to get user {UserId}", id);
215+
}
216+
return NotFound(new { error, hasException = exception is not null });
217+
}
218+
);
219+
}
220+
```
221+
222+
#### Non-Generic Result
223+
224+
The `Match()` method is also available on the non-generic `Result` type:
225+
226+
```csharp
227+
public async Task<IActionResult> DeleteUser(int id)
228+
{
229+
var result = await _dispatcher.Dispatch(new DeleteUserRequest(id));
230+
231+
return result.Match(
232+
() => NoContent(),
233+
error => NotFound(new { error })
234+
);
235+
}
236+
237+
// With exception handling
238+
public async Task<IActionResult> DeleteUser(int id)
239+
{
240+
var result = await _dispatcher.Dispatch(new DeleteUserRequest(id));
241+
242+
return result.Match(
243+
() => NoContent(),
244+
(error, exception) =>
245+
{
246+
_logger.LogError(exception, "Failed to delete user {UserId}", id);
247+
return StatusCode(500, new { error });
248+
}
249+
);
250+
}
251+
```
252+
170253
## Exception Handling
171254

172255
ResultR automatically catches exceptions in handlers and converts them to failure results:

0 commit comments

Comments
 (0)