new error handling :D

This commit is contained in:
Paul Liverman III 2018-04-24 17:37:52 -07:00
parent 7fb42921b1
commit 7f7af41bf5

View File

@ -1,5 +1,5 @@
import Application from require "lapis"
import json_params, capture_errors_json, yield_error from require "lapis.application"
import yield_error from require "lapis.application"
import assert_valid from require "lapis.validate"
import APIKeys, Users, Tasks from require "models"
@ -12,13 +12,13 @@ class API extends Application
@path: "/v1"
@name: "api_"
@before_filter( capture_errors_json json_params =>
@before_filter( api_request =>
-- TODO implement Authorization: api_key VALUE as acceptable method to send api_key
yield_error "api_key not specified!" unless @params.api_key -- this does not seem to be triggering!!!
yield_error "api_key not specified." unless @params.api_key -- this does not seem to be triggering!!!
@api_key = APIKeys\find key: @params.api_key
yield_error "Invalid api_key" unless @api_key
@user = Users\find id: @api_key.user_id
yield_error "Invalid api_key!" unless @user -- NOTE this should also delete the api_key and error (this should never happen!)
yield_error "Invalid api_key." unless @user -- NOTE this should also delete the api_key and error (this should never happen!)
)
-- TODO intentionally cause an error to see if this is working as intended
@ -37,11 +37,11 @@ class API extends Application
user_id: @user.id
content: @params.content
}
yield_error err unless task
yield_error 500, err unless task
return json: { success: true, :task }
[do: "/do"]: capture_errors_json json_params =>
[do: "/do"]: api_request =>
local task
if @params.id
assert_valid @params, {
@ -57,13 +57,13 @@ class API extends Application
else
yield_error "Task id or content not specified."
yield_error "Invalid task specified." unless task
yield_error 404, "Invalid task specified." unless task
task, err = task\update done: true
yield_error err unless task
yield_error 500, err unless task
return json: { success: true, :task }
[undo: "/undo"]: capture_errors_json json_params =>
[undo: "/undo"]: api_request =>
local task
if @params.id
assert_valid @params, {
@ -79,17 +79,17 @@ class API extends Application
else
yield_error "Task id or content not specified."
yield_error "Invalid task specified." unless task
yield_error 404, "Invalid task specified." unless task
task, err = task\update done: false
yield_error err unless task
yield_error 500, err unless task
return json: { success: true, :task }
[get: "/get"]: capture_errors_json json_params =>
[get: "/get"]: api_request =>
-- TODO
return status: 501, json: { errors: {"Not implemented."} }
yield_error 501, "Not implemented."
[random: "/random"]: capture_errors_json json_params =>
[random: "/random"]: api_request =>
assert_valid @params, {
{"count", exists: true, is_integer: true, optional: true, "Count is not an integer."}
{"done", exists: true, one_of: {true, false}, optional: true, "Done is not a boolean."}
@ -97,6 +97,8 @@ class API extends Application
@params.count or= 1
@params.done = false if @params.done == nil
yield_error 501, "Not implemented."
-- TODO figure out how to return random selection
-- possibly need to store how many items each user has and use a different strategy for users with low amounts vs high amounts
-- key = get_key(@)
@ -112,10 +114,9 @@ class API extends Application
-- if tasks and #tasks == 1
-- return json: { success: true, task: tasks[1] }
-- else
-- return standard_err!
return status: 501, json: { errors: {"Not implemented."} }
-- yield_error!
[list: "/list"]: capture_errors_json json_params =>
[list: "/list"]: api_request =>
assert_valid @params, {
{"count", exists: true, is_integer: true, optional: true, "Count is not an integer."}
{"done", exists: true, one_of: {true, false}, optional: true, "Done is not a boolean."}
@ -141,13 +142,13 @@ class API extends Application
-- returns page in case it returned a different page than you asked for! (in the case you ask for a page beyond the end)
return json: { success: true, page: @params.page, :tasks }
[new_key: "/key/new"]: capture_errors_json json_params =>
[new_key: "/key/new"]: api_request =>
-- TODO
return status: 501, json: { errors: {"Not implemented."} }
yield_error 501, "Not implemented."
[delete_key: "/key/delete"]: capture_errors_json json_params =>
[delete_key: "/key/delete"]: api_request =>
-- TODO
return status: 501, json: { errors: {"Not implemented."} }
yield_error 501, "Not implemented."
-- /new { content: "string" }
-- /do { id: # } or content