aboutsummaryrefslogtreecommitdiff
path: root/apps/dreki_web/src/dreki_web_error.erl
blob: e3005821365c18d4075e59c85b0fcb5b9192f872 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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}.