simplex/helpers/api.moon
2018-05-01 15:41:08 -07:00

35 lines
690 B
Plaintext

import json_params, capture_errors, yield_error from require "lapis.application"
import insert, remove from table
import max from math
api_request = (fn) ->
json_params capture_errors {
fn,
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: { :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_request
:abort
:assert_model
}