dt0/helpers.moon

37 lines
752 B
Plaintext
Raw Normal View History

2018-09-09 23:29:56 +00:00
import json_params, capture_errors, yield_error, respond_to from require "lapis.application"
import insert from table
import max from math
api = (tab) ->
json_params capture_errors {
2018-09-10 04:51:39 +00:00
=>
result = respond_to(tab)
return json: result,
2018-09-09 23:29:56 +00:00
on_error: =>
status = 400
errors = {}
for err in *@errors
if "table" == type err
status = max status, err[1]
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
}