@@ -39,7 +39,8 @@ scope in which we will capture errors. We do that using the `capture_errors`
3939helper. Then we can throw a raw error using ` yield_error ` .
4040
4141
42- ``` lua
42+ $dual_code{
43+ lua = [[
4344local lapis = require("lapis")
4445local 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!"
5354end))
54- ```
55-
56- ``` moon
55+ ]] ,
56+ moon = [[
5757import capture_errors, yield_error from require "lapis.application"
5858
5959class App extends lapis.Application
6060 "/do_something": capture_errors =>
6161 yield_error "something bad happened"
6262 "Hello!"
63- ```
63+ ]]
64+ }
6465
6566What happens when there is an error? The action will stop executing at the
6667first 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
7576a table: (note that <span class =" for_moon " >` @errors ` </span ><span
7677class="for_lua">` self.errors ` </span > is set before the custom handler)
7778
78- ``` lua
79+ $dual_code{
80+ lua = [[
7981app: 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 = [[
9495class 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
108110as the action.
109111
110112If 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 = [[
114117local lapis = require("lapis")
115118local app_helpers = require("lapis.application")
116119
@@ -121,15 +124,15 @@ local app = lapis.Application()
121124app: match ("/", capture_errors_json(function(self)
122125 yield_error("something bad happened")
123126end))
124- ```
125-
126- ``` moon
127+ ]] ,
128+ moon = [[
127129import capture_errors_json, yield_error from require "lapis.application"
128130
129131class App extends lapis.Application
130132 "/": capture_errors_json =>
131133 yield_error "something bad happened"
132- ```
134+ ]]
135+ }
133136
134137Would 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
194197idiom.
195198
196- ``` lua
199+ $dual_code{
200+ lua = [[
197201local lapis = require("lapis")
198202local 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
207211end))
208-
209- ```
210-
211- ``` moon
212+ ]] ,
213+ moon = [[
212214import capture_errors, assert_error from require "lapis.application"
213215
214216class App extends lapis.Application
215217 "/": capture_errors =>
216218 user = assert_error Users\find id: "leafo"
217219 "result: #{user.id}"
218- ```
220+ ]]
221+ }
219222
220223If you call this function not within a ` capture_errors ` context, then a hard
221224Lua ` error ` will be thrown.
0 commit comments