2021-08-14 13:20:42 +00:00
|
|
|
defmodule MatrixServerWeb.Error do
|
2021-06-25 23:14:09 +00:00
|
|
|
import Plug.Conn
|
|
|
|
import Phoenix.Controller, only: [json: 2]
|
|
|
|
|
2021-07-29 14:59:40 +00:00
|
|
|
@error_map %{
|
2021-06-25 23:14:09 +00:00
|
|
|
bad_json: {400, "M_BAD_JSON", "Bad request."},
|
2021-07-13 17:35:02 +00:00
|
|
|
user_in_use: {400, "M_USER_IN_USE", "Username is already taken."},
|
2021-06-25 23:14:09 +00:00
|
|
|
invalid_username: {400, "M_INVALID_USERNAME", "Invalid username."},
|
|
|
|
forbidden: {400, "M_FORBIDDEN", "The requested action is forbidden."},
|
2021-06-26 20:02:18 +00:00
|
|
|
unrecognized: {400, "M_UNRECOGNIZED", "Unrecognized request."},
|
2021-06-27 15:28:28 +00:00
|
|
|
unknown: {400, "M_UNKNOWN", "An unknown error occurred."},
|
2021-07-29 20:06:02 +00:00
|
|
|
invalid_room_state:
|
2021-07-30 13:56:24 +00:00
|
|
|
{400, "M_INVALID_ROOM_STATE", "The request would leave the room in an invalid state."},
|
2021-08-10 16:02:53 +00:00
|
|
|
unauthorized: {400, "M_UNAUTHORIZED", "The request was unauthorized."},
|
2021-08-21 09:25:36 +00:00
|
|
|
invalid_param: {400, "M_INVALID_PARAM", "A request parameter was invalid."},
|
2021-08-21 19:39:28 +00:00
|
|
|
missing_param: {400, "M_MISSING_PARAM", "A request parameter is missing."},
|
2021-06-25 23:14:09 +00:00
|
|
|
unknown_token: {401, "M_UNKNOWN_TOKEN", "Invalid access token."},
|
2021-07-30 13:56:24 +00:00
|
|
|
missing_token: {401, "M_MISSING_TOKEN", "Access token required."},
|
|
|
|
not_found: {404, "M_NOT_FOUND", "The requested resource was not found."},
|
2021-08-10 16:02:53 +00:00
|
|
|
room_alias_exists: {409, "M_UNKNOWN", "The given room alias already exists."}
|
2021-06-25 23:14:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 13:56:24 +00:00
|
|
|
def put_error(conn, {error, msg}), do: put_error(conn, error, msg)
|
|
|
|
|
2021-07-29 14:59:40 +00:00
|
|
|
def put_error(conn, error, msg \\ nil) do
|
|
|
|
{status, errcode, default_msg} = @error_map[error]
|
2021-07-29 20:06:02 +00:00
|
|
|
data = %{errcode: errcode, error: msg || default_msg}
|
2021-06-25 23:14:09 +00:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(status)
|
|
|
|
|> json(data)
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|