passwd0/helpers.moon

37 lines
821 B
Plaintext
Raw Normal View History

2018-09-03 17:38:10 +00:00
import json_params, capture_errors, yield_error, respond_to from require "lapis.application"
import insert from table
import max from math
2018-09-16 19:04:52 +00:00
api = (fn) =>
2018-09-03 17:38:10 +00:00
json_params capture_errors {
2018-09-10 04:43:21 +00:00
=>
2018-09-16 19:04:52 +00:00
result = fn(@)
return json: result
2018-09-03 17:38:10 +00:00
on_error: =>
2018-09-16 19:04:52 +00:00
status = 400 -- most likely a bad request
2018-09-03 17:38:10 +00:00
errors = {}
for err in *@errors
if "table" == type err
2018-09-16 19:04:52 +00:00
status = max status, err[1] -- the worst error will have a higher status number
2018-09-03 17:38:10 +00:00
insert errors, err[2]
else
insert errors, err
return(:status, json: { success: false, :errors })
}
abort = (status, message) ->
if message
yield_error {status, message}
else
yield_error status
assert_model = (result, err) ->
abort 500, err if err
return result
{
:api
:abort
:assert_model
}