Created timestamper_dev module, logging.
This commit is contained in:
parent
b59b628cb5
commit
1b981b206c
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
start() ->
|
start() ->
|
||||||
ok = application:load(mnesia),
|
ok = application:load(mnesia),
|
||||||
|
ok = application:set_env(mnesia, dir, "/usr/lib/yaws/mnesia"),
|
||||||
ok = mnesia:start(),
|
ok = mnesia:start(),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
17
src/timestamper_dev.erl
Normal file
17
src/timestamper_dev.erl
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
-module(timestamper_dev).
|
||||||
|
-export([start/0, create_tables/1]).
|
||||||
|
|
||||||
|
start() ->
|
||||||
|
ok = application:load(mnesia),
|
||||||
|
ok = application:set_env(mnesia, dir, "/usr/lib/yaws"),
|
||||||
|
ok = mnesia:start(),
|
||||||
|
error_logger:info_report("TimeStampter app started."),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
create_tables(Nodes) ->
|
||||||
|
TableOpts = [{disc_copies, Nodes}],
|
||||||
|
{atomic, ok} = id_counter:create_table(TableOpts),
|
||||||
|
{atomic, ok} = ts_user:create_table(TableOpts),
|
||||||
|
{atomic, ok} = ts_timeline:create_table(TableOpts),
|
||||||
|
{atomic, ok} = ts_entry:create_table(TableOpts),
|
||||||
|
ok.
|
@ -21,8 +21,8 @@ out(YArg) ->
|
|||||||
case catch dispatch_request(YArg, Session, PathElements) of
|
case catch dispatch_request(YArg, Session, PathElements) of
|
||||||
{'EXIT', Err} ->
|
{'EXIT', Err} ->
|
||||||
% TODO: log error internally
|
% TODO: log error internally
|
||||||
io:format("~p", [Err]),
|
error_logger:error_report("TimeStamper: ~p", [Err]),
|
||||||
make_json_500(YArg);
|
make_json_500(YArg, Err);
|
||||||
Other -> Other
|
Other -> Other
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@ -306,7 +306,9 @@ put_timeline(YArg, Username, TimelineId) ->
|
|||||||
|
|
||||||
{content, "application/json", JSONResponse};
|
{content, "application/json", JSONResponse};
|
||||||
|
|
||||||
_Error -> make_json_500(YArg)
|
Error ->
|
||||||
|
error_logger:error_report("Unable to create a new timeline: ~p", [Error]),
|
||||||
|
make_json_500(YArg, Error)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
post_timeline(YArg, Username, TimelineId) ->
|
post_timeline(YArg, Username, TimelineId) ->
|
||||||
@ -333,7 +335,9 @@ post_timeline(YArg, Username, TimelineId) ->
|
|||||||
[{status, "no such timeline"},
|
[{status, "no such timeline"},
|
||||||
{see_docs, "/ts_api_doc/timelines.html#POST"}]);
|
{see_docs, "/ts_api_doc/timelines.html#POST"}]);
|
||||||
|
|
||||||
_Error -> make_json_500(YArg)
|
Error ->
|
||||||
|
error_logger:error_report("Unable update timeline: ~p", [Error]),
|
||||||
|
make_json_500(YArg, Error)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
delete_timeline(_YArg, _Username, _TimelineId) -> {status, 405}.
|
delete_timeline(_YArg, _Username, _TimelineId) -> {status, 405}.
|
||||||
@ -458,8 +462,8 @@ put_entry(YArg, Username, TimelineId) ->
|
|||||||
{content, "application/json", JSONResponse};
|
{content, "application/json", JSONResponse};
|
||||||
|
|
||||||
OtherError ->
|
OtherError ->
|
||||||
io:format("Could not create entry: ~p", [OtherError]),
|
error_logger:error_report("TimeStamper: Could not create entry: ~p", [OtherError]),
|
||||||
make_json_500(YArg)
|
make_json_500(YArg, OtherError)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
post_entry(YArg, Username, TimelineId, EntryId) ->
|
post_entry(YArg, Username, TimelineId, EntryId) ->
|
||||||
@ -479,7 +483,9 @@ post_entry(YArg, Username, TimelineId, EntryId) ->
|
|||||||
no_record -> make_json_404(YArg,
|
no_record -> make_json_404(YArg,
|
||||||
[{status, "no such entry"}, {see_docs, "/ts_api_doc/entries.html#POST"}]);
|
[{status, "no such entry"}, {see_docs, "/ts_api_doc/entries.html#POST"}]);
|
||||||
|
|
||||||
_Error -> make_json_500(YArg)
|
Error ->
|
||||||
|
error_logger:error_report("TimeStamper: Unable to update entry: ~p", [Error]),
|
||||||
|
make_json_500(YArg, Error)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
delete_entry(YArg, Username, TimelineId, EntryId) ->
|
delete_entry(YArg, Username, TimelineId, EntryId) ->
|
||||||
@ -494,8 +500,8 @@ delete_entry(YArg, Username, TimelineId, EntryId) ->
|
|||||||
case ts_entry:delete(Record) of
|
case ts_entry:delete(Record) of
|
||||||
ok -> {status, 200};
|
ok -> {status, 200};
|
||||||
Error ->
|
Error ->
|
||||||
io:format("Error occurred deleting entry record: ~p", [Error]),
|
error_logger:error_report("Error occurred deleting entry record: ~p", [Error]),
|
||||||
make_json_500(YArg)
|
make_json_500(YArg, Error)
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@ -512,7 +518,7 @@ parse_json_body(YArg) ->
|
|||||||
{done, {ok, EJSON}, _} -> EJSON;
|
{done, {ok, EJSON}, _} -> EJSON;
|
||||||
Error ->
|
Error ->
|
||||||
% TODO: log error internally
|
% TODO: log error internally
|
||||||
io:format("~p", [Error]),
|
error_logger:error_report("Error parsing JSON request body: ~p", [Error]),
|
||||||
throw(make_json_400(YArg))
|
throw(make_json_400(YArg))
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@ -577,6 +583,12 @@ make_json_405(YArg, Fields) ->
|
|||||||
|
|
||||||
[{status, 405}, {content, "application/json", json:encode({struct, F2})}].
|
[{status, 405}, {content, "application/json", json:encode({struct, F2})}].
|
||||||
|
|
||||||
|
make_json_500(_YArg, Error) ->
|
||||||
|
EJSON = {struct, [
|
||||||
|
{status, "internal server error"},
|
||||||
|
{error, io_lib:format("~p", [Error])}]},
|
||||||
|
[{status, 500}, {content, "application/json", json:encode(EJSON)}].
|
||||||
|
|
||||||
make_json_500(_YArg) ->
|
make_json_500(_YArg) ->
|
||||||
EJSON = {struct, [
|
EJSON = {struct, [
|
||||||
{status, "internal server error"}]},
|
{status, "internal server error"}]},
|
||||||
|
@ -11,7 +11,7 @@ create_table(TableOpts) ->
|
|||||||
|
|
||||||
new(TR = #ts_timeline{}) -> ts_common:new(TR).
|
new(TR = #ts_timeline{}) -> ts_common:new(TR).
|
||||||
|
|
||||||
update(TR = #ts_timeline{}) -> ts_commone:update(TR).
|
update(TR = #ts_timeline{}) -> ts_common:update(TR).
|
||||||
|
|
||||||
lookup(Username, TimelineId) ->
|
lookup(Username, TimelineId) ->
|
||||||
case mnesia:dirty_read(ts_timeline, {Username, TimelineId}) of
|
case mnesia:dirty_read(ts_timeline, {Username, TimelineId}) of
|
||||||
|
Loading…
x
Reference in New Issue
Block a user