aboutsummaryrefslogtreecommitdiff
path: root/apps/dreki_web/src/dreki_web_error.erl
diff options
context:
space:
mode:
Diffstat (limited to 'apps/dreki_web/src/dreki_web_error.erl')
-rw-r--r--apps/dreki_web/src/dreki_web_error.erl20
1 files changed, 20 insertions, 0 deletions
diff --git a/apps/dreki_web/src/dreki_web_error.erl b/apps/dreki_web/src/dreki_web_error.erl
new file mode 100644
index 0000000..e300582
--- /dev/null
+++ b/apps/dreki_web/src/dreki_web_error.erl
@@ -0,0 +1,20 @@
+-module(dreki_web_error).
+-behaviour(cowboy_handler).
+-export([init/2]).
+
+init(Req, not_found) ->
+ reply(Req, 404, <<"Not Found">>, undefined);
+init(Req, State = #{code := Code, status := Status}) ->
+ reply(Req, Code, Status, maps:get(message, State, undefined));
+init(Req, oauth2) ->
+ {ok, ErrorDescription} = dreki_web:req_param(Req, <<"error_description">>),
+ reply(Req, 500, <<"Error">>, ErrorDescription);
+init(Req = #{method := <<"GET">>}, _) ->
+ {ok, ErrorId} = dreki_web:req_param(Req, <<"id">>),
+ {ok, #{<<"error">> := #{<<"status">> := Status, <<"code">> := Code, <<"message">> := Msg}}} = ory_kratos:error(ErrorId),
+ reply(Req, Code, Status, Msg).
+
+reply(Req0, Code, Status, Msg) ->
+ Json = #{<<"error">> => #{<<"code">> => Code, <<"status">> => Status, <<"message">> => Msg}},
+ Req = dreki_web:reply_json(Req0, Code, Json),
+ {ok, Req, undefined}.