2011-12-29 00:06:22 +01:00
|
|
|
%% Feel free to use, reuse and abuse the code in this file.
|
|
|
|
|
2013-04-24 20:28:44 +02:00
|
|
|
-module(http_errors).
|
2011-12-29 00:06:22 +01:00
|
|
|
-behaviour(cowboy_http_handler).
|
2013-01-22 02:34:18 +01:00
|
|
|
-export([init/3, handle/2, terminate/3]).
|
2011-12-29 00:06:22 +01:00
|
|
|
|
|
|
|
init({_Transport, http}, Req, _Opts) ->
|
2014-09-23 16:43:29 +03:00
|
|
|
#{'case' := Case} = cowboy_req:match_qs(Req, ['case']),
|
|
|
|
case_init(Case, Req).
|
2011-12-29 00:06:22 +01:00
|
|
|
|
|
|
|
case_init(<<"init_before_reply">> = Case, _Req) ->
|
2014-04-21 21:22:08 +02:00
|
|
|
cowboy_error_h:ignore(?MODULE, case_init, 2),
|
2014-09-24 15:07:59 +03:00
|
|
|
error(Case);
|
2011-12-29 00:06:22 +01:00
|
|
|
case_init(<<"init_after_reply">> = Case, Req) ->
|
2014-04-21 21:22:08 +02:00
|
|
|
cowboy_error_h:ignore(?MODULE, case_init, 2),
|
2014-09-23 16:43:29 +03:00
|
|
|
_ = cowboy_req:reply(200, [], "http_handler_crashes", Req),
|
2014-09-24 15:07:59 +03:00
|
|
|
error(Case);
|
2011-12-29 00:06:22 +01:00
|
|
|
case_init(<<"init_reply_handle_error">> = Case, Req) ->
|
2014-09-23 16:43:29 +03:00
|
|
|
Req1 = cowboy_req:reply(200, [], "http_handler_crashes", Req),
|
2011-12-29 00:06:22 +01:00
|
|
|
{ok, Req1, Case};
|
|
|
|
case_init(<<"handle_before_reply">> = Case, Req) ->
|
|
|
|
{ok, Req, Case};
|
|
|
|
case_init(<<"handle_after_reply">> = Case, Req) ->
|
|
|
|
{ok, Req, Case}.
|
|
|
|
|
|
|
|
handle(_Req, <<"init_reply_handle_error">> = Case) ->
|
2014-04-21 21:22:08 +02:00
|
|
|
cowboy_error_h:ignore(?MODULE, handle, 2),
|
2014-09-24 15:07:59 +03:00
|
|
|
error(Case);
|
2011-12-29 00:06:22 +01:00
|
|
|
handle(_Req, <<"handle_before_reply">> = Case) ->
|
2014-04-21 21:22:08 +02:00
|
|
|
cowboy_error_h:ignore(?MODULE, handle, 2),
|
2014-09-24 15:07:59 +03:00
|
|
|
error(Case);
|
2011-12-29 00:06:22 +01:00
|
|
|
handle(Req, <<"handle_after_reply">> = Case) ->
|
2014-04-21 21:22:08 +02:00
|
|
|
cowboy_error_h:ignore(?MODULE, handle, 2),
|
2014-09-23 16:43:29 +03:00
|
|
|
_ = cowboy_req:reply(200, [], "http_handler_crashes", Req),
|
2014-09-24 15:07:59 +03:00
|
|
|
error(Case).
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2013-01-22 02:34:18 +01:00
|
|
|
terminate(_, _, _) ->
|
2011-12-29 00:06:22 +01:00
|
|
|
ok.
|