Skip to content

Commit

Permalink
show errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nickthecook committed Mar 15, 2024
1 parent 2668267 commit be0bdfb
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
!/app/assets/builds/.keep
deps/redis
node_modules
deps/postgres
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

ruby "3.2.2"
ruby "~> 3.2"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.1.3", ">= 7.1.3.2"
Expand Down
38 changes: 33 additions & 5 deletions app/services/reply_to_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,30 @@ def execute
conversation: @message.conversation
)

append(reply_id, "messages/spinner", { message: @reply })
prepend(reply_id, "messages/spinner")

streamer.stream do |message|
Rails.logger.info("Got back: #{message}")
@reply.update!(content: @reply.content + message)

convert_to_markdown
end
rescue ResponseStreamer::ResponseStreamerError => e
Rails.logger.error("\n#{e.class.name}: #{e.message}#{e.backtrace.join("\n")}")

append("messages", "messages/error", { error: e.to_s })
rescue ResponseStreamer::NetworkError
raise
rescue LlmClients::ResponseError => e
Rails.logger.error("\n#{e.class.name}: #{e.message}#{e.backtrace.join("\n")}")

append("messages", "messages/error", { error: "#{e.to_s}: #{e.additional_info}" })
rescue StandardError => e
Rails.logger.error("\n#{e.class.name}: #{e.message}#{e.backtrace.join("\n")}")

append("messages", "messages/error", { error: "An internal error occurred" })
ensure
remove(reply_id, "messages/spinner")
end

private
Expand All @@ -30,19 +46,31 @@ def convert_to_markdown
Turbo::StreamsChannel.broadcast_append_to(
"conversations",
target: reply_id,
html: "<script>document.getElementById('#{reply_id}').dataset.markdownTextUpdatedValue = #{Time.current.to_f};</script>"
html: <<~HTML
<script>
document.getElementById('#{reply_id}').dataset.markdownTextUpdatedValue = #{Time.current.to_f};
</script>
HTML
)
end

def append(target, partial, locals)
def append(target, partial, locals = {})
locals.merge!({ message: @reply })
Turbo::StreamsChannel.broadcast_append_to("conversations", target:, partial:, locals:)
end

def replace(target, partial, locals)
def prepend(target, partial, locals = {})
locals.merge!({ message: @reply })
Turbo::StreamsChannel.broadcast_prepend_to("conversations", target:, partial:, locals:)
end

def replace(target, partial, locals = {})
locals.merge!({ message: @reply })
Turbo::StreamsChannel.broadcast_replace_to(target, partial:)
end

def remove(target)
def remove(target, partial, locals = {})
locals.merge!({ message: @reply })
Turbo::StreamsChannel.broadcast_remove_to(target)
end

Expand Down
25 changes: 0 additions & 25 deletions app/services/response_streamer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ class ResponseStreamerError < StandardError; end
class NetworkError < ResponseStreamerError; end
class UnsupportedServerError < ResponseStreamerError; end

class ResponseError < StandardError
attr_reader :additional_info

def initialize(message, additional_info)
@additional_info = additional_info

super(message)
end
end

delegate :input, to: :processor
delegate :output, to: :processor
delegate :stats, to: :client
Expand Down Expand Up @@ -62,21 +52,6 @@ def headers
headers
end

def response_error_for(response)
additional_info = [response.uri]

error = begin
JSON.parse(response.body)["error"]
rescue JSON::ParserError
nil
end
additional_info.append(error) if error

ResponseError.new(
"Server responded with #{response.code}: #{response.message}", additional_info
)
end

def processor
@processor ||= MessageProcessor.new
end
Expand Down
3 changes: 3 additions & 0 deletions app/views/messages/_error.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="bg-red-400 text-secondary-950 dark:bg-red-800 dark:text-secondary-200 text-muted text-sm rounded-lg px-2">
<%= error %>
</div>
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@

Message.find_or_create_by!(content: "Hello, world!") do |msg|
msg.conversation = Conversation.first
msg.user = User.first
msg.author = User.first
end
2 changes: 2 additions & 0 deletions ops.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ dependencies:
- overmind
docker:
- deps
custom:
- bundle
forwards:
deps: deps
actions:
Expand Down

0 comments on commit be0bdfb

Please sign in to comment.