simplex/helpers/api.moon

35 lines
706 B
Plaintext
Raw Normal View History

2018-04-25 00:55:38 +00:00
import json_params, capture_errors, yield_error from require "lapis.application"
import insert, remove from table
import max from math
2018-04-25 00:28:23 +00:00
api_request = (fn) ->
json_params capture_errors {
fn,
on_error: =>
status = 400
2018-04-25 00:55:38 +00:00
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 })
2018-04-25 00:28:23 +00:00
}
2018-04-25 00:55:38 +00:00
abort = (status, message) ->
if message
yield_error {status, message}
else
yield_error status
2018-05-01 22:41:08 +00:00
assert_model = (result, err) ->
abort 500, err if err
return result
2018-04-25 00:28:23 +00:00
{
:api_request
2018-04-25 00:55:38 +00:00
:abort
2018-05-01 22:41:08 +00:00
:assert_model
2018-04-25 00:28:23 +00:00
}