Packages

Gleam Client for Sentry Error tracking

Current section

Files

Jump to
gleam_sentry gen src gleam_sentry.erl
Raw

gen/src/gleam_sentry.erl

-module(gleam_sentry).
-compile(no_auto_import).
-export([init/2, capture_exception/4]).
auth_header(Key, Timestamp) ->
gleam@string:concat(
[<<"Sentry sentry_version="/utf8>>,
gleam@int:to_string(7),
<<", sentry_client="/utf8>>,
<<"sentry_gleam/0.1"/utf8>>,
<<", sentry_timestamp="/utf8>>,
gleam@int:to_string(Timestamp),
<<", sentry_key="/utf8>>,
Key]
).
init(Dsn, Environment) ->
case case gleam@uri:parse(Dsn) of
{ok, {uri, _, {some, Key}, {some, Host}, _, Path, _, _}} ->
case case gleam@string:split_once(Path, <<"/"/utf8>>) of
{ok, {<<""/utf8>>, Project_id}} ->
{ok, Project_id};
_ ->
{error, nil}
end of
{error, Gleam@try_error} -> {error, Gleam@try_error};
{ok, Project_id@1} ->
{ok, {client, Host, Key, Project_id@1, Environment}}
end;
_ ->
{error, nil}
end of
{error, Gleam@try_error@1} -> {error, Gleam@try_error@1};
{ok, Client} ->
{ok, Client}
end.
capture_exception(Client, Exception, Stacktrace, Timestamp) ->
{client, Host, Key, Project_id, Environment} = Client,
Event = gleam@json:object(
[{<<"timestamp"/utf8>>, gleam@json:int(Timestamp)},
{<<"environment"/utf8>>, gleam@json:string(Environment)},
{<<"exception"/utf8>>, exception_to_json(Exception, Stacktrace)},
{<<"platform"/utf8>>, gleam@json:string(<<"gleam"/utf8>>)}]
),
Path = gleam@string:concat(
[<<"/api/"/utf8>>, Project_id, <<"/store/"/utf8>>]
),
Body = gleam@base:encode64(
gleam@bit_string:from_string(zlib:compress(gleam@json:encode(Event))),
false
),
Request = gleam@http:set_req_body(
gleam@http:prepend_req_header(
gleam@http:prepend_req_header(
gleam@http:prepend_req_header(
gleam@http:prepend_req_header(
gleam@http:set_path(
gleam@http:set_host(
gleam@http:set_scheme(
gleam@http:set_method(
gleam@http:default_req(),
post
),
https
),
Host
),
Path
),
<<"content-type"/utf8>>,
<<"application/json"/utf8>>
),
<<"x-sentry-auth"/utf8>>,
auth_header(Key, Timestamp)
),
<<"user-agent"/utf8>>,
<<"sentry_gleam/1"/utf8>>
),
<<"accept"/utf8>>,
<<"applicaton/json"/utf8>>
),
Body
),
gleam@httpc:send(Request).
exception_detail(Reason) ->
case Reason of
badarg ->
{<<"badarg"/utf8>>, <<""/utf8>>};
badarith ->
{<<"badarith"/utf8>>, <<""/utf8>>};
{badmatch, Term} ->
{<<"badmatch"/utf8>>, gleam@beam:format(Term)};
function_clause ->
{<<"function_clause"/utf8>>, <<""/utf8>>};
{case_clause, Term@1} ->
{<<"case_clause"/utf8>>, gleam@beam:format(Term@1)};
if_clause ->
{<<"if_clause"/utf8>>, <<""/utf8>>};
{try_clause, Term@2} ->
{<<"try_clause"/utf8>>, gleam@beam:format(Term@2)};
undef ->
{<<"undef"/utf8>>, <<""/utf8>>};
{badfun, Term@3} ->
{<<"badfun"/utf8>>, gleam@beam:format(Term@3)};
{badarity, Term@4} ->
{<<"badarity"/utf8>>, gleam@beam:format(Term@4)};
timeout_value ->
{<<"timeout_value"/utf8>>, <<""/utf8>>};
noproc ->
{<<"noproc"/utf8>>, <<""/utf8>>};
{nocatch, Term@5} ->
{<<"nocatch"/utf8>>, gleam@beam:format(Term@5)};
system_limit ->
{<<"system_limit"/utf8>>, <<""/utf8>>}
end.
exception_to_json(Exception, Stacktrace) ->
Detail = exception_detail(Exception),
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(erlang:element(1, Detail))},
{<<"value"/utf8>>, gleam@json:string(erlang:element(2, Detail))},
{<<"stacktrace"/utf8>>, stacktrace_to_json(Stacktrace)}]
).
stacktrace_to_json(Stacktrace) ->
Frames = gleam@json:list(
gleam@list:map(
gleam@list:reverse(Stacktrace),
fun stack_frame_to_json/1
)
),
gleam@json:object([{<<"frames"/utf8>>, Frames}]).
stack_frame_to_json(Frame) ->
{Module, Function, Arity, Filename, Line_number} = Frame,
Function@1 = gleam@string:join(
[Function, gleam@int:to_string(Arity)],
<<"/"/utf8>>
),
gleam@json:object(
[{<<"filename"/utf8>>, gleam@json:string(Filename)},
{<<"function"/utf8>>, gleam@json:string(Function@1)},
{<<"module"/utf8>>, gleam@json:string(gleam@atom:to_string(Module))},
{<<"lineno"/utf8>>, gleam@json:int(Line_number)}]
).