Add plug to generate Matrix error
This commit is contained in:
parent
dac1a429b9
commit
1ec18163c1
6 changed files with 47 additions and 50 deletions
23
lib/matrix_server_web/plug/error.ex
Normal file
23
lib/matrix_server_web/plug/error.ex
Normal file
|
@ -0,0 +1,23 @@
|
|||
defmodule MatrixServerWeb.Plug.Error do
|
||||
import Plug.Conn
|
||||
import Phoenix.Controller, only: [json: 2]
|
||||
|
||||
@error_code_and_message %{
|
||||
bad_json: {400, "M_BAD_JSON", "Bad request."},
|
||||
user_in_use: {400, "M_USE_IN_USE", "Username is already taken."},
|
||||
invalid_username: {400, "M_INVALID_USERNAME", "Invalid username."},
|
||||
forbidden: {400, "M_FORBIDDEN", "The requested action is forbidden."},
|
||||
unknown_token: {401, "M_UNKNOWN_TOKEN", "Invalid access token."},
|
||||
missing_token: {401, "M_MISSING_TOKEN", "Access token required."}
|
||||
}
|
||||
|
||||
def put_error(conn, error) do
|
||||
{status, errcode, errmsg} = @error_code_and_message[error]
|
||||
data = %{errcode: errcode, error: errmsg}
|
||||
|
||||
conn
|
||||
|> put_status(status)
|
||||
|> json(data)
|
||||
|> halt()
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue