Skip to content

Commit

Permalink
Helper to create error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
geekingfrog committed Sep 1, 2024
1 parent a205975 commit 9806b25
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
8 changes: 1 addition & 7 deletions lib/teiserver/autohost/tachyon_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ defmodule Teiserver.Autohost.TachyonHandler do

def handle_command(command_id, _message_type, message_id, _message, state) do
resp =
%{
type: :response,
status: :failed,
reason: :command_unimplemented,
commandId: command_id,
messageId: message_id
}
Schema.error_response(command_id, message_id, :command_unimplemented)
|> Jason.encode!()

{:reply, :ok, {:text, resp}, state}
Expand Down
8 changes: 1 addition & 7 deletions lib/teiserver/player/tachyon_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,7 @@ defmodule Teiserver.Player.TachyonHandler do
) :: WebSock.handle_result()
def handle_command(command_id, _message_type, message_id, _message, state) do
resp =
%{
type: :response,
status: :failed,
reason: :command_unimplemented,
commandId: command_id,
messageId: message_id
}
Schema.error_response(command_id, message_id, :command_unimplemented)
|> Jason.encode!()

{:reply, :ok, {:text, resp}, state}
Expand Down
36 changes: 36 additions & 0 deletions lib/teiserver/tachyon/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,40 @@ defmodule Teiserver.Tachyon.Schema do
end
end
end

@doc """
helper to create a tachyon response
"""
@spec response(command_id(), message_id(), term()) :: map()
def response(command_id, message_id, data \\ nil) do
resp = %{
type: :response,
status: :success,
commandId: command_id,
messageId: message_id
}

if is_nil(data) do
resp
else
Map.put(resp, :data, data)
end
end

@spec error_response(command_id(), message_id(), term(), String.t() | nil) :: map()
def error_response(command_id, message_id, reason, details \\ nil) do
resp = %{
type: :response,
status: :failed,
commandId: command_id,
messageId: message_id,
reason: reason
}

if is_nil(details) do
resp
else
Map.put(resp, :details, details)
end
end
end
27 changes: 3 additions & 24 deletions lib/teiserver/tachyon/transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,14 @@ defmodule Teiserver.Tachyon.Transport do

:missing_schema ->
resp =
%{
type: :response,
status: :failed,
reason: :command_unimplemented,
commandId: command_id,
messageId: message_id
}
Schema.error_response(command_id, message_id, :command_unimplemented)
|> Jason.encode!()

{:reply, :ok, {:text, resp}, state}

{:error, err} ->
resp =
%{
type: :response,
status: :failed,
reason: :internal_error,
commandId: command_id,
messageId: message_id,
details: inspect(err)
}
Schema.error_response(command_id, message_id, :internal_error, inspect(err))
|> Jason.encode!()

{:reply, :ok, {:text, resp}, state}
Expand Down Expand Up @@ -131,15 +118,7 @@ defmodule Teiserver.Tachyon.Transport do
str_err = inspect({e, __STACKTRACE__})
Logger.error([inspect(message), str_err])

resp = %{
type: :response,
status: :failed,
messageId: message_id,
commandId: command_id,
reason: :internal_error,
details: str_err
}

resp = Schema.error_response(command_id, message_id, :internal_error, str_err)
{:push, {:text, Jason.encode!(resp)}, state}
end
end
Expand Down

0 comments on commit 9806b25

Please sign in to comment.