Created timestamper_dev module, logging.

This commit is contained in:
Jonathan Bernard 2011-03-11 15:08:18 +00:00
parent b59b628cb5
commit 1b981b206c
4 changed files with 41 additions and 11 deletions

View File

@ -3,6 +3,7 @@
start() ->
ok = application:load(mnesia),
ok = application:set_env(mnesia, dir, "/usr/lib/yaws/mnesia"),
ok = mnesia:start(),
ok.

17
src/timestamper_dev.erl Normal file
View 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.

View File

@ -21,8 +21,8 @@ out(YArg) ->
case catch dispatch_request(YArg, Session, PathElements) of
{'EXIT', Err} ->
% TODO: log error internally
io:format("~p", [Err]),
make_json_500(YArg);
error_logger:error_report("TimeStamper: ~p", [Err]),
make_json_500(YArg, Err);
Other -> Other
end.
@ -306,7 +306,9 @@ put_timeline(YArg, Username, TimelineId) ->
{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.
post_timeline(YArg, Username, TimelineId) ->
@ -333,7 +335,9 @@ post_timeline(YArg, Username, TimelineId) ->
[{status, "no such timeline"},
{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.
delete_timeline(_YArg, _Username, _TimelineId) -> {status, 405}.
@ -458,8 +462,8 @@ put_entry(YArg, Username, TimelineId) ->
{content, "application/json", JSONResponse};
OtherError ->
io:format("Could not create entry: ~p", [OtherError]),
make_json_500(YArg)
error_logger:error_report("TimeStamper: Could not create entry: ~p", [OtherError]),
make_json_500(YArg, OtherError)
end.
post_entry(YArg, Username, TimelineId, EntryId) ->
@ -479,7 +483,9 @@ post_entry(YArg, Username, TimelineId, EntryId) ->
no_record -> make_json_404(YArg,
[{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.
delete_entry(YArg, Username, TimelineId, EntryId) ->
@ -494,8 +500,8 @@ delete_entry(YArg, Username, TimelineId, EntryId) ->
case ts_entry:delete(Record) of
ok -> {status, 200};
Error ->
io:format("Error occurred deleting entry record: ~p", [Error]),
make_json_500(YArg)
error_logger:error_report("Error occurred deleting entry record: ~p", [Error]),
make_json_500(YArg, Error)
end
end.
@ -512,7 +518,7 @@ parse_json_body(YArg) ->
{done, {ok, EJSON}, _} -> EJSON;
Error ->
% TODO: log error internally
io:format("~p", [Error]),
error_logger:error_report("Error parsing JSON request body: ~p", [Error]),
throw(make_json_400(YArg))
end.
@ -577,6 +583,12 @@ make_json_405(YArg, Fields) ->
[{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) ->
EJSON = {struct, [
{status, "internal server error"}]},

View File

@ -11,7 +11,7 @@ create_table(TableOpts) ->
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) ->
case mnesia:dirty_read(ts_timeline, {Username, TimelineId}) of