better error handling

This commit is contained in:
Paul Liverman III 2018-04-24 17:55:38 -07:00
parent 796cb344bf
commit dbfa8280b2
2 changed files with 35 additions and 23 deletions

View File

@ -1,10 +1,9 @@
import Application from require "lapis"
import yield_error from require "lapis.application"
import assert_valid from require "lapis.validate"
import APIKeys, Users, Tasks from require "models"
import locate from require "locator"
import api_request from locate "helpers.api"
import api_request, abort from locate "helpers.api"
-- import random from locate "calc"
-- import escape_similar_to from locate "db"
@ -14,11 +13,11 @@ class API extends Application
@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!!!
abort "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
abort "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!)
abort "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,7 +36,7 @@ class API extends Application
user_id: @user.id
content: @params.content
}
yield_error 500, err unless task
abort 500, err unless task
return json: { success: true, :task }
@ -55,11 +54,11 @@ class API extends Application
}
task = Tasks\find content: @params.content, user_id: @user.id
else
yield_error "Task id or content not specified."
abort "Task id or content not specified."
yield_error 404, "Invalid task specified." unless task
abort 404, "Invalid task specified." unless task
task, err = task\update done: true
yield_error 500, err unless task
abort 500, err unless task
return json: { success: true, :task }
@ -77,17 +76,17 @@ class API extends Application
}
task = Tasks\find content: @params.content, user_id: @user.id
else
yield_error "Task id or content not specified."
abort "Task id or content not specified."
yield_error 404, "Invalid task specified." unless task
abort 404, "Invalid task specified." unless task
task, err = task\update done: false
yield_error 500, err unless task
abort 500, err unless task
return json: { success: true, :task }
[get: "/get"]: api_request =>
-- TODO
yield_error 501, "Not implemented."
abort 501, "Not implemented."
[random: "/random"]: api_request =>
assert_valid @params, {
@ -97,7 +96,7 @@ class API extends Application
@params.count or= 1
@params.done = false if @params.done == nil
yield_error 501, "Not implemented."
abort 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
@ -114,7 +113,7 @@ class API extends Application
-- if tasks and #tasks == 1
-- return json: { success: true, task: tasks[1] }
-- else
-- yield_error!
-- abort!
[list: "/list"]: api_request =>
assert_valid @params, {
@ -126,7 +125,7 @@ class API extends Application
@params.count or= 50
@params.page or= 1
@params.order or= "asc"
yield_error "Invalid page. (Must be a positive integer.)" if @params.page < 1
abort "Invalid page. (Must be a positive integer.)" if @params.page < 1
local Paginator
if @params.done == nil
@ -144,11 +143,11 @@ class API extends Application
[new_key: "/key/new"]: api_request =>
-- TODO
yield_error 501, "Not implemented."
abort 501, "Not implemented."
[delete_key: "/key/delete"]: api_request =>
-- TODO
yield_error 501, "Not implemented."
abort 501, "Not implemented."
-- /new { content: "string" }
-- /do { id: # } or content

View File

@ -1,16 +1,29 @@
import json_params, capture_errors from require "lapis.application"
import remove from table
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
if "number" == type @errors[1]
status = remove(@errors, 1)
return(:status, json: { errors: @errors })
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
{
:api_request
:abort
}