123456789101112131415161718192021222324252627282930313233343536 |
- import json_params, capture_errors, yield_error, respond_to from require "lapis.application"
- import insert from table
- import max from math
-
- api = (fn) =>
- json_params capture_errors {
- =>
- result = fn(@)
- return json: result
- on_error: =>
- status = 400 -- most likely a bad request
- errors = {}
- for err in *@errors
- if "table" == type err
- status = max status, err[1] -- the worst error will have a higher status number
- 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
- }
|