Skip to content

Commit dca0701

Browse files
committed
update error handling docs to use dual_code
1 parent d4750d9 commit dca0701

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

docs/exception_handling.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ scope in which we will capture errors. We do that using the `capture_errors`
3939
helper. Then we can throw a raw error using `yield_error`.
4040

4141

42-
```lua
42+
$dual_code{
43+
lua = [[
4344
local lapis = require("lapis")
4445
local app_helpers = require("lapis.application")
4546

@@ -51,16 +52,16 @@ app:match("/do_something", capture_errors(function(self)
5152
yield_error("something bad happened")
5253
return "Hello!"
5354
end))
54-
```
55-
56-
```moon
55+
]],
56+
moon = [[
5757
import capture_errors, yield_error from require "lapis.application"
5858

5959
class App extends lapis.Application
6060
"/do_something": capture_errors =>
6161
yield_error "something bad happened"
6262
"Hello!"
63-
```
63+
]]
64+
}
6465

6566
What happens when there is an error? The action will stop executing at the
6667
first error, and then the error handler is run. The default error handler will
@@ -75,7 +76,8 @@ If you want to have a custom error handler you can invoke `capture_errors` with
7576
a table: (note that <span class="for_moon">`@errors`</span><span
7677
class="for_lua">`self.errors`</span> is set before the custom handler)
7778

78-
```lua
79+
$dual_code{
80+
lua = [[
7981
app:match("/do_something", capture_errors({
8082
on_error = function(self)
8183
log_errors(self.errors) -- you would supply the log_errors function
@@ -88,9 +90,8 @@ app:match("/do_something", capture_errors({
8890
return { render = true }
8991
end
9092
}))
91-
```
92-
93-
```moon
93+
]],
94+
moon = [[
9495
class App extends lapis.Application
9596
"/do_something": capture_errors {
9697
on_error: =>
@@ -102,15 +103,17 @@ class App extends lapis.Application
102103
yield_error "something bad happened"
103104
render: true
104105
}
105-
```
106+
]]
107+
}
106108

107109
`capture_errors` when called with a table will use the first positional value
108110
as the action.
109111

110112
If you're building a JSON API then another method is provided,
111113
`capture_errors_json`, which renders the errors in a JSON object like so:
112114

113-
```lua
115+
$dual_code{
116+
lua = [[
114117
local lapis = require("lapis")
115118
local app_helpers = require("lapis.application")
116119

@@ -121,15 +124,15 @@ local app = lapis.Application()
121124
app:match("/", capture_errors_json(function(self)
122125
yield_error("something bad happened")
123126
end))
124-
```
125-
126-
```moon
127+
]],
128+
moon = [[
127129
import capture_errors_json, yield_error from require "lapis.application"
128130

129131
class App extends lapis.Application
130132
"/": capture_errors_json =>
131133
yield_error "something bad happened"
132-
```
134+
]]
135+
}
133136

134137
Would render (with the correct content type):
135138

@@ -193,7 +196,8 @@ error, otherwise all the arguments are returned from the function unchanged.
193196
`assert_error` is very handy with database methods, which make use of this
194197
idiom.
195198

196-
```lua
199+
$dual_code{
200+
lua = [[
197201
local lapis = require("lapis")
198202
local app_helpers = require("lapis.application")
199203

@@ -205,17 +209,16 @@ app:match("/", capture_errors(function(self)
205209
local user = assert_error(Users:find({id = "leafo"}))
206210
return "result: " .. user.id
207211
end))
208-
209-
```
210-
211-
```moon
212+
]],
213+
moon = [[
212214
import capture_errors, assert_error from require "lapis.application"
213215

214216
class App extends lapis.Application
215217
"/": capture_errors =>
216218
user = assert_error Users\find id: "leafo"
217219
"result: #{user.id}"
218-
```
220+
]]
221+
}
219222

220223
If you call this function not within a `capture_errors` context, then a hard
221224
Lua `error` will be thrown.

0 commit comments

Comments
 (0)