Skip to content

Commit 928975e

Browse files
mpscholtenclaude
andcommitted
Update documentation for new response handling
Remove references to ResponseException which is no longer used. Document earlyReturn for conditional early exits. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent eddc0cd commit 928975e

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

Guide/controller.markdown

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,19 +398,26 @@ action ExampleAction = do
398398

399399
## Action Execution
400400

401-
When calling a function to send the response, IHP will stop executing the action. Internally this is implemented by throwing and catching a [`ResponseException`](https://ihp.digitallyinduced.com/api-docs/src/IHP.ControllerSupport.html#ResponseException). Any code after e.g. a `render SomeView { .. }` call will not be called. This also applies to all redirect helpers.
401+
When calling a function to send the response like `render` or `redirectTo`, the response is sent to the client and the action returns. Since these functions return `IO ResponseReceived`, any code after them would be unreachable.
402402

403-
Here is an example of this behavior:
403+
Here is an example:
404404

405405
```haskell
406406
action ExampleAction = do
407407
redirectTo SomeOtherAction
408-
putStrLn "This line here is not reachable"
408+
-- Any code here would be unreachable since redirectTo returns the response
409409
```
410410

411-
The [`putStrLn`](https://ihp.digitallyinduced.com/api-docs/IHP-Prelude.html#v:putStrLn) will never be called because the [`redirectTo`](https://ihp.digitallyinduced.com/api-docs/IHP-Controller-Redirect.html#v:redirectTo) already stops execution.
411+
For conditional early exits (like access control), use `earlyReturn`:
412412

413-
When you have created a [`Response`](https://hackage.haskell.org/package/wai-3.2.2.1/docs/Network-Wai.html#t:Response) manually, you can use [`respondAndExit`](https://ihp.digitallyinduced.com/api-docs/src/IHP.ControllerSupport.html#respondAndExit) to send your response and stop action execution.
413+
```haskell
414+
action ExampleAction = do
415+
when (not loggedIn) do
416+
earlyReturn (redirectTo LoginAction)
417+
418+
-- This code runs only if loggedIn is True
419+
render MyView
420+
```
414421

415422
## Controller Context
416423

Guide/database.markdown

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -822,16 +822,7 @@ incomplete data is left in the database when there's an error.
822822
823823
The [`withTransaction`](https://ihp.digitallyinduced.com/api-docs/IHP-ModelSupport.html#v:withTransaction) function will automatically commit after it succesfully executed the passed do-block. When any exception is thrown, it will automatically rollback.
824824
825-
Keep in mind that some IHP functions like [`redirectTo`](https://ihp.digitallyinduced.com/api-docs/IHP-Controller-Redirect.html#v:redirectTo) or [`render`](https://ihp.digitallyinduced.com/api-docs/IHP-Controller-Render.html#v:render) throw a [`ResponseException`](https://ihp.digitallyinduced.com/api-docs/IHP-ControllerSupport.html#t:ResponseException). So code like below will not work as expected:
826-
827-
```haskell
828-
action CreateUserAction = do
829-
withTransaction do
830-
user <- newRecord @User |> createRecord
831-
redirectTo NewSessionAction
832-
```
833-
834-
The [`redirectTo`](https://ihp.digitallyinduced.com/api-docs/IHP-Controller-Redirect.html#v:redirectTo) throws a [`ResponseException`](https://ihp.digitallyinduced.com/api-docs/IHP-ControllerSupport.html#t:ResponseException) and will cause a rollback. This code should be structured like this:
825+
It's good practice to keep your transaction blocks focused on database operations only:
835826
836827
```haskell
837828
action CreateUserAction = do

0 commit comments

Comments
 (0)