From 032ea859fb3f1e203a6f4c763180eb1a3efc25d1 Mon Sep 17 00:00:00 2001 From: optclblast Date: Wed, 11 Sep 2024 00:25:50 +0300 Subject: [PATCH] request parsing fix --- calls.api | 11 ---- .../auth_controller/auth_controller.ex | 60 +++++++++++-------- .../controllers/errors/errors.ex | 6 +- 3 files changed, 41 insertions(+), 36 deletions(-) delete mode 100644 calls.api diff --git a/calls.api b/calls.api deleted file mode 100644 index 1b34a2d..0000000 --- a/calls.api +++ /dev/null @@ -1,11 +0,0 @@ -{ - "body": { - "content": "", - "file": false, - "fileSize": false - }, - "headers": {}, - "method": "GET", - "title": "New HTTP Request", - "url": "" -} \ No newline at end of file diff --git a/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex b/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex index d15d56d..ca7c9a4 100644 --- a/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex +++ b/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex @@ -8,6 +8,7 @@ defmodule DrainCloudCoreWeb.AuthController do def register(conn = %Plug.Conn{}, _params) do req = Request.from_request(conn) + Log.debug("new register request: #{req}") model = Request.to_model(req) @@ -31,22 +32,10 @@ defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do alias DrainCloudCore.Auth.Users, as: User alias DrainCloudCoreWeb.Errors.InvalidArgumentException, as: InvalidArgumentException - def from_request(conn) do - res = Plug.Conn.read_body(conn) - :logger.warning(Kernel.inspect(res)) + def from_request(conn = %Plug.Conn{}) do + :logger.debug("[from_request] incoming request: #{Kernel.inspect(conn.params)}") - case res do - {:error, _} -> - raise "failed to read request body: #{:error}" - - {_, data, _} -> - req = Poison.decode!(data, as: %Request{}) - validate_request(req) - req - - _ -> - raise "failed to read request body. unexpected result #{Kernel.inspect(res)}" - end + validate_and_build(conn.params) end def to_model(req = %Request{}) do @@ -56,22 +45,45 @@ defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do } end - defp validate_request(req = %Request{}) do - if String.length(req.login) < 3 do + defp validate_and_build(req) do + validate(req) + |> build + end + + defp validate(req) do + validate_login(req) + |> validate_password + end + + defp validate_login(req) do + if String.length(Map.get(req, "login")) >= 3 do + req + else raise InvalidArgumentException, %{ message: "login must be 3 symbols at minimum", args: ["login"] } - else - if String.length(req.password) < 8 do - raise InvalidArgumentException, %{ - message: "password must be 8 symbols at minimum", - args: ["password"] - } - end end end + defp validate_password(req) do + if String.length(Map.get(req, "password")) >= 8 do + req + else + raise InvalidArgumentException, %{ + message: "password must be 8 symbols at minimum", + args: ["password"] + } + end + end + + defp build(req) do + %Request{ + login: Map.get(req, "login"), + password: Map.get(req, "password") + } + end + defimpl String.Chars, for: DrainCloudCoreWeb.AuthController.RegisterRequest do def to_string(req) do case Jason.encode(req) do diff --git a/lib/draincloud_core_web/controllers/errors/errors.ex b/lib/draincloud_core_web/controllers/errors/errors.ex index 480901b..7fd488a 100644 --- a/lib/draincloud_core_web/controllers/errors/errors.ex +++ b/lib/draincloud_core_web/controllers/errors/errors.ex @@ -2,11 +2,15 @@ defmodule DrainCloudCoreWeb.Errors.InvalidArgumentException do alias __MODULE__, as: InvalidArgumentException defexception [:message, :args] + @impl true + def exception(%{message: message, args: args}) do + %InvalidArgumentException{message: message, args: args} + end + @impl true def exception(term) do case term do [] -> %InvalidArgumentException{} - {message, args} -> %InvalidArgumentException{message: message, args: args} _ -> %InvalidArgumentException{message: "Error: " <> term} end end