Skip to content

Commit

Permalink
Fix an issue where controllers could not set their own error body or …
Browse files Browse the repository at this point in the history
…content (#45)

* Fix an issue where controllers could not set their own error body or content

* GH Actions: remove automatic publish and add OTP 26

* Fix types
  • Loading branch information
mworrell committed Jun 20, 2023
1 parent 855fccb commit 8d470e7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 35 deletions.
18 changes: 0 additions & 18 deletions .github/workflows/hex-publish.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

strategy:
matrix:
otp_version: [23,24,25]
otp_version: [24,25,26]
os: [ubuntu-latest]

container:
Expand Down
7 changes: 4 additions & 3 deletions src/cowmachine.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
{applications, [
kernel,
stdlib,
inets,
zotonic_stdlib,
ranch,
cowlib,
cowboy
]},
{env, [
{use_sendfile, erlang}
]},
{env, []},
{licenses, ["Apache-2.0"]},
{links, [{"GitHub", "https://github.com/zotonic/cowmachine"}]}
]}.
31 changes: 20 additions & 11 deletions src/cowmachine_decision_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ is_cacheable(generate_etag) -> true;
is_cacheable(_) -> false.

-spec controller_call_process(ContentType, State, Context) -> Result when
ContentType :: cow_http_hd:media_type(),
State :: cmstate(),
ContentType :: cow_http_hd:media_type(),
State :: cmstate(),
Context :: cowmachine_req:context(),
Result :: {boolean() | ContentType, State, Context}.
Result :: {Res, State, Context},
Res :: boolean() | cowmachine_req:halt() | {error, any(), any()} | {error, any()} |
cowmachine_req:resp_body().
controller_call_process(ContentType, State, Context) ->
{T, Context1} = cowmachine_controller:do_process(ContentType, State, Context),
{T, State, Context1}.
Expand Down Expand Up @@ -137,11 +139,18 @@ respond(Code, State, Context) ->
ExpCtx0)
end,
{StateExp, ExpCtx};
% Let the error controller handle 4xx and 5xx errors
E when E =:= 401; E =:= 403; E =:= 404; E=:= 410;
(E >= 500 andalso E =< 599) ->
controller_call(finish_request, State, Context),
throw({stop_request, Code});
E when E =:= 401; E =:= 403; E =:= 404; E=:= 410; (E >= 500 andalso E =< 599) ->
% Http errors - maybe handled by error controller
HasRespContentType = cowmachine_req:get_resp_header(<<"content-type">>, Context) =/= undefined,
HasBody = cowmachine_req:resp_body(Context) =/= undefined,
if
HasBody andalso HasRespContentType ->
{State, Context};
true ->
% Let the error controller handle 4xx and 5xx errors without body
controller_call(finish_request, State, Context),
throw({stop_request, Code})
end;
_ ->
{State, Context}
end,
Expand Down Expand Up @@ -778,9 +787,9 @@ process_helper(ContentTypeAccepted, State, Context) ->
{halt, _} -> Result;
{error, _, _} -> Result;
{error, _} -> Result;
true when is_boolean(Res) -> Result;
false when is_boolean(Res) -> Result;
RespBody ->
true -> Result;
false -> Result;
RespBody when is_binary(RespBody); is_list(RespBody) ->
C3 = cowmachine_req:set_resp_body(RespBody, C2),
{body, S2, C3}
end.
Expand Down
2 changes: 1 addition & 1 deletion src/cowmachine_response.erl
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ send_device_body(Context, Length, IO) ->
-spec send_file_body(Context, Length, File, FinNoFin) -> Result when
Context :: cowmachine_req:context(),
Length :: non_neg_integer(),
File :: Filename | file:iodata(),
File :: Filename | iodata(),
Filename :: file:name_all(),
FinNoFin :: fin | nofin,
Result :: cowmachine_req:context().
Expand Down
2 changes: 1 addition & 1 deletion src/cowmachine_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ accept_header_to_media_types(HeadVal) ->

-spec normalize_provided(Provided) -> Result when
Provided :: [cowmachine_req:media_type()],
Result :: cow_http_hd:media_type().
Result :: [cow_http_hd:media_type()].
normalize_provided(Provided) ->
[ normalize_content_type(X) || X <- Provided ].

Expand Down

0 comments on commit 8d470e7

Please sign in to comment.